5

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?

1 Answer 1

7

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
7
  • Thank you Dennis, your method works..although, it adds the word twice.."[Replays][Replays] bla bla.mp3...i tried "move" instead of "ren" but same result..
    – TonalDev
    Commented Dec 30, 2012 at 18:04
  • 1
    Interesting. It behaves differently on my machine. That might depend on the version or on the number of files.
    – Dennis
    Commented Dec 30, 2012 at 18:26
  • Trying your workaround now...i'm using Windows Server 2012
    – TonalDev
    Commented Dec 30, 2012 at 18:29
  • 1
    You're welcome! It's for that behaves differently. It doesn't read the directory contents at once, so it effectively finds renamed files twice.
    – Dennis
    Commented Dec 30, 2012 at 18:33
  • 2
    Simple FOR iterations can be affected by the DO clause. FOR /F cannot be affected by DO clause. You can avoid the temp file by using FOR /F to process the DIR command: for /f "eol=: delims=" %%a in ('dir /b *.mp3') do .... I added EOL=: to guard against file names that start with ;, though I doubt you will ever run into one :)
    – dbenham
    Commented Dec 30, 2012 at 19:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .