XCopy Code Snippets
Update: I was running the script below and found that the timer was not working. You can monitor the activity from Program Manager. I'll be reworking this code to run better.
Using XCopy in a script is really a time saver when you have everything setup. I've used Vic Laurie script as an easy method to copy things over to my thumb drive. It's a click, click yes, browse, browse, click copy. This is what it takes, clicks. No applications to install and best of all it works! I'll see if I can email Vic to see if he has newer scripts that can help people do simple things and give programmers some creative ideas. Other authors will be listed in their code snippets.
I'll be uploading these scripts just incase the formating is wrong which would drive you nuts debugging a working script. I'll also find my Copy to USB and Drive Folder version.
'--------------------------------------------------------- 'Written by Vic Laurie, January, 2006 'All rights reserved. Provided as is 'with no guarantees, express or implied 'User assumes all responsibility '--------------------------------------------------------- 'Description- Copies any new or updated files from user selected folder 'and its subfolders to user selected backup folder 'Uses Xcopy with switches /e /q /h /k /i /y /r /d /c Option Explicit Dim sFldrInput1, sFldrInput2, introMsg introMsg = msgBox("This application will help you copy changed files" & _ " from your Video and My Documents folder."& _ " " & vbCrLf & _ " If you have not created a backup copy this may take a" & _ " few minutes."& _ " " & vbCrLf & _ " A message will appear when copying is finished."& _ " " & vbCrLf & _ " If you have any questions call your IT Guy (000) 000-1411"& _ " " & vbCrLf & _ " You will see two screens popup."& _ " " & vbCrLf &_ " The first screen SELECT WHAT YOU WOULD LIKE TO COPY"& _ " The second screen SELECT WHERE you would like to copy your"&_ " files."& vbCrLf &_ " I would suggest your Z:\ drive.",vbOKCancel) If introMsg = vbCancel Then Wscript.Quit ChooseFolder sFldrInput1,"Select the source folder: " ChkForSystemFldr sFldrInput1 ChooseFolder sFldrInput2, "Select the backup folder: " ChkForSystemFldr sFldrInput2 CopyFolder sFldrInput1, sFldrInput2 Wscript.Quit sub ChooseFolder(sFldrChoice, sSelectionString) dim objShell, objFolder, objFolderItem, strPath, msgValue Const DESK_TOP = &H10& Const WINDOW_HANDLE = 0 Const OPTIONS = 0 sFldrChoice = "" Set objShell = CreateObject("Shell.Application") Set objFolder = objShell.Namespace(DESK_TOP) Set objFolderItem = objFolder.Self strPath = objFolderItem.Path Set objShell = CreateObject("Shell.Application") Set objFolder = objShell.BrowseForFolder _ (WINDOW_HANDLE, sSelectionString, OPTIONS, strPath) If objFolder Is Nothing Then Wscript.Quit End If Set objFolderItem = objFolder.Self sFldrChoice = objFolderItem.Path End sub sub CopyFolder (sSourceFldr,sBkupFldr) Const CopyX = "xcopy " Const sSwitches = " /e /q /h /k /i /y /r /d /c" Const sWildCard = "\*.*" dim sStatement dim objWshell Dim oIE, oIEDoc, sMsg sStatement= copyX & chr(34) & sSourceFldr& sWildcard & chr(34) & " " & chr(34) & sBkupFldr & chr(34) & sSwitches 'The next part is just to display a message while copying Dim strTimerSec strTimerSec = 0 set objWshell=Wscript.CreateObject("Wscript.Shell") Set oIE = Wscript.CreateObject("InternetExplorer.Application") oIE.Navigate "about:blank" do while oIE.busy strTimerSec = strTimerSec + 1 wscript.sleep 10 loop Set oIEDoc = oIE.Document oIE.AddressBar = False oIE.StatusBar = False oIE.ToolBar = False oIE.height=200 oIE.width=300 oIE.Resizable = False oIE.Visible = True sMsg= "
Files are being copied.
Large folders may take
several minutes or hours.
" sMsg = sMsg & "Loops so completed: "&strTimerSec oIEDoc.Body.Innerhtml= sMsg 'copy the files objWshell.Run sStatement,7,true Set oIEDoc = Nothing oIE.Quit Set oIE = Nothing set objWshell = Nothing msgBox "Copying job done" End sub 'This is necessary to remove trailing slash on drive letters Sub chkForDrv(sFldrChoice) Dim oRe, bMatch set oRe = New RegExp oRe.pattern = "[a-zA-Z]:\\$" bMatch= oRe.Test(sFldrChoice) If bMatch Then sFldrChoice= Left(sFldrChoice, 2) End sub Sub chkForSystemFldr(sFldrChoice) dim msgWarn, msgValue If Left(sFldrChoice, 1) = ":" then msgWarn = msgBox("You have selected a special system folder. Please select a different folder", vbRetryCancel+vbCritical) Select case msgWarn Case vbcancel Wscript.Quit Case VbRetry ChooseFolder sFldrChoice,"Select a folder: " If Left(SfldrChoice, 1) = ":" then Wscript.Quit msgValue = msgBox("You selected "& sFldrChoice, vbOKCancel) If msgValue = vbCancel Then Wscript.Quit If Len(sFldrChoice) = 3 then chkForDrv sFldrChoice End select Else msgValue = msgBox("You selected "& sFldrChoice, vbOKCancel) If msgValue = vbCancel Then Wscript.Quit End If If Len(sFldrChoice) = 3 then chkForDrv sFldrChoice End if end if End sub
I noticed that this code needed a little tweaking to run correctly as it once did.
Robocopy might be a better solution if you can't use the SyncToy 2.1 version or you have a login script that you want to run the sync .
Nice thing about robocopy is the UNC path and the permissions. I'm thinking of a script that would setup 2 clones on a domain network passing the permissions.
I'll get right on that one for you.
The link is to the syncToy which I'll see if I can create that scheduled task trigger on login to run.