Search code examples
batch-filewindow

Rename file with the .bak extension


I am trying to rename a file that has the .bak extension. But I won't know the test56.bak name since it changes all the time.

So lets say:

test56.bak

Rename to:

Upload.bak

I have tried this but it does not work:

rename F:\BackupRestore *.bak Upload.bak

Solution

  • syntax you used is incorrect

    REN [drive:][path]SourceMask TargetMask

    correct syntax:

    rename F:\BackupRestore\*.bak Upload.bak
    

    for multiple file

    dir c:\bkp\ /A:-D /b | findstr.exe /n /R "*.bkp" > %temp%/file-count.txt
    
    FOR /F "tokens=1-10 delims=:" %%A IN ('type "%temp%/file-count.txt"') do set NUMBER-OF-FILES=%%A
    
    FOR /L %%A IN (1,1,%number-of-files%) DO CALL rename %%A
    :rename
    
    FOR /F "tokens=1-10 delims=:" %%A IN ('type "%temp%/file-count.txt" ^| findstr "%1:"') do ren %%B upload-%RANDOM%.bak