1

I want to create a windows batch file (Win7) to achieve the following:

Copy source.doc to destination with destinationFilename.doc taken from a list in a text file (nameList.txt)

I have batch file that will make directories from nameList.txt but I can't figure out how to modify the batch file to make it copy source.doc in the required manner.

1 Answers1

0

Using xargs you can process a file nameList.txt which contains a newline separated list of target filenames like this:

cat nameList.txt | xargs -I "F" cp source.doc F

where -I "F" defines F as a placeholder to be used in command invocation of cp.

Jens Jensen
  • 1,038
  • 1
  • 10
  • 20
  • Oops, sorry: I should have specified I'm trying to do this on a Windows machine(win7) – user2331305 Apr 29 '13 at 08:59
  • Thanks for the suggestion, but it doesn't work for me under Windows ("cat" and "cp"look like Linux commands) – user2331305 Apr 29 '13 at 09:03
  • You might try Windows-ports of the tools, go for a full-blown scripting language, or have a look at this: http://stackoverflow.com/questions/206114/batch-files-how-to-read-a-file – Jens Jensen Apr 29 '13 at 11:50
  • OK, I'm getting there: I've got cat.exe, cp.exe and xargs.exe from sourceforge (thanks Elektro Hacker) and the -I switch doesn't work with xargs. However, -i does work but then xargs complains it "cannot fork". I've tried the obvious (using "f" in place of "F") and I've looked at the help that comes with xargs, but I'm still stumped. – user2331305 May 02 '13 at 08:07
  • Aha! I looked at the other question http://stackoverflow.com/questions/206114/batch-files-how-to-read-a-file and that method worked. From the command line: FOR /F %i IN (file.txt) DO copy source.doc %i.doc (in a batch file, the "%" character should be "%%"). – user2331305 May 02 '13 at 08:16