Resources for IT Professionals >
포럼 홈
>
The Official Scripting Guys Forum!
>
Get RoboCopy to read list of files to copy from a text file
Get RoboCopy to read list of files to copy from a text file
- Is it possible to get Robocopy to read a list of files that are to be copied from a text file or simillar?
Many Thanks
답변
- Robocopy is great for replicating a whole structure from one place to another but if you want to call it a thousand times (one time for every file in a list) you should consider using something more lightweight like the regular copy command a built in fuction of the scripting language you're using.
Look at this thread for some examples:
http://social.technet.microsoft.com/Forums/en-US/ITCG/thread/59eed295-a18f-4c78-9b87-cf211c2e58b2
You can modify the script to do objFSO.CopyFolder instead of objFSO.CopyFile or ultimately determine if the line in the textfile is a file or a folder by using objFSO.FileExists(file) or objFSO.FolderExists(file) checks before using copyfile or copyfolder.- 답변으로 표시됨Mike2010 2009년 7월 9일 목요일 오전 9:45
모든 응답
- Hi,
I just looked at robocopy's syntax, and it doesn't appear to support it directly, but it does look like you can specify multiple file specifications after naming the source and destination directories; e.g.
robocopy c:\source d:\dest file1 file2 file3
..and so forth. If this list of file names gets very long, you may run up against command-line length limits. Alternatively, you can use a script to read a list of files and call robocopy multiple times. For example, in PowerShell, you could write:
gc list.txt | % { robocopy c:\source d:\dest $_ }
This code gets the content ('gc') of the file list.txt, and for each ('%') line, it executes the robocopy command ('$_' represents the current line).
HTH,
Bill - C:\> For /f %%f in (list.txt) do copy /y c:\source\%%f d:\dest
- Here's a robocopy one-liner adapted from bnborg's answer above:
for /f %%f in (list.txt) do robocopy c:\source d:\dest %%f
Here's a VBS that copies files in a single robocopy call (provided the resulting strFiles does not fill up the CLI buffer):
Alternatively, you can do a robocopy call within the For-Next loop to copy each file listed in list.txt.On Error Resume Next inputFile = ".\list.txt" Const conForReading = 1 Set objFSO = Wscript.CreateObject("Scripting.FileSystemObject")<br/>Set oShell=WScript.CreateObject("WScript.Shell") <br/><br/><br/>Set oInputFile = objFSO.OpenTextFile(inputFile, conForReading) Do Until oInputFile.AtEndOfStream arrstrFiles = oInputFile.ReadAll Loop arrFiles = Split(arrstrFiles, vbCrLf) strFiles = "" For i=0 to Ubound(arrFiles) strFiles = strFiles & " " & arrFiles(i) Next oshell.Run "robocopy c:\source d:\dest" & strFiles
Regards,
Salvador Manaois III
MCITP | Enterprise & Server Administrator
MCSE MCSA MCTS(x5) CIWA C|EH
My Blog: Bytes and Badz
My Shots: View My PhotoStream - For completeness, here's a JScript version:
var fso = new ActiveXObject("Scripting.FileSystemObject"); var wshShell = new ActiveXObject("WScript.Shell"); var ts = fso.OpenTextFile("List.txt", 1); while (! ts.AtEndOfStream) { var file = ts.ReadLine(); if (file.length > 0) wshShell.Run("robocopy c:\\source d:\\dest " + file); } ts.Close();
HTH,
Bill - Hi,
I need it to copy specified folders as well, i tried modifying your code with the /R, but couldn't get it to work.
How would i add the ability to specify Folders as well?
Many Thanks - Hi,
Can you be more specific about what exactly you want to accomplish?
Bill - Hi Bill,
The above code is only copying files, it doesn't seem to copy any folders across?
Thanks
Mike - Hi Mike,
Try adding the '/s' (copy subdirectories, but not empty ones) or the '/e' (copy subdirectories, including empty ones) parameter to robocopy's command line. (See 'robocopy /?' at a command prompt for more information.)
HTH,
Bill - Hi Bill,
I only want the folders that are named in the list.txt file to be copied across though. Adding a switch to the end copy's everything.
Mike - Is there a particular reason you're totally into Robocopy on this one?
Since it doesn't support file lists itself, maybe it's not the tool you should use?
I doubt that calling Robocopy separately for every single file and folder on your list will be that efficient. - Hi Mike,
I'm afraid I don't understand what you want to do, then. Can you give us a specific example?
Bill - What else is there?
- Robocopy is great for replicating a whole structure from one place to another but if you want to call it a thousand times (one time for every file in a list) you should consider using something more lightweight like the regular copy command a built in fuction of the scripting language you're using.
Look at this thread for some examples:
http://social.technet.microsoft.com/Forums/en-US/ITCG/thread/59eed295-a18f-4c78-9b87-cf211c2e58b2
You can modify the script to do objFSO.CopyFolder instead of objFSO.CopyFile or ultimately determine if the line in the textfile is a file or a folder by using objFSO.FileExists(file) or objFSO.FolderExists(file) checks before using copyfile or copyfolder.- 답변으로 표시됨Mike2010 2009년 7월 9일 목요일 오전 9:45
- Hi Mike,
Are you saying that you want to replicate a folder structure, but not copy any files?
Bill - Hi,
Sorry for not being clear, what i need is the list.txt to have folder names as well as file names that need to be copied across. The script from the above thread looks like it will crack it.
Many Thanks to everyone who helped out on this.
Mike Hi,
Sorry for not being clear, what i need is the list.txt to have folder names as well as file names that need to be copied across. The script from the above thread looks like it will crack it.
Many Thanks to everyone who helped out on this.
Mike
So would a variation on my oneliner:
> For /f %%f in (list.txt) do xcopy "c:\source\%%f" d:\dest [switches]
The quotes would only be necessary if any of the file or folder names include spaces.
Typical switches:
/C Continues copying even if errors occur.
/I If destination does not exist and copying more than one file,
assumes that destination must be a directory.
/H Copies hidden and system files also.
/R Overwrites read-only files.
/Y Suppresses prompting to confirm you want to overwrite an
existing destination file.- Hello,
I need to do something that is almost similar to what is being discussed in this thread. So I am posting the question here (Apologies if I should have started another thread).
Details of my requirement:
- I already use robocopy to sync a large number of binary files. I use robocopy obviously because I want to copy only the files that are changed since the last copy. (something like rsync)
- Almost always I can run the robocopy on the top level directory. But once in a while (very infrequently), I need to copy a few files (~100 files) from 3-4 directories. I do not want to copy anything else. Again, not all files within these 3-4 directories have changed. But I know that the files that have changed are within these directories. So again using robocopy will be ideal because it will copy only the changed files.
From what I see so far, there is no way to provide just these directory names to robocopy and copy the appropriate files. I will have to loop through these directories and then robocopy individual files. Is that correct?
thanks very much,
-Hari - Hi Hari,
Yes, please start a new thread.
Thanks,
Bill - Hello Bill,
OK. Done. http://social.technet.microsoft.com/Forums/en-US/ITCG/thread/07a82e5e-aa3b-40cf-8202-0433dc467564
Thank you,
-Hari

