0

I am using a CMD batch file to rename hundreds of files with the REN command, but this one file with a "%" in the name is being skipped:

REN "40% @ thesuper.ogg" "ZXS 40% @ thesuper.ogg"

I'm guessing the batch file interpreter is seeing the % as a variable, especially when followed by the @ sign afterwards. The problem is, both % and @ should be there in the filename. Is there a way to force the rename?

4
  • I believe what you may be looking for is called "escaping", which is the term for telling the application that what follows is NOT a command character but is just part of a string that it should treat AS a string. I'm not sure how to escape these, but I'd guess that this keyword may help you find the right answer to this. Commented Sep 4 at 18:59
  • 1
    Thanks, music2myear, your reply lead me to the answer. It seemed that all I had to do was to use "%%" in both cases instead of just the "%". It reminds me of a similar issue in Windows application programming, where the single & was used in the IDE to put an underline in the following character, and so if you wanted to use the & normally, you would put it twice. Commented Sep 4 at 19:14
  • Please edit the question to include the complete batch file.
    – DavidPostill
    Commented Sep 4 at 19:14
  • Please take our short tour and learn how the site is designed to work. Solutions belong to answers, not to questions. And do not litter the title. Commented Sep 4 at 20:28

1 Answer 1

0
@echo off

cd /d "D:\Path\To\Your\Files.ogg"

for /f tokens^=1*delims^=^%% %%i in =;('
     "%WinDir%\System32\where.exe" .:*^%%*.ogg
   ');= do rename "%%~fi%%%%~j" "ZXS 40%%%%~j"

This for /f loop command lists .ogg files that contain the % character in their names. It defines the file name in two parts: the portion before the % and the portion after it. To handle the % correctly in the command, an extra % is added as an escape for the % itself, while the initial % is escaped using ^. This prevents the command interpreter from misinterpreting the double %% as a reference to a variable with no value (null). The original command would fail to rename these files because it doesn’t properly account for the escaping required when handling the % character in file names.

But note that @ does not interfere in any way with the rename command via the command line and/or the batch file.

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