Cmd file looping question

J

John

How do I construct a FOR loop or other batch command loop that will copy a
list of files File1, File2,...,Filen from C: to D:. I know something like
the code below will do it, but this is a lot of redundant code. I'd like to
be able to just add the file name to a list at the start of the batch file
and not have to mess w/ the rest of the batch file code.

set a1=File1
set a2=File2
....
set an=Filen
copy C:\%a1% d:\%a1%
copy C:\%a2% d:\%a2%
....
copy C:\%an% d:\%an%

I appreciate your help, -John
 
P

Pegasus [MVP]

John said:
How do I construct a FOR loop or other batch command loop that will copy a
list of files File1, File2,...,Filen from C: to D:. I know something like
the code below will do it, but this is a lot of redundant code. I'd like
to
be able to just add the file name to a list at the start of the batch file
and not have to mess w/ the rest of the batch file code.

set a1=File1
set a2=File2
...
set an=Filen
copy C:\%a1% d:\%a1%
copy C:\%a2% d:\%a2%
...
copy C:\%an% d:\%an%

I appreciate your help, -John

You could do something like this:
[01] @echo off
[02] goto Start
[03] ***FileList***
[04] File1.txt
[05] File 2.txt
[06] Last File.txt
[07] ***FileList***
[08] :Start
[09] for /F "skip=3 delims=" %%a in (d:\temp\test.bat) do (
[10] if /i %%a==***FileList*** goto :eof
[11] echo copy /y "c:\%%a" d:\
[12] )
Note:
- Put all file names between the ***FileList*** strings as shown. There is
no limit on their number.
- Do not surround your file names with double quotes.
- Line 9 must reference the correct path & name of your batch file.
- The string ***FileList*** occurs three times. It MUST be the same each
time.
- Remove the word "echo" in Line 11 to activate the batch file.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top