poly4life Posted March 24, 2011 Share Posted March 24, 2011 (edited) Hello,As the Topic Title says, I want to copy multiple files to multiple directories, all in one line. The files doesn't necessarily have the same extensions and the directories are not necessarily similar. Example: I want to copy Test.txt, Test.html, Test.doc, Test.xls, and AnotherTest.txt to folders C:\Root\Documents, C:\Root\Folder and C:\Root\MoreDocuments.I don't think there's a way to do this with a straight copy or xcopy command, correct? I mean, if I was using some form of wildcard for the file or folders, then maybe I might have something (i.e. copy *.txt C:\Root\Folder and/or an Exclusions list). If I'm copying a file to multiple folders, or multiple files to a folder, or -- in my case -- multiple files to multiple folders, I probably have to use a FOR loop, correct? The goal is to get it all in one line. I thought, perhaps, I can use nested FOR loops to accomplish this, but I'm getting an error of "The syntax of the command is incorrect."Here's the code(I am executing this from a batch file, NOT the command line prompt):FOR %%X in (Test.txt Test.html Test.doc Test.xls AnotherTest.txt) DO (FOR %%G in (Documents Folder MoreDocuments) DO COPY %%X %%G)Why is this not working? I would appreciate any help or improvements. Thank you.Update: Somewhat strange -- I had to move the opening parenthesis to the first line, probably because of a carriage return. Now the nested for loop works.Still, I would be interested if there are other methods, better methods, perhaps one that just involves copy or xcopy.Thank you. Edited March 24, 2011 by poly4life Link to comment Share on other sites More sharing options...
Yzöwl Posted March 24, 2011 Share Posted March 24, 2011 I'm interested to know why you need this as a single line.You said you were using a batch file, since that batch is doing the work and you are not typing multiple lines on each run I see no benefit in condensing the number of lines. Link to comment Share on other sites More sharing options...
Flip1001 Posted March 24, 2011 Share Posted March 24, 2011 What do you think about this?@ECHO OFFFOR %%X IN (Test.txt Test.html Test.doc Test.xls AnotherTest.txt) DO (Call :CopyFiles %%X)GOTO :END:CopyFilesFOR %%G IN (Documents Folder MoreDocuments) DO ( ECHO Copying %1 to %%G COPY /Y "%1" "%%G"> NUL)GOTO :EOF:ENDECHO.ECHO DonePAUSE > NUL Link to comment Share on other sites More sharing options...
poly4life Posted March 24, 2011 Author Share Posted March 24, 2011 (edited) I'm interested to know why you need this as a single line.You said you were using a batch file, since that batch is doing the work and you are not typing multiple lines on each run I see no benefit in condensing the number of lines.Because there may be a better way to do something, I ask the question. this is not a forum to rant, but memory is a luxury and code, in general, is not as tight as it was when memory was scarce. I'm not an expert, but I always want to learn and I'm willing to abandon my method if there is a better way.Thank you. Edited March 24, 2011 by poly4life Link to comment Share on other sites More sharing options...
poly4life Posted March 24, 2011 Author Share Posted March 24, 2011 What do you think about this?@ECHO OFFFOR %%X IN (Test.txt Test.html Test.doc Test.xls AnotherTest.txt) DO (Call :CopyFiles %%X)GOTO :END:CopyFilesFOR %%G IN (Documents Folder MoreDocuments) DO ( ECHO Copying %1 to %%G COPY /Y "%1" "%%G"> NUL)GOTO :EOF:ENDECHO.ECHO DonePAUSE > NULWonderful. This will work. Thank you. Link to comment Share on other sites More sharing options...
jaclaz Posted March 24, 2011 Share Posted March 24, 2011 (edited) Just for the record, you don't need /Y when using copy from within a batch:http://ss64.com/nt/copy.htmlPrompt to overwrite destination fileNT 4 will overwrite destination files without any prompt, Windows 2000 and above will prompt unless the COPY command is being executed from within a batch script.jaclaz Edited March 24, 2011 by jaclaz Link to comment Share on other sites More sharing options...
Yzöwl Posted March 24, 2011 Share Posted March 24, 2011 <snip />I want to copy multiple files to multiple directories, all in one line.<snip />I would appreciate any help or improvements. Thank you.<snip />I would be interested if there are other methods, better methods, perhaps one that just involves copy or xcopy.I'm interested to know why you need this as a single line.You said you were using a batch file, since that batch is doing the work and you are not typing multiple lines on each run I see no benefit in condensing the number of lines.Because there may be a better way to do something, I ask the question. this is not a forum to rantI do hope that the remark you've made about a rant was not directed towards me for providing you with information which may have been of interest.'Better' or 'tighter' code in a batch file may be faster running, less memory intensive, improved syntax etc. but less lines or more specifically one it will rarely be.I do apologise for not knowing what this forum is for, especially embarrassing because I oversee it. Link to comment Share on other sites More sharing options...
Recommended Posts