title: VBS script to copy files and folders across network
body: <p>I needed to copy all of my photos from my old laptop to my new laptop. This is a quick and dirty script that I put together (based on other scripts on this site) to copy files from one network location to another. I wanted the process to be able to recover in case of a network copy error because the total time to copy all of my photos was 40 hours. </p>
  
  <p>sourceRoot and targetRoot is the beginning part of the file path to replace between locations. lastFileLog is a file used to keep track of the last file that was copied. This is needed to recover from a partial copy. Windows seems to allocate the full file size even when the file fails to copy. So I just keep track of the last file to copy it again on failure. objStartFolder is the starting path on the source network location. </p>
  
  <pre><code>'initialize paths
  objStartFolder = "\\owner-pc\d\pics"
  lastFileLog = "c:\Files\misc\archive.log"
  sourceRoot = "\\owner-pc\d"
  targetRoot = "c:\Files"
  
  
  Set objFSO = CreateObject("Scripting.FileSystemObject")
  
  'read log
  Set objFile = objFSO.OpenTextFile(lastFileLog)
  Do Until objFile.AtEndOfStream
      replacefile= objFile.ReadLine
      Wscript.Echo "This file will be replaced: " &amp; replacefile
  Loop
  objFile.Close
  
  'copy files
  Set objFolder = objFSO.GetFolder(objStartFolder)
  ShowSubfolders objFSO.GetFolder(objStartFolder)
  
  'clear log
  Set objFileLog = objFSO.CreateTextFile(lastFileLog,True)
  objFileLog.Write ""
  objFileLog.Close
  
  Sub ShowSubFolders(Folder)
      For Each Subfolder in Folder.SubFolders
  
          Wscript.Echo Subfolder.Path
  
          if not(objFSO.FolderExists(replace(Subfolder.Path,sourceRoot,targetRoot))) then
            objFSO.CreateFolder(replace(Subfolder.Path,sourceRoot,targetRoot))
          end if
  
          Set objFolder = objFSO.GetFolder(Subfolder.Path)
          Set colFiles = objFolder.Files
          For Each objFile in colFiles
  
              if not(objFSO.FileExists(replace(Subfolder.Path &amp; "\" &amp; objFile.Name,sourceRoot,targetRoot))) then 
                Wscript.Echo Subfolder.Path &amp; "\" &amp; objFile.Name
  
                Set objFileLog = objFSO.CreateTextFile(lastFileLog,True)
                objFileLog.Write Subfolder.Path &amp; "\" &amp; objFile.Name
                objFileLog.Close
  
                objFSO.CopyFile Subfolder.Path &amp; "\" &amp; objFile.Name, replace(Subfolder.Path &amp; "\" &amp; objFile.Name,sourceRoot,targetRoot)
  
              elseif replacefile = Subfolder.Path &amp; "\" &amp; objFile.Name then
                Wscript.Echo "Replacing ... " &amp; Subfolder.Path &amp; "\" &amp; objFile.Name  
                objFSO.CopyFile Subfolder.Path &amp; "\" &amp; objFile.Name, replace(Subfolder.Path &amp; "\" &amp; objFile.Name,sourceRoot,targetRoot),true            
              else
                Wscript.Echo "Skip ... " &amp; Subfolder.Path &amp; "\" &amp; objFile.Name
              end if
          Next
          ShowSubFolders Subfolder
      Next
  end sub
  </code></pre>