Search code examples
batch-filecmdbatch-rename

how to rename file via batch from other folder?


I have this which works if the batch is in the folder, but fails when I run it from any other folder

@echo off
if exist "E:\[0] EE\feed\*.ss" (
  if not exist "E:\[0] EE\feed\O.ss" (
    FOR %%A IN ("E:\[0] EE\feed\*.ss") DO REN "*.ss" "O.ss" > nul 2>&1 
  )
)
@echo on
pause

also (if its possible) is there a way how to not rename file if file is "O2.ss" so basicly I need to renname any file with extension .ss (if such file is in the folder) to O.ss, but avoid renaming file O2.ss ??


Solution

  • This is how I would accomplish the task. Pretty straight forward when you use the IF command to do the heavy lifting.

    @echo off
    cd /D "E:\[0] EE\feed\"
    if exist "*.ss" (
        if not exist "O.ss" (
            FOR %%A IN (*.ss) DO (
                IF /I NOT "%%~A"=="O2.ss" REN "%%~A" "O.ss" > nul 2>&1
            )
        )
    )
    
    pause