![]() Some files are in multiple SubDirectories and I have the file names in a text file (file1.pdf file2.pdf file3.pdf) I want to create the scenario Manuel was asking about: You have to set a temporary variable (“fil” in the above), since as far as I can tell string indexing doesn’t work with the %%i for loop variable. ![]() If the variable %i was your full UNC path, and you want to drop the first character (so you don’t have a double-slash in your path), you can specify %i:~1 - the :~1 says “start from the second character” (strings are zero-indexed, so the first character is at position 0, the second at position 1, etc.).įor /f “delims=” %%i in (filelist.txt) do ( set fil=%%iĮcho D|xcopy “%%i” “c:\temp\%fil:~1%” /i /z /y ) Remove the first slash in the UNC path in the destination parameter of the xcopy command.įortunately the command shell in Windows allows you to specify a substring of characters from a variable (called string indexing, see ). Remove any path prefix to the source parameter of the xcopy command.Ģ. ![]() If your filelist.txt has the full path to the files, you need to do two things:ġ. This will result in operations like the following:Ĭ:\windows\Microsoft.NET\Framework\v7\ngen.logĬ:\temp\Microsoft.NET\Framework\v7\ngen.log This will copy every *.log file from the Windows directory to your c:\temp directory. Xcopy c:\windows\*.log c:\temp /i /z /y /s 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. 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 two key changes are the question mark at the end of the first path in the xcopy command, and the /s switch.īy adding a question mark to the end of the first path, you allow xcopy to look for “all files that match the pattern”. The easiest way I can think of is by doing this:įor /f "delims=" %%i in (filelist.txt) do xcopy "c:\Temp\%%i?" "c:\Final" /i /z /y /s So it sounds like you want to copy only those files whose names are in the given file list. 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. To turn this off (have it prompt you), change /y to /y- in the batch file. Note: The batch file overwrites files in the destination automatically. Another trick in here is the output parser pipe, which allows us to automatically press the “D” key with each xcopy command. 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. Put the file list in the same directory as the batch file and name the file list filelist.txt. (I used c:\temp\ for testing.) Change \\server\share\folder to the root folder of the files to copy.įor /f "delims=" %%i in (filelist.txt) do echo D|xcopy "\\server\share\folder\%%i" "c:\temp\%%i" /i /z /y Change c:\temp\ to whatever path you want the files copied to. We’ll call the batch file xcopylist.bat its one line of content is below. 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. A simple file copy won’t work because there may be files in the source folders which should not be copied.Ĭlient\CD120\Samarai Legends\Drafts\drafts folder.txtĬlient\CD120\Samarai Legends\Inbox\Legends.docĬlient\CD120\Bushido Warriors\Inbox\Warrior Code.doc 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. In a nutshell, here is the high-level description of what the script must do: The path in the new location should begin with the part after “folder”… All are currently located in \\server\share\folder. … would want to copy all files on this list to another location (doesn’t really matter where for now).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |