Jump to content

batch: trying to copy multiple files to multiple directories in one li


Recommended Posts

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 by poly4life
Link to comment
Share on other sites


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

What do you think about this?


@ECHO OFF

FOR %%X IN (Test.txt Test.html Test.doc Test.xls AnotherTest.txt) DO (Call :CopyFiles %%X)
GOTO :END

:CopyFiles
FOR %%G IN (Documents Folder MoreDocuments) DO (
ECHO Copying %1 to %%G
COPY /Y "%1" "%%G"> NUL
)
GOTO :EOF

:END
ECHO.
ECHO Done
PAUSE > NUL

Link to comment
Share on other sites

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 by poly4life
Link to comment
Share on other sites

What do you think about this?


@ECHO OFF

FOR %%X IN (Test.txt Test.html Test.doc Test.xls AnotherTest.txt) DO (Call :CopyFiles %%X)
GOTO :END

:CopyFiles
FOR %%G IN (Documents Folder MoreDocuments) DO (
ECHO Copying %1 to %%G
COPY /Y "%1" "%%G"> NUL
)
GOTO :EOF

:END
ECHO.
ECHO Done
PAUSE > NUL

Wonderful. This will work. Thank you.

Link to comment
Share on other sites

Just for the record, you don't need /Y when using copy from within a batch:

http://ss64.com/nt/copy.html

Prompt to overwrite destination file

NT 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 by jaclaz
Link to comment
Share on other sites

<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 rant

I 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. :whistle:

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...