A Windows XP help forum. PCbanter

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.

Go Back   Home » PCbanter forum » Microsoft Windows XP » General XP issues or comments
Site Map Home Register Authors List Search Today's Posts Mark Forums Read Web Partners

Cmd file looping question



 
 
Thread Tools Display Modes
  #1  
Old April 27th 10, 09:57 PM posted to microsoft.public.windowsxp.general
John
external usenet poster
 
Posts: 816
Default 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  
Old April 27th 10, 10:40 PM posted to microsoft.public.windowsxp.general
Pegasus [MVP]
external usenet poster
 
Posts: 2,361
Default 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  
Old April 28th 10, 04:00 AM posted to microsoft.public.windowsxp.general
John
external usenet poster
 
Posts: 816
Default 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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off






All times are GMT +1. The time now is 10:39 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 PCbanter.
The comments are property of their posters.