Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Batch script help for renaming a bunch of files 1

Status
Not open for further replies.

djpingpong

Programmer
Jul 6, 2004
70
Hello all,

I've been doing a certain task at work manually for a long time and it has reach the point where I would like to create a batch script so that I could run it in DOS (yes, i know it doesn't exist anymore.. but i still like to call it by that name). My work runs a WinXP Pro environment, not sure if that's relevant or not...

But here's my scenario...
I have a master file, "1.doc", and a list of usernames (ie: user.txt may contain 40 names). Basically, here's what I like the batch script to do:
1. loop through user.txt and fetch one username at a time
2. make a copy of 1.doc and paste it to the same folder
3. rename the new copy of 1.doc to "1_username.doc" (of course, the username should be dynamic..)

So if you willing to help, you'd understand how much manual work you will be saving me. Currently, I am making ~30 copies of the same file and then I manually rename the file to its respective username... it might be an easy task, but its tedious and takes me 30min each time, and I do this about 4-5 times per month

I hope there are still ol' skool coders out there who can still script DOS batches... i know this is a dying art
ANY HELP, would be greatly... greatly appreciated

cheers
 
Something like this should work, but I haven't tested it.
Code:
@echo off
cd "c:\your real path"
setLocal EnableDelayedExpansion
for /f %%a in (user.txt) do (
  set var=%%a
  )
copy 1.doc "1_!var!.doc"
echo done
pause
cls

If you need more info about these commands type: setLocal /?, for /? at the command prompt. There is also no error checking. Set the correct path.
 
Your bat file won't work smah because you are initaitng the copy outside of the loop, so basically you are only copying the last entry in the file.

something simpler like this works for me:




Code:
FOR /F %%i in (users.txt) do copy 1.doc 1_%%i.doc



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
That's why I set the delayedExpansion. I suspected that the copy would work inside the for loop, but didn't actually try it.
 
oh geniuses...
i knew the art of DOS haven't been lost to the world..

i'll have time to try it tomorrow at work.. so thanks for the quick responses... i'll post my result afterwards

cheerios
 
The point is your copy command is not inside the For loop.

Which means the for loop runs and once its done it moves on and executes the copy command with the last value your variable got during the loop.

I'm not familiar with the EnabledelayedExpansion what is it sdupposed to do?




----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
[embarrassed]vacunita, you're right, I did accidentally put the task outside the loop.[/embarrassed] I grabbed that from something else I did a while back and modified it without really thinking about it, and in addition, in this case delayed variable expansion is not needed.

What it does, when needed (not in this case) is to delay the expansion of the variable created in the loop. For fun, here's an example where it would be needed to get the results that you expect. Compare the output of this:
Code:
set filename=
for /f %%i in ('dir "%tmp%" /b') do (
set filename=%%i
echo now do something with %filename% ...
)
to this:
Code:
set filename=
setLocal EnableDelayedExpansion
for /f %%i in ('dir "%tmp%" /b') do (
set filename=%%i
echo now do something with !filename! ...
)
(both of which do their work inside the loop)

djpingpong, use vacunita's correct for loop
 
Interesting.

I'll play around with these.

Thanks for the examples smah.


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
It's actually a useless example, but I don't use it often enough to come up with anything better.
 
Hey guys...
the script sample you gave worked... but only have 1 little problem

so i didn't make any changes to the script:
Code:
@echo off
cd "c:\temp"

FOR /F %%i in (users.txt) do copy 1.doc 1_%%i.doc

echo done
pause
cls

However, in my users.txt each line contains the first and last name of a person... ie: bob smith
right now, the current script is copying the file as "1_bob.doc", but i need "1_bob smith.doc"

any suggestions?
 
You'll need to include additional tokes for the last name so:

Code:
FOR /F [red]"tokens=1,2"[/red] %%i in (users.txt) do copy 1.doc "1_%%i [red]%%j[/red].doc"          ?




----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
One of the standard variable delimiters is the space character. So when parsing the file users.txt, the space character signifies the end of the variable [!]i[/!] that is being collected. There are a couple of possibile solutions. My personal recommendation is to replace the space character with an underscore character, but you could keep the space if you choose.

replace space with underscore
Code:
@echo off
cd "c:\temp"
FOR /F %%i in (users.txt) do copy 1.doc 1_%%i_%%j.doc
echo done
pause
cls

keep space, possibility #1
Code:
@echo off
cd "c:\temp"
FOR /F %%i in (users.txt) do copy 1.doc "1_%%i %%j.doc"
echo done
pause
cls

keep space, possibility #2 (hint: for /? will help with what this does)
Code:
@echo off
cd "c:\temp"
FOR "delims=" /F %%i in (users.txt) do copy 1.doc "1_%%i.doc"
echo done
pause
cls
 
my untested examples were wong again - I forgot the tokens. Thanks vacunita, I'll be quiet now.
 
No worries. Happens to all of us.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
OMG... thank you guys...
i appreciate all your help ... this will TOTALLY save me a bunch of time for the future

really thank you..
much appreciated

here's my final script
Code:
@echo off
cd "H:\work\Projects\SAM\test"

FOR /F "tokens=1,2" %%i in (users.txt) do copy BES_Front.mdb "BES_Front_%%i %%j.mdb"

echo done
pause
cls
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top