A coworker asked me for a script. Here’s the request:
… would want to copy all files on this list [an attached text document] to another location (doesn’t really matter where for now). All are currently located in \\server\share\folder. The path in the new location should begin with the part after “folder”…
In a nutshell, here is the high-level description of what the script must do:
Given a text file which provides a list of files, copy the files from a fixed source to a fixed destination, recreating the directory trees on the destination. A simple file copy won’t work because there may be files in the source folders which should not be copied.
Sample content of the text file is:
client\CD120\Samarai Legends\Drafts\drafts folder.txt
client\CD120\Samarai Legends\Inbox\Legends.doc
client\CD120\Bushido Warriors\Inbox\Warrior Code.doc
The solution to this is to make a simple batch file that parses the content of the text file, generating the appropriate xcopy command to copy the file. We’ll call the batch file xcopylist.bat; its one line of content is below. Change c:\temp\ to whatever path you want the files copied to. (I used c:\temp\ for testing.) Change \\server\share\folder to the root folder of the files to copy.
for /f "delims=" %%i in (filelist.txt) do echo D|xcopy "\\server\share\folder\%%i" "c:\temp\%%i" /i /z /y
Put the file list in the same directory as the batch file and name the file list filelist.txt. Then run the batch file and viola! You’ve got to love the for command, which lets you (among other things) parse text files and use the line-by-line output. Another trick in here is the output parser pipe, which allows us to automatically press the “D” key with each xcopy command.
Note: The batch file overwrites files in the destination automatically. To turn this off (have it prompt you), change /y to /y- in the batch file. However, if you’re using Windows NT 4.0, just delete the /y switch altogether - it’s only supported in Windows XP and Windows 2000.
Popularity: 23% [?]
Stumble Upon
Comments 15
Thanks Brian for this nice solution. It saved a lot of time for us
Posted 21 Feb 2008 at 5:46 am ¶Hello,
I would like to implement your script to backup starred picasa photos, these are stored in a file named starlist.txt in the format:
C:\Users\Joe\Pictures\Play\Journeys\New Years 08\IMG_1682.JPG
C:\Users\Joe\Pictures\Play\Journeys\New Years 08\IMG_1683.JPG
etc
The problem being that your script uses the whole file path when duplicating the files, resulting in the following (unusable) address:
C:\temp\C:\Users\Joe\Pictures\Play\Journeys\New Years 08\IMG_1682.JPG
Is there a simple modification that can be made to the script which would remove the “C:\Users\Joe\Pictures” element from the created file path?
Many thanks in advance for your assistance.
Posted 30 May 2008 at 2:46 pm ¶The simples thing is to make a copy of starlist.txt, do a search/replace removing all text c:\users\joe\pictures, and running the script against that.
As far as I can remember (it’s been a while), there is no easy way to truncate or replace data in strings from within the Windows shell.
Posted 30 May 2008 at 5:41 pm ¶Many thanks for your fast response!
I modified starlist.txt as you suggested, so that each line reads as follows:
\Play\Journeys\Spain 05\Chris’s Photos\DSCN0319.JPG
etc
This duplicated the files successfully, but put each individual image into a folder with the image name, within the correct folder structure e.g:
D:\Starred Photos\Play\Journeys\Spain 05\Chris’s Photos\DSCN0319.JPG\DSCN0319.JPG
I am using the following command:
for /f “delims=” %%i in (starlist.txt) do echo D|xcopy “C:\Users\Joe\Pictures%%i” “D:\Starred Photos\%%i” /i /z /y
Do you have any suggestions on why the folder is being created?
Posted 31 May 2008 at 5:11 am ¶Only thing I can think to try is adding a backslash after the folder “Pictures” in the path “C:\Users\Joe\Pictures%%i” (that is, make it “C:\Users\Joe\Pictures\%%i”
Posted 05 Jun 2008 at 10:23 pm ¶Hi Brian, this script is very useful, i apreciate to much if you can help me to modify
a little this script.
I´ve a filename.txt file with the names of the files something like these
text.txt
text.txt
text.txt
etc…
But the really I need is copy the files with your relative path to another folder
thanks and regards
MM
Posted 22 Jul 2008 at 1:16 pm ¶Not 100% sure what you need… You mention a relative path — relative to what?
Something you can do to help explain may be to give an example of one line in the text file, and an example of how the script would use that line. For example, if one line was this:
test.txt
… and you wanted to generate this:
copy test.txt c:\files\test.txt
… the script would be:
for /f “delims=” %%i in (filelist.txt) do copy “%%i” “c:\files\%%i”
Give me an illustrative example and I’ll help if I can.
Posted 22 Jul 2008 at 4:04 pm ¶Hi Brian,
Supose that I got a file “filelist.txt” with this content
test.txt
test1.txt
test2.txt
….etc
but these files (test.txt, test1.txt, etc…) are in a differents folders, something like that
c:\Temp\test\test.txt
c:\Temp\test1\test1.txt
c:\Temp\test2\test2.txt
I need copy all of the contents in the “Temp” folder, to another folder “Final”, but including the same folder structure of the “Temp” folder, see below
c:\Final\ \\this folder are empty at first
This is that i will to result after execute the batch file
c:\Final\test\test.txt
c:\Final\test1\test1.txt
c:\Final\test2\test2.txt
But I only have the filelist.txt, this file dont have the relative paths of the files
Ps sorry for my enligish
Thanks and regards
MM
Posted 23 Jul 2008 at 10:27 am ¶So it sounds like you want to copy only those files whose names are in the given file list. The easiest way I can think of is by doing this:
for /f "delims=" %%i in (filelist.txt) do xcopy "c:\Temp\%%i?" "c:\Final" /i /z /y /sThe two key changes are the question mark at the end of the first path in the xcopy command, and the /s switch.
By adding a question mark to the end of the first path, you allow xcopy to look for “all files that match the pattern”. Of course, this means that test1.txt and test1.txtt would both match, but since it’s unlikely you have files with more than three characters in the extension, this should be fine.
The /s switch tells xcopy to search all subdirectories of the source directory for matching files, and copy them to the destination with the same folder hierarchy.
To test it out, run the following:
xcopy c:\windows\*.log c:\temp /i /z /y /sThis will copy every *.log file from the Windows directory to your c:\temp directory. This will result in operations like the following:
C:\windows\WindowsUpdate.logC:\windows\Debug\blastcln.log
C:\windows\Debug\UserMode\userenv.log
C:\windows\Logs\DirectX.log
C:\windows\Microsoft.NET\Framework\v2.0.50727\ngen.log
In the temp directory, you’d have this:
C:\temp\WindowsUpdate.logC:\temp\Debug\blastcln.log
C:\temp\Debug\UserMode\userenv.log
C:\temp\Logs\DirectX.log
C:\temp\Microsoft.NET\Framework\v2.0.50727\ngen.log
Viola!
Posted 23 Jul 2008 at 1:17 pm ¶Hi Brian, thanks for your help. The script its working ok…I´ve an error in the
sintaxis of the xcopy command…but now is everything OK
Regards
Posted 23 Jul 2008 at 4:38 pm ¶Hi Brian, a need your help one more time
Supose that I got a file “filelist.txt” with this content (names without extensions)
test
test1
test2
test3
test4
….etc
I have a folder with a lot of files named like this form (test.txt, test.xls, test.doc, etc…) see example below
c:\Temp\test.txt
\test.xls
\test.doc
\test1.txt
\test1.xls
\test1.doc
.
.
.
etc.
I need copy all of the contents in the “Temp” folder, to another folder “Final”, with subfolders named like
the name of files and including the files with same name inside of the several folder see below
c:\Final\ \\this folder are empty at first or don´t exist at first
This is that i will to result after execute the batch file
c:\Final\test\test.txt
\test.xls
\test.doc
c:\Final\test1\test1.txt
\test1.xls
\test1.doc
c:\Final\test2\test2.txt
\test2.xls
\test3.doc
Ps sorry for my enligish, I hope I had explained correctly
Thanks and regards
MM
Posted 02 Aug 2008 at 6:55 pm ¶Manuel,
I’d love to help out, but your requests are getting very specialized, and getting close to consulting services. I’ll try to point you in the right direction…
First, you’re going to need to a combination of a FOR loop to parse the filenames in the text file. Next, you need to create the directory, then copy files to it.
Try creating a batch file with this:
for /f "delims=" %%i in (filelist.txt) do (md c:\Final\%%i
xcopy c:\temp\%%i.* c:\Final\%%i /i /z /y /s
)
I didn’t test that but it looks right.
If you need further services, you can consider hiring me as a consultant.
Posted 04 Aug 2008 at 9:56 am ¶I appear to be having the same issue as Joe.
Before running this in my live environment, I decided to test it first.
My bat file states:
for /f “delims=” %%I in (test.txt) do echo D| xcopy “C:\Documents and Settings\e1000721\Desktop\New\%%I” “C:\Copy\%%I”
My test.txt file states
Level\test\File1.txt
Level\test\File2.txt
test\File3.txt
test\File4.txt
And it does copy over fine, only when it finishes, the file structure appears as
C:\Copy\Level\test\File1.txt\File2.txt
C:\Copy\Level\test\File2.txt\File2.txt
C:\Copy\test\File3.txt\File3.txt
C:\Copy\test\File4.txt\File4.txt
Any thoughts on how I can get it to not make the last subfolder the name of the file?
Thanks.
Posted 11 Nov 2008 at 7:29 pm ¶Try replacing xcopy with a regular copy command — it might do the trick. (For some reason xcopy is defaulting to behavior assuming that the destination is a directory, not a file. Usually this only happens when the /I switch is present.)
Posted 11 Nov 2008 at 10:13 pm ¶Tried copy and it just bombed out. I then put a pause at the end of the batch file so I could see exactly what it was doing. The issue with the xcopy wound up being that it was set for Directory instead of File so I changed D| to F| before the xcopy thus making the script:
for /f “delims=” %%I in (test.txt) do echo F| xcopy “C:\Documents and Settings\e1000721\Desktop\New\%%I” “C:\Copy\%%I”
Thanks again for the great help.
Posted 12 Nov 2008 at 11:23 am ¶Post a Comment