Ressourcen für IT-Professionals > Forenhomepage > The Official Scripting Guys Forum! > Get RoboCopy to read list of files to copy from a text file
Stellen Sie eine FrageStellen Sie eine Frage
 

BeantwortetGet RoboCopy to read list of files to copy from a text file

Antworten

  • Mittwoch, 8. Juli 2009 07:43perhof TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     Beantwortet
    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.
    • Als Antwort markiertMike2010 Donnerstag, 9. Juli 2009 09:45
    •  

Alle Antworten

  • Donnerstag, 2. Juli 2009 22:33AbqBillModeratorTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     
    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
  • Freitag, 3. Juli 2009 01:11Brian Borg TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     
    C:\> For /f %%f in (list.txt) do copy /y c:\source\%%f d:\dest
  • Freitag, 3. Juli 2009 02:29Salvador Manaois IIIModeratorTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     Enthält Code
    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
    • Tag als Antwort aufgehobenMike2010 Montag, 6. Juli 2009 17:49
    • Als Antwort markiertMike2010 Montag, 6. Juli 2009 16:52
    •  
  • Freitag, 3. Juli 2009 19:48AbqBillModeratorTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     Enthält Code
    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
  • Montag, 6. Juli 2009 17:51Mike2010 TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     
    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
  • Dienstag, 7. Juli 2009 16:13AbqBillModeratorTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     
    Hi,

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

    Bill
  • Dienstag, 7. Juli 2009 17:15Mike2010 TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     
    Hi Bill,

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

    Thanks

    Mike
  • Dienstag, 7. Juli 2009 18:50AbqBillModeratorTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     
    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
  • Dienstag, 7. Juli 2009 20:09Mike2010 TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     
    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
  • Dienstag, 7. Juli 2009 20:16perhof TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     
    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.
  • Dienstag, 7. Juli 2009 21:37AbqBillModeratorTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     
    Hi Mike,

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

    Bill
  • Mittwoch, 8. Juli 2009 05:06Mike2010 TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     
    What else is there?
  • Mittwoch, 8. Juli 2009 07:43perhof TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     Beantwortet
    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.
    • Als Antwort markiertMike2010 Donnerstag, 9. Juli 2009 09:45
    •  
  • Mittwoch, 8. Juli 2009 14:12AbqBillModeratorTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     
    Hi Mike,

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

    Bill
  • Donnerstag, 9. Juli 2009 09:45Mike2010 TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     
    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
  • Donnerstag, 9. Juli 2009 23:00Brian Borg TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     
    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.
  • Dienstag, 14. Juli 2009 00:02Hari Kodungallur TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     
    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
  • Dienstag, 14. Juli 2009 00:31AbqBillModeratorTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     
    Hi Hari,

    Yes, please start a new thread.

    Thanks,

    Bill
  • Dienstag, 14. Juli 2009 00:53Hari Kodungallur TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     
    Hello Bill,

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

    Thank you,
    -Hari