You are not logged in.
Pages: 1
I have very little experience coding so i was hoping someone could give me a hand with part of a script i'm writing. just found this and adapted it from another post i saw online.
i have a folder of files that i want to add a prefix of "rdcomp_" to
FOR /r "FOLDER_NAME" %%a in (*) DO REN "%%~a" "rdcomp_%%~nxa"
this script works really well if i have less than 10 files for example (all the files get renamed then the script closes)
rdcomp_FILE_1
rdcomp_FILE_2
rdcomp_FILE_3...
however where it falls over is when i use the same script on a folder with >1000 files and the script seems to get stuck in a loop and keeps running over and over again until i manually close it. so i end up with something like
rdcomp_rdcomp_rdcomp_rdcomp_FILE_1
any ideas on what i could do to fix this? seems odd that the same script should work so differently for a few files and many.
thanks in advance
Offline
this is run using windows and saved as a .bat
Offline
This behavior is due to a race condition in how the for command reads a directory of files. The for command parses a directory of files one by one and if a file is modified during that process (renamed) the loop will process that "new" file again. Use the for /f and dir commands to counteract this behavior. The dir command generates a static list of files and then the for loop processes that list.
for /f "delims=" %%A in ('dir /a-d /b /s "FOLDER_NAME\*"') do ren "%%~A" "rdcomp_%%~nxA"
Edit: Added closing single quotation mark '
Last edited by DigitalSnow (15 Oct 2014 17:13)
Offline
thanks for your reply i suspected it might be something like that.
i've tried your suggestion and it doesn't work the cmd window closes immediately so there must be some kind of error any idea?
for /f "delims=" %%A in ('dir /a-d /b /s "K:\ICMLiveTesting\TEST\Holding_2\*") do ren "%%~A" "rdcomp_%%~nxa"
Offline
Yep, I forgot the closing single quotation ' mark in my answer.
for /f "delims=" %%A in ('dir /a-d /b /s "FOLDER_NAME\*"') do ren "%%~A" "rdcomp_%%~nxA"
Offline
thats amazing thank you so much that works perfectly now.
i should have spotted that missing quote myself
Offline
Pages: 1