Skip to content

Instantly share code, notes, and snippets.

@russellpierce
Last active January 22, 2021 14:22
Show Gist options
  • Save russellpierce/6132223 to your computer and use it in GitHub Desktop.
Save russellpierce/6132223 to your computer and use it in GitHub Desktop.
Visual basic script to clone directories using robocopy as a back-end; code adapted from code originally developed by Rob van der Woude; Modification requires that grep be available at the launch location (earlier version does not: https://gist.github.com/drknexus/6132223/693faf00fa6a7a228a176c52446dc600f44f0aa0)
Option Explicit
Dim SD
Dim DD
Dim CMD, CMDRUN, CMDRUNPersist
Dim oShell, oShellOut
Dim Resp
Set oShell = WScript.CreateObject("WScript.Shell")
SD = BrowseFolder( "%userprofile%\documents", True, "Select source directory")
DD = BrowseFolder( "%userprofile%\documents", True, "Select destination directory")
CMD="cmd /K robocopy " & chr(34) & SD & chr(34) &" " & chr(34) & DD & chr(34) & " /L /MIR /FFT /ETA | grep -F *EXTRA more"
CMDRUN="cmd /C robocopy " & chr(34) & SD & chr(34) &" " & chr(34) & DD & chr(34) & " /MIR /FFT /ETA"
CMDRUNPersist="cmd /C robocopy " & chr(34) & SD & chr(34) &" " & chr(34) & DD & chr(34) & " /MIR /FFT /ETA /MOT:60"
Resp = MsgBox("About to display the proposed deletions. Files in the source will overwrite files in the destination. Close the console window when done reviewing. Note Files marked *EXTRA File will be deleted from the destination directory.",4096)
oShellOut = oShell.Run(CMD,3,true)
Resp = MsgBox("Do proposed action?",4)
If Resp = 6 THEN oShellOut = oShell.Run(CMDRun,3,true)
Resp = MsgBox("Keep doing every 60 minutes? Note: You will have to leave the resulting console window open.",4)
If Resp = 6 THEN oShellOut = oShell.Run(CMDRUNPersist,3,true)
MsgBox("Script complete")
Function BrowseFolder( myStartLocation, blnSimpleDialog, Desc )
' This function generates a Browse Folder dialog
' and returns the selected folder as a string.
'
' Arguments:
' myStartLocation [string] start folder for dialog, or "My Computer", or
' empty string to open in "Desktop\My Documents"
' blnSimpleDialog [boolean] if False, an additional text field will be
' displayed where the folder can be selected
' by typing the fully qualified path
'
' Returns: [string] the fully qualified path to the selected folder
'
' Based on the Hey Scripting Guys article
' "How Can I Show Users a Dialog Box That Only Lets Them Select Folders?"
' http://www.microsoft.com/technet/scriptcenter/resources/qanda/jun05/hey0617.mspx
'
' Function written by Rob van der Woude
' http://www.robvanderwoude.com
Const MY_COMPUTER = &H11&
Const WINDOW_HANDLE = 0 ' Must ALWAYS be 0
Dim numOptions, objFolder, objFolderItem
Dim objPath, objShell, strPath, strPrompt
' Set the options for the dialog window
strPrompt = Desc
If blnSimpleDialog = True Then
numOptions = 0 ' Simple dialog
Else
numOptions = &H10& ' Additional text field to type folder path
End If
' Create a Windows Shell object
Set objShell = CreateObject( "Shell.Application" )
' If specified, convert "My Computer" to a valid
' path for the Windows Shell's BrowseFolder method
If UCase( myStartLocation ) = "MY COMPUTER" Then
Set objFolder = objShell.Namespace( MY_COMPUTER )
Set objFolderItem = objFolder.Self
strPath = objFolderItem.Path
Else
strPath = myStartLocation
End If
Set objFolder = objShell.BrowseForFolder( WINDOW_HANDLE, strPrompt, _
numOptions, strPath )
' Quit if no folder was selected
If objFolder Is Nothing Then
BrowseFolder = ""
Exit Function
End If
' Retrieve the path of the selected folder
Set objFolderItem = objFolder.Self
objPath = objFolderItem.Path
' Return the path of the selected folder
BrowseFolder = objPath
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment