Windows – Append to start of filename

batchrenamescriptwindows

I'm trying to add the word [Replay] to all mp3 files in a folder using a batch file like so:

@echo off
for %a in (c:\folder\*.mp3) do ren %a [Replay]%a

But it wont rename the files.

all the examples i tried from googling appends the word but runs over the next characters in the filename, i just need to add the word to the start without running letters over.

Any thoughts?

Best Answer

  • There are three problems with your batch file:

    • In batch files, you have to use %%a instead of %a.

    • %%a will hold the full path, not just the filename.

    • The rename will fail if there are spaces in the filename.

    In general, I'd also recommend leaving echo on while troubleshooting.

    Try this instead:

    cd /d c:\folder
    for %%a in (*.mp3) do ren "%%a" "[Replay]%%a"
    

    If that renames the files twice, for is reading the directory entries as it goes. As a workaround, you can save the list in a temporary file:

    cd /d c:\folder
    dir /b *.mp3 > temp
    for /f "delims=" %%a in (temp) do ren "%%a" "[Replay]%%a"
    del temp