If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. |
|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
Cmd file looping question
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 |
Ads |
#2
|
|||
|
|||
Cmd file looping question
"John" wrote in message ... 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. |
#3
|
|||
|
|||
Cmd file looping question
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? |
Thread Tools | |
Display Modes | |
|
|