|
|
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
|
|
0
|
|
|
|
Reply
|
Utf
|
4/27/2010 8:57:01 PM |
|
"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.
|
|
0
|
|
|
|
Reply
|
Pegasus
|
4/27/2010 9:40:22 PM
|
|
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?
|
|
0
|
|
|
|
Reply
|
Utf
|
4/28/2010 3:00:03 AM
|
|
|
2 Replies
401 Views
(page loaded in 0.083 seconds)
Similiar Articles: General T-SQL Question - microsoft.public.sqlserver.programming ...I have two tables that I want to combine. The two tables to NOT have a common field. Here is table one: Description ----- A B ... How to code a batch file for backup? - microsoft.public.windowsxp ...Does anyone have any suggestions on how to code a batch file for backup? ... for any suggestions My suggestion: Read the responses to your identical question ... Create Multiple Triggers from a Cmd File - microsoft.public ...Hi It's easy to loop through each file in a given ... Multiple Triggers from a Cmd File ... database.itags.org: Oracle question: Multiple CREATE TRIGGER's in SQL file ... cmd.exe shell - microsoft.public.windows.powershellCan't find a cmd.exe group so I'm hoping to get some help with advanced batch file features: setlocal ... I discovered that a for /f loop I had which ... Hide Command button based on a loop timer - microsoft.public ...Why does one have to click on the cmd ... Hide Command button based on a loop timer - microsoft.public ... A Question, Hiding ... Hide/Unhide File Folder ... spreadsheet ... Show status message of the script - microsoft.public.scripting ...... script: .....part of the some script in a loop ... A0 =A0 =A0 =A0 objUser.Put "scriptPath", "europa.cmd ... -- Crash "The real question is not whether machines think ... SQL command to GROUP BY buckets... - microsoft.public.access ...... some SQL-script by SqlCmd.exe . In Cmd.exe I ran this command ... Post Question ... I want to secure my Access MDE file so ... Looping through table to build sql case ... invoke command - microsoft.public.windows.powershellIt works on a cmd prompt, but not as invoke in PS. ... with invoke-expression in Powershell, it creates a file ... redirecting loop output - microsoft.public.windows ... Get a list of files on network drive - microsoft.public.access ...when I add the switch to write to a text file it takes ... Open a recordset outside the loop and then inside the ... database.itags.org: Microsoft Access question: Get a list ... Run SQL Script from Console application - microsoft.public.dotnet ...Loop through the array then executing each statement. something like... ... Data.SqlClient.SqlCommand cmd ... Cmd file looping question Windows 7 - Windows 7 Discussion List ...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 Answer : Cmd file looping question - GNT : your source for ...Cmd file looping question - answer - 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 ... For - Loop through command output - SS64... FOR command is the answer to innumerable questions ... set _ping_cmd=ping -n 5 127.0.0.1. FOR /f ... FOR /F - Loop through items in a text file SETLOCAL - Control the visibility ... CMD Scripts - Umachandar's HomepageIf there are any bugs in the script file, questions or ... 10) Another example of a FOR loop extension ... to "MUNGE.EXE". The "Global Replace.Cmd" script uses this file. CMD For Loop does not hold set /a value - Stack OverflowSee CMD /? for more help about this: /V:ON ... echo. set numLines=0 echo examining file '%~f0' echo. rem loop N times ... question feed 7/25/2012 9:06:29 AM
|
|
|
|
|
|
|
|
|