PC Review


Reply
Thread Tools Rate Thread

Cmd file looping question

 
 
John
Guest
Posts: n/a
 
      27th Apr 2010
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
 
Reply With Quote
 
 
 
 
Pegasus [MVP]
Guest
Posts: n/a
 
      27th Apr 2010


"John" <(E-Mail Removed)> wrote in message
news:667A5ADA-4B65-40A1-8386-(E-Mail Removed)...
> 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.

 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Looping Question trekgoes2malaysia@hotmail.com Microsoft Access Form Coding 1 19th Jan 2008 12:07 PM
Looping Question =?Utf-8?B?TVdT?= Microsoft Excel Programming 3 29th Nov 2005 08:35 PM
looping question Gary Keramidas Microsoft Excel Programming 1 31st May 2005 02:58 PM
Looping Sound file with Looping slide show? =?Utf-8?B?a29rYW5lZWdvbGQ=?= Microsoft Powerpoint 11 19th Jan 2005 06:14 PM
VBA Looping Question JimFor Microsoft Excel Misc 9 17th Nov 2004 02:40 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:45 PM.