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

  • 2009年7月2日 下午 08:50Mike2010 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    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

解答

  • 2009年7月8日 上午 07:43perhof 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     已答覆
    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日 上午 09:45
    •  

所有回覆

  • 2009年7月2日 下午 10:33AbqBill版主使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    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
  • 2009年7月3日 上午 01:11Brian Borg 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    C:\> For /f %%f in (list.txt) do copy /y c:\source\%%f d:\dest
  • 2009年7月3日 上午 02:29Salvador Manaois III版主使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     包含代碼
    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):

    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
    
    Alternatively, you can do a robocopy call within the For-Next loop to copy each file listed in list.txt.

    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
    • 已取消標示為解答Mike2010 2009年7月6日 下午 05:49
    • 已標示為解答Mike2010 2009年7月6日 下午 04:52
    •  
  • 2009年7月3日 下午 07:48AbqBill版主使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     包含代碼
    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
  • 2009年7月6日 下午 05:51Mike2010 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    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
  • 2009年7月7日 下午 04:13AbqBill版主使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    Hi,

    Can you be more specific about what exactly you want to accomplish?

    Bill
  • 2009年7月7日 下午 05:15Mike2010 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    Hi Bill,

    The above code is only copying files, it doesn't seem to copy any folders across?

    Thanks

    Mike
  • 2009年7月7日 下午 06:50AbqBill版主使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    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
  • 2009年7月7日 下午 08:09Mike2010 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    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
  • 2009年7月7日 下午 08:16perhof 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    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.
  • 2009年7月7日 下午 09:37AbqBill版主使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    Hi Mike,

    I'm afraid I don't understand what you want to do, then. Can you give us a specific example?

    Bill
  • 2009年7月8日 上午 05:06Mike2010 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    What else is there?
  • 2009年7月8日 上午 07:43perhof 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     已答覆
    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日 上午 09:45
    •  
  • 2009年7月8日 下午 02:12AbqBill版主使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    Hi Mike,

    Are you saying that you want to replicate a folder structure, but not copy any files?

    Bill
  • 2009年7月9日 上午 09:45Mike2010 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    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
  • 2009年7月9日 下午 11:00Brian Borg 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    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.
  • 2009年7月14日 上午 12:02Hari Kodungallur 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    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
  • 2009年7月14日 上午 12:31AbqBill版主使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    Hi Hari,

    Yes, please start a new thread.

    Thanks,

    Bill
  • 2009年7月14日 上午 12:53Hari Kodungallur 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    Hello Bill,

    OK. Done. http://social.technet.microsoft.com/Forums/en-US/ITCG/thread/07a82e5e-aa3b-40cf-8202-0433dc467564

    Thank you,
    -Hari