You are not logged in.

#1 10 Mar 2015 18:39

MrChris
New Member
Registered: 10 Mar 2015
Posts: 3

COPY Command From Multiple File Paths.

Hi, I'm new to the forum and have limited experince over the last 10 years with using the Windows cmd line.

I'm batteling with my memory and have pretty much exhausted the knowledge within the office, so I'm hoping someone on this forum can help.

Essentially I have several long list of files that need to be copied from their respective server locations and placed into a common directory to apply a data conversion tool. If I had unlimited time I'd type out something like the below for every entry in every list but, given the each list contains around 500 file paths this isn't really an option.

COPY /V D:\FolderA\FolderB\File_To_Copy.dat D:\Destination_Folder\

I have all of the file lists in .csv or .txt formated one file path to each line. eg.
D:\FolderA\FolderB\File_To_Copy1.dat
D:\FolderC\FolderD\File_To_Copy2.dat
D:\FolderA\FolderF\FolderF\File_To_Copy3.dat
D:\FolderG\File_To_Copy4.dat

So ideally what I need is to do is replace the single file to copy in the above a example with the file list .txt file but, instead of copying the file read and execute the COPY command with it's contents line by line. Something like:

COPY /V /L D:\List_of_Files_To_Copy.txt D:\Destination_Folder\

Somehow about 6 years ago I managed to do this on a differnt data migration project and now frustatingly just can't work out how. I seem to have a recolection that mid command I had to drag and drop a .txt file containing all of the file paths into the COPY function but, all I seem to be able to manage now is either copying a single file at a time or copying the file that contains all of the file paths.

I've made the example simple but in reality the flies are scattered all over the server, some with long file paths and some at the drive root, with varying file extensions, some duplicate files and many that just need to be ignored. As such I really need a method that works from the data lists I have to ensure I'm only copying the correct files I need.

Any help on this matter would be greatly appreciated.

Offline

#2 10 Mar 2015 20:07

Shadow Thief
Member
Registered: 12 Jul 2012
Posts: 205

Re: COPY Command From Multiple File Paths.

You can iterate through each line in a file with a for loop.

for /F %%A in (D:\List_of_Files_to_Copy.txt) do copy /V /L %%A D:\Destination_Folder

Or if you wanted to keep the drag-and-drop functionality of your old script, you can take advantage of the fact that the first file you drag onto a script gets stored in the variable %1.

for /F %%A in (%1) do copy /V /L %%A D:\Destination_Folder

Offline

#3 11 Mar 2015 16:02

MrChris
New Member
Registered: 10 Mar 2015
Posts: 3

Re: COPY Command From Multiple File Paths.

Perfect answer, thank you very much.

I was trying to squeeze functions into the copy command, when that was really just the action to take after first processing the input.
Not sure it's quite the same as I did before but, being able to drop the list files directly onto the batch execuables with (%1) is even better than I could have hoped for.

3000 successfully copied files and counting... big_smile

Offline

#4 12 Mar 2015 12:45

Shadow Thief
Member
Registered: 12 Jul 2012
Posts: 205

Re: COPY Command From Multiple File Paths.

If you want to speed up the process further, you can take advantage of the fact that the %* variable contains all arguments passed into the script and that there is essentially no limit to how many parameters can be passed in.

for /F %%A in (%*) do (
    for /F %%B in (%%A) do copy /V /L %%B D:\Destination_Folder
)

And then drag every single file onto the script at once.

Offline

#5 12 Mar 2015 13:36

foxidrive
Member
Registered: 04 Apr 2013
Posts: 339

Re: COPY Command From Multiple File Paths.

Shadow Thief wrote:

essentially no limit

It's ~8 KB in modern windows.

With long paths and long filenames it will be chewed up pretty quick - if I followed the task.

Offline

#6 12 Mar 2015 14:44

MrChris
New Member
Registered: 10 Mar 2015
Posts: 3

Re: COPY Command From Multiple File Paths.

Thats a nice addition to know for the future, thanks.

I hit a bit of  unxpected behaviour with filepaths containing spaces as the for loop reads them, it reads up until the first sapce and then ignores the rest of the path. As such it errors file not found as the path is incomplete then moves on to the next row. Thinking I can just add " " around variable (as you would with a typed path) was pretty nieve as I ended up just copying the flie of filepaths again with

for /f %%A IN ("%1") do copy /v %%A D:\Destination_Folder

or

for /f "%%A" IN (%1) do copy /v "%%A" D:\Destination_Folder

that just errors.

Working though the FOR /F options info on the SS64 site, I always seemed to end up with one or other of the above outcomes.
I've been trying different combinations of delims, eol, tokens and usebackq, and can't seem to make the command ignore the default space delimiter even with delims^=^ and setting "eol= ". So I've come to the conclution that I can't use the filepaths file with spaces in it and only an enter between each line as I assume it reads this as a space as well.

That said, the only work around I've manage to sucessfully execute for spaces in filepaths is to format all of my filepaths (in Excel) as such:
"D:\FolderA\FolderB\File To Copy1.dat";
"D:\FolderC\FolderD\File To Copy2.dat";
"D:\FolderA\FolderF\FolderF\File To Copy3.dat";
"D:\FolderG\File To Copy4.dat";

This then works using a semicolon as the delimter (or anything other than space), something like:

for /f "delims=;" %%A IN (%1) do copy /v %%A D:\Destination_Folder

Also by aparetly disableing delims and eol with:

for /f delims^=^ eol^= %%A IN (%1) do copy /v %%A D:\Destination_Folder

I get a similar level of success but still need to manually format my filepaths to be within " " before starting.

Both are perfectly workable for me, but do require an extra formatting step in the process. Is it possible to use just enter as the eol and then disable space for delims or does the fact that space is the default mean it can't be used for eol? And can the " " quotations be applied within the .bat?

Many Thanks.

Offline

Board footer

Powered by