Risorse per professionisti IT > Home page del forum > The Official Scripting Guys Forum! > Get RoboCopy to read list of files to copy from a text file
Formula una domandaFormula una domanda
 

Con rispostaGet RoboCopy to read list of files to copy from a text file

Risposte

  • mercoledì 8 luglio 2009 7.43perhof Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     Con risposta
    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.
    • Contrassegnato come rispostaMike2010 giovedì 9 luglio 2009 9.45
    •  

Tutte le risposte

  • giovedì 2 luglio 2009 22.33AbqBillModeratoreMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
    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
  • venerdì 3 luglio 2009 1.11Brian Borg Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
    C:\> For /f %%f in (list.txt) do copy /y c:\source\%%f d:\dest
  • venerdì 3 luglio 2009 2.29Salvador Manaois IIIModeratoreMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     Contiene codice
    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
    • Contrassegno come risposta annullatoMike2010 lunedì 6 luglio 2009 17.49
    • Contrassegnato come rispostaMike2010 lunedì 6 luglio 2009 16.52
    •  
  • venerdì 3 luglio 2009 19.48AbqBillModeratoreMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     Contiene codice
    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
  • lunedì 6 luglio 2009 17.51Mike2010 Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
    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
  • martedì 7 luglio 2009 16.13AbqBillModeratoreMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
    Hi,

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

    Bill
  • martedì 7 luglio 2009 17.15Mike2010 Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
    Hi Bill,

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

    Thanks

    Mike
  • martedì 7 luglio 2009 18.50AbqBillModeratoreMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
    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
  • martedì 7 luglio 2009 20.09Mike2010 Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
    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
  • martedì 7 luglio 2009 20.16perhof Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
    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.
  • martedì 7 luglio 2009 21.37AbqBillModeratoreMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
    Hi Mike,

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

    Bill
  • mercoledì 8 luglio 2009 5.06Mike2010 Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
    What else is there?
  • mercoledì 8 luglio 2009 7.43perhof Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     Con risposta
    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.
    • Contrassegnato come rispostaMike2010 giovedì 9 luglio 2009 9.45
    •  
  • mercoledì 8 luglio 2009 14.12AbqBillModeratoreMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
    Hi Mike,

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

    Bill
  • giovedì 9 luglio 2009 9.45Mike2010 Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
    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
  • giovedì 9 luglio 2009 23.00Brian Borg Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
    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.
  • martedì 14 luglio 2009 0.02Hari Kodungallur Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
    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
  • martedì 14 luglio 2009 0.31AbqBillModeratoreMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
    Hi Hari,

    Yes, please start a new thread.

    Thanks,

    Bill
  • martedì 14 luglio 2009 0.53Hari Kodungallur Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
    Hello Bill,

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

    Thank you,
    -Hari