• Welcome to Tux Reports: Where Penguins Fly. We hope you find the topics varied, interesting, and worthy of your time. Please become a member and join in the discussions.

Cmd file looping question

J

John

Flightless Bird
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]

Flightless Bird
"John" <John@discussions.microsoft.com> wrote in message
news:667A5ADA-4B65-40A1-8386-2073B6C64F3C@microsoft.com...
> 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.
 
J

John

Flightless Bird
This is close! But I over simpliied the example. I really am dealing w/ full
path & file names that include spaces and the copy from and to names are
different. So I'm really needing to do as series like:
copy /y "C:/dir a\sub dir a\File 1.txt" "D:/dir x\sub dir xx\new name
1.txt"
copy /y "C:/dir b\sub dir b\File 2.txt" "D:/dir y\sub dir yy\new name
2.txt"
....
Your example has problems w/ spaces in the file names and assumes the
from/to names are the same.

How can we generalize this example to handle the additional requirements?
 
Top