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월 8일 수요일 오전 7: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일 목요일 오전 9: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일 금요일 오전 1:11Brian Borg 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     
    C:\> For /f %%f in (list.txt) do copy /y c:\source\%%f d:\dest
  • 2009년 7월 3일 금요일 오전 2: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일 월요일 오후 5:49
    • 답변으로 표시됨Mike2010 2009년 7월 6일 월요일 오후 4:52
    •  
  • 2009년 7월 3일 금요일 오후 7: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일 월요일 오후 5: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일 화요일 오후 4:13AbqBill중재자사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     
    Hi,

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

    Bill
  • 2009년 7월 7일 화요일 오후 5:15Mike2010 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     
    Hi Bill,

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

    Thanks

    Mike
  • 2009년 7월 7일 화요일 오후 6: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일 화요일 오후 8: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일 화요일 오후 8: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일 화요일 오후 9: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일 수요일 오전 5:06Mike2010 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     
    What else is there?
  • 2009년 7월 8일 수요일 오전 7: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일 목요일 오전 9:45
    •  
  • 2009년 7월 8일 수요일 오후 2:12AbqBill중재자사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     
    Hi Mike,

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

    Bill
  • 2009년 7월 9일 목요일 오전 9: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