We help IT Professionals succeed at work.

Enumerate subfolders and run robocopy job for each subfolder

I am using Robocopy to sync data directories.  The total volume of data is around 100 GB and although the amount of data that changes daily and needs to be sync'd is small, it takes an exceptionally long time for a single Robocopy job to work it's way through all of the subdirectories and finish.  I have found that if I create separate robocopy jobs, one for each subdirectory off the main folder then I performance is greatly enhanced.  So what I would like to do is create a script that enumerates all the subfolders and then runs a robocopy job for each subfolder enumerated.  

I would like to modify this script (below).  The diference is that this script reads from a text file to start the jobs.  It works well until a folder is added I forget to update the text file.  Thanks for your help.


Option Explicit

Dim objShell, objFSO
Dim strOptions, strLog, strSource, strDestination, strFolder
Dim txtFile, strFileName, strPath

Set objShell =       CreateObject("WScript.Shell")      ' Instantiate the Windows Script Host
Set objFSO =       CreateObject("Scripting.FileSystemObject")      'Instatiate the File System Object (FSO)
Set txtFile =       objFSO.OpenTextFile("Robocopy_File.txt", 1)      'Open File with FSO


do until txtFile.AtEndOfStream  '      Loops through the file "Robocopy_File.txt" starting jobs for each line in the file.
        strFolder      = txtFile.ReadLine            'Open Robocopy_File.txt
        strSource      = "\\olyfile02\billimages$" & "\" & strFolder      'Copy source
        strDestination = "\\prodfile01\billimages$" & "\" & strFolder      'Copy Destination
        strOptions       = " /XF *.tif /S /maxage:1095 /R:5 /w:2 /NFL /NDL /TEE "      'Robocopy options.  See Below.
       strLog         = strFolder & ".log"      'Log file name.  Each job creates a unique log file.
      objShell.Run "Robocopy "& strSource & " " & strDestination & " " & strOptions & " /log:" & strLog
      WScript.Sleep 500
loop
txtFile.Close

Comment
Watch Question

You can use your script with dynamically updated Robocopy_File.txt. You can update this file every time before run robocopy script. For example:
dir /b /s /A:D %your_folder_path% >Robocopy_File.txt

Open in new window

Bill PrewTest your restores, not your backups...
Expert of the Year 2019
Top Expert 2016
Commented:
How about this.  Adjust the C:\Temp to be your base folder, then this will loop through each subfolder and spawn robocopy as before.

~bp
Option Explicit
 
Dim objShell, objFSO
Dim strOptions, strLog, strSource, strDestination, strFolder
Dim txtFile, strFileName, strPath
Dim objBaseFolder, objFolder
 
Set objShell = CreateObject("WScript.Shell")      ' Instantiate the Windows Script Host
Set objFSO = CreateObject("Scripting.FileSystemObject")      'Instatiate the File System Object (FSO)
 
Set objBaseFolder = objFSO.GetFolder("C:\Temp")
 
For Each objFolder In objBaseFolder.SubFolders
  strFolder = objFolder.Name
  strSource      = "\\olyfile02\billimages$" & "\" & strFolder      'Copy source
  strDestination = "\\prodfile01\billimages$" & "\" & strFolder      'Copy Destination
  strOptions       = " /XF *.tif /S /maxage:1095 /R:5 /w:2 /NFL /NDL /TEE "      'Robocopy options.  See Below.
  strLog         = strFolder & ".log"      'Log file name.  Each job creates a unique log file.
  objShell.Run "Robocopy "& strSource & " " & strDestination & " " & strOptions & " /log:" & strLog
  WScript.Sleep 500
Next

Open in new window

Author

Commented:
Thank you for your responses.  I like both of these solutions and can actually see using both in my environment.  Using the DIR command (Tamerlan666) and piping to a file is very nice because I can easily modify the command using wild cards to select only certain directories.  I also like Billprew's suggestion for it's simplicity and elegance.  I will try them both and let you know how it goes.

Thanks again!