Batch Rename Script

2

0

I have been trying all morning to try to write a simple batch rename script. This is my first time trying this sort of thing. I have been trying to use For /R so that my command recurses through subfolders but it is not working.

My code looks like this: FOR /R %%a in (.) do REN "i.JPG" "inlet.JPG"

REN is working in the parent directory, but not recursing through subdirectories. Can anyone see what I am doing wrong?

Thank you.

eyerah

Posted 2018-01-05T18:43:34.257

Reputation: 21

You need to add the /d option in for if you want to match directories. Also, you need to use "%%a\i.JPG" in the ren command source file name. – AFH – 2018-01-05T19:02:08.920

Thank you. I fixed the filename and added the %%a. This seemed to solve the problem. What do you mean match directories? – eyerah – 2018-01-05T19:06:13.690

By default for matches files, not directories, unless you add /d: see for /? for more details. – AFH – 2018-01-05T19:08:19.843

Further investigation shows that . will be matched without /d - I don't fully understand why. What %%a\i.JPG expands to is SubDirName\.\i.JPG. A neater solution is FOR /R %%a in (i.JPG) do REN "%%a" "inlet.JPG": this avoids errors on subdirectories with no i.JPG file in them, especially if you want to rerun the batch file. – AFH – 2018-01-05T19:23:58.230

@eyerah Can you specify what you really want to do with batch file? Is there only a single file or many file with same name? – Biswapriyo – 2018-01-05T21:25:49.183

I've always linked using the FOR /F and then putting the DIR /S commands to do the REN commands if doing so recursively—just a personal preference I suppose but always works very well for me. – Pimp Juice IT – 2018-01-06T01:10:46.743

Thanks all for the thoughtful comments and welcoming vibes. @Biswapriyo

I just wanted a script to rename photos that I had previously sorted through and added simple codes (i.e., "i"). In sub directory there were many files, but no files with the same name twice. – eyerah – 2018-01-11T01:09:56.703

No answers