You are not logged in.

#1 15 Oct 2014 12:56

jamiefinney
New Member
Registered: 15 Oct 2014
Posts: 4

Newbie Question

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 smile

Offline

#2 15 Oct 2014 13:00

jamiefinney
New Member
Registered: 15 Oct 2014
Posts: 4

Re: Newbie Question

this is run using windows and saved as a .bat

Offline

#3 15 Oct 2014 17:02

DigitalSnow
Member
From: United States
Registered: 27 Dec 2012
Posts: 24

Re: Newbie Question

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

#4 15 Oct 2014 17:10

jamiefinney
New Member
Registered: 15 Oct 2014
Posts: 4

Re: Newbie Question

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

#5 15 Oct 2014 17:12

DigitalSnow
Member
From: United States
Registered: 27 Dec 2012
Posts: 24

Re: Newbie Question

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

#6 15 Oct 2014 17:21

jamiefinney
New Member
Registered: 15 Oct 2014
Posts: 4

Re: Newbie Question

thats amazing thank you so much that works perfectly now.

i should have spotted that missing quote myself smile

Offline

Board footer

Powered by