Solved Batch File to Rename Files recursively without variable

May 23, 2013 at 14:32:11
Specs: Windows Vista
Hello,

Can anybody advise me on which command I should use for my batch file problem. I'm new to batch files but I'm learning and I want to create a batch files that renames files that start with "Audit_" and change each file in all sub folders to "QREP_Audit_". The "Rename" command works well but if I want it to apply to subfolders then I think I need to use FOR /R but I don't know about variables. Any help is kindly appreciated.


See More: Batch File to Rename Files recursively without variable

Report •

#1
May 23, 2013 at 15:33:54

Report •

#2
May 23, 2013 at 19:32:16
✔ Best Answer
Yeh, you're right, FOR /R is made to do that:
@echo off & setlocal
for /r %%a in (audit_*.*) do ren %%~pnxa qrep_%%~nxa

You can also use FOR /F like:
for /f %%a in ('dir /b /s audit_*.*') do ren %%a qrep_%%~nxa

You might want to test with a small test-set before unleashing the beast, although I did test in on my end and it worked ok (I've hardly ever used FOR /R, so I had to trial/error till I got it right).


Report •

#3
May 24, 2013 at 05:24:35
Thanks Paulsep. I had already looked at that site but unfortunately it doesn't really explain the variables well enough.

Cheers.


Report •

Related Solutions

#4
May 24, 2013 at 05:28:36
Thanks nbrane, that worked great! If possible can I ask you to explain that parts of the script - %%~pnxa qrep_%%~nxa please? Is this to set the current location? Also how would I handle a space character in a filename using this command?

Thanks.


Report •

#5
May 24, 2013 at 18:33:00
Open a Command Prompt, and type the following:
for /?

How To Ask Questions The Smart Way


Report •

#6
May 24, 2013 at 19:02:09
Sorry, I forgot about "spaces-in-filenames", just put quotes around them, like:
@echo off & setlocal
for /r %%a in (audit_*.*) do ren "%%~pnxa" "qrep_%%~nxa"

and as razor mentioned, "for /?" will deliver the goods pretty well. the tilde-enhancement stuff is on the last page. These allow batch to obtain or isolate various attributes of a file.


Report •

#7
May 29, 2013 at 16:26:21
Another question if you don't mind. Before I ran the above file I thought I'd modify it slightly to keep it to a specific folder before running it live and as you can see I only added an _ to the end. The problem is that when I ran it it ate into the characters in the file name after the "AUDIT_" so that the file name went from "AUDIT_123456.pdf" to "QREP_AUDIT_6.PDF". Does anybody know why this happens and how can I correct it?

@echo off & setlocal
For /r "D:\Scripts\Batch_Scripts\Tutorial_Files" %%a in (QREP_AUDIT*.pdf) do ren "%%a" "QREP_AUDIT_*.pdf"

end

Thanks.


Report •

#8
May 29, 2013 at 19:21:27
I think I see what's happening. The char.s are being overlayed onto the filename, not inserted into the filename. If you count the chars in the name, the total is the same (12) for both "before" and after. Since you know ahead of time that the first 10 chars will always be QREF_AUDIT, split the filename at that point and insert your other char(s), like:
@echo off & setlocal enabledelayedexpansion
For /r "D:\Scripts\Batch_Scripts\Tutorial_Files" %%a in (QREP_AUDIT*.pdf) do (
set n=%%~nxa
set n=!n:~0,10!_!n:~10!
echo ren "%%a" !n!
)
::=========== end
there is probably a better way, but this was my initial approach.

Report •

Ask Question