You are on page 1of 8

Dim Dim Dim Dim Dim

oArgs CmdLine, TempArg bRemoveFolders bRemoveFiles SleepTime

Dim SrcFolder Dim arrSrcFolders() Dim s Dim DstFolder Dim arrDstFolders() Dim d Dim Dim Dim Dim Dim Dim arrCopyFrom() arrCopyTo() arrSizes() fMax TotalSize oExplorer bRemoveFolders = False bRemoveFiles = False SleepTime = 10 ' default to leaving display onscreen for 10 seconds Set oArgs = WScript.Arguments For s = 0 To oArgs.Count - 1 CmdLine = CmdLine & oArgs(s) & " " TempArg = Ucase(Left(oArgs(s),3)) Select Case TempArg Case "/?" ShowSyntax() Wscript.Quit Case "/S:" CurSwitch = "S" SrcFolder = Trim(Mid(oArgs(s),4)) Case "/D:" CurSwitch = "D" DstFolder = Trim(Mid(oArgs(s),4)) Case "/RD" CurSwitch = "RD" bRemoveFolders = True Case "/RF" CurSwitch = "RF" bRemoveFiles = True Case "/T:" CurSwitch = "T" SleepTime = Cint(Mid(oArgs(s),4)) If SleepTime < 0 Then SleepTime = 0 End If Case Else If Left(oArgs(s),1) <> "/" Then If SrcFolder = "" Then SrcFolder = oArgs(s) ElseIf DstFolder = "" Then DstFolder = oArgs(s) Else Wscript.Echo "Unknown parameter on command line... " & oArgs( s)

ShowSyntax() Wscript.Quit End If End If End Select Next If SrcFolder = "" or DstFolder = "" Then Wscript.Echo "You must enter a 'SourcePath' and 'DestinationPath' when sta rting this script." ShowSyntax() Wscript.Quit End If If Right(SrcFolder,1) <> "\" Then SrcFolder = SrcFolder & "\" End If If Right(DstFolder,1) <> "\" Then DstFolder = DstFolder & "\" End If If InStr(LCase(SrcFolder),LCase(DstFolder)) > 0 _ Or InStr(LCase(DstFolder),LCase(SrcFolder)) > 0 Then Wscript.Echo "The 'SourcePath' and 'DestinationPath' cannot overlap!" Wscript.Quit End If TotalSize = 0 Set FSO = CreateObject("Scripting.FileSystemObject") Set oExplorer = CreateObject("InternetExplorer.Application") oExplorer.Navigate2 oExplorer.Width oExplorer.Height oExplorer.Top oExplorer.Left oExplorer.Toolbar oExplorer.Menubar oExplorer.Statusbar oExplorer.Visible "about:blank" = 550 = 300 = 300 = 300 = false = false = false = True "<font color=green>" "<MARQUEE WIDTH=100% BEHAVIOR=ALTERNATE>File Backup "<br>Beginning backup...<br>" = "Please be patient.... "

oExplorer.Document.Write oExplorer.Document.Write in Progress</MARQUEE><br>" oExplorer.Document.Write oExplorer.Document.Title GetSrcInfo GetDstInfo CreateMissingFolders If bRemoveFolders Then DeleteExtraFolders End If If bRemoveFiles Then DeleteExtraFiles End If FindFilesNeedingBackup

CopySourceToDest oExplorer.document.write "<font color=red>" oExplorer.Document.Write " Finished!<br>" If SleepTime > 0 Then Wscript.Sleep (SleepTime * 1000) End If QuitScript() Sub GetSrcInfo() oExplorer.Document.Write "Anaylzing source directories...<br>" '-- get information about source directories If FSO.FolderExists(SrcFolder) Then ReDim arrSrcFolders(50) s = 0 arrSrcFolders(0) = Left(SrcFolder, Len(SrcFolder) - 1) GetSrcFolders FSO.GetFolder(SrcFolder) ReDim Preserve arrSrcFolders(s) Else Wscript.Echo "Source folder not found... aborting." QuitScript() End If End Sub Sub GetSrcFolders(Folder) For Each Subfolder in Folder.SubFolders s = s + 1 If s > Ubound(arrSrcFolders) Then ReDim Preserve arrSrcFolders(Ubound(arrSrcFolders) + 50) End If arrSrcFolders(s) = SubFolder.Path GetSrcFolders Subfolder Next End Sub Sub GetDstInfo() oExplorer.Document.Write "Anaylzing backup directories...<br>" '-- get information about destination directories If Not FSO.FolderExists(DstFolder) Then CreateMultiLevelFolder DstFolder End If ReDim arrDstFolders(50) d = 0 arrDstFolders(0) = Left(DstFolder, Len(DstFolder) - 1) GetDstFolders FSO.GetFolder(DstFolder) ReDim Preserve arrDstFolders(d) End Sub Sub Dim Dim Dim Dim CreateMultiLevelFolder(FolderPath) TempPath Path arrNodes n TempPath = FolderPath Path = "" ' See if input is in UNC format... If Instr(TempPath,"\\") > 0 Then TempPath = Mid(TempPath,3) n = Instr(TempPath,"\")

Path = "\\" & Left(TempPath,n) TempPath = Mid(TempPath,n+1) End If arrNodes = Split(TempPath,"\") Path = Path & arrNodes(0) For n = 1 to UBound(arrNodes) Path = Path & "\" & arrNodes(n) If Not FSO.FolderExists(Path) Then FSO.CreateFolder(Path) End If Next End Sub Sub GetDstFolders(Folder) For Each Subfolder in Folder.SubFolders d = d + 1 If d > Ubound(arrDstFolders) Then ReDim Preserve arrDstFolders(Ubound(arrDstFolders) + 50) End If arrDstFolders(d) = SubFolder.Path GetDstFolders Subfolder Next End Sub Sub CreateMissingFolders() Dim LenOfSrcBaseDir Dim strFolder oExplorer.Document.Write "Creating any missing backup directories...<br>" LenOfSrcBaseDir = Len(SrcFolder) + 1 For f = 0 to s strFolder = DstFolder & Mid(arrSrcFolders(f), LenOfSrcBaseDir) If Not FSO.FolderExists(strFolder) Then FSO.CreateFolder(strFolder) End IF Next End Sub Sub DeleteExtraFolders() Dim LenOfDstBaseDir Dim strFolder oExplorer.Document.Write "Removing any extra backup directories...<br>" LenOfDstBaseDir = Len(DstFolder) + 1 For f = 0 to d strFolder = SrcFolder & Mid(arrDstFolders(f), LenOfDstBaseDir) If Not FSO.FolderExists(strFolder) Then FSO.DeleteFolder(arrDstFolders(f)) End IF Next End Sub Sub DeleteExtraFiles() Dim LenOfSrcBaseDir Dim LenOfDstBaseDir Dim strDstFolder

Dim Dim Dim Dim

strDstFile strSrcFile oFldr fc

oExplorer.Document.Write "Removing any extra backup files...<br>" LenOfSrcBaseDir = Len(SrcFolder) + 1 LenOfDstBaseDir = Len(DstFolder) + 1 For f = 0 to s strDstFolder = DstFolder & Mid(arrSrcFolders(f), LenOfSrcBaseDir) set oFldr = FSO.GetFolder(strDstFolder) set fc = oFldr.Files For Each oFile in fc strDstFile = strDstFolder & IIf(f = 0, "", "\") & oFile.Name strSrcFile = SrcFolder & Mid(strDstFile, LenOfDstBaseDir) If Not FSO.FileExists(strSrcFile) Then FSO.DeleteFile(strDstFile) End If Next Next Set fc = Nothing Set oFldr = Nothing End Sub Sub FindFilesNeedingBackup() Dim LenOfSrcBaseDir Dim strFrom Dim strTo oExplorer.Document.Write "Determining which files to backup...<br>" ReDim arrCopyFrom(50) ReDim arrCopyTo(50) ReDim arrSizes(50) fMax = -1 LenOfSrcBaseDir = Len(SrcFolder) + 1 arrSrcFolders(0) = SrcFolder arrDstFolders(0) = DstFolder For f = 0 to s set oFldr = FSO.GetFolder(arrSrcFolders(f)) set fc = oFldr.Files For Each oFile in fc strFrom = arrSrcFolders(f) & "\" & oFile.Name strTo = DstFolder & Mid(strFrom, LenOfSrcBaseDir) If Not FSO.FileExists(strTo) Then AddToList strFrom, strTo, oFile.Size Else Set oDstFile = FSO.GetFile(strTo) If FileIsNewer(oFile, oDstFile) Then AddToList strFrom, strTo, oFile.Size End If End If Next Next Set oDstFile = Nothing Set fc = Nothing Set oFldr = Nothing End Sub

Function FileIsNewer(Source, Dest) FileIsNewer = False If Source.DateLastModified > Dest.DateLastModified Then FileIsNewer = True End If End Function Sub AddToList(strFrom, strTo, FileSize) Dim fCur fCur = Ubound(arrCopyFrom) fMax = fMax + 1 if fMax > fCur Then ReDim Preserve arrCopyFrom(fCur + 50) ReDim Preserve arrCopyTo(fCur + 50) ReDim Preserve arrSizes(fCur + 50) End If arrCopyFrom(fMax) = strFrom arrCopyTo(fMax) = strTo arrSizes(fMax) = FileSize TotalSize = TotalSize + FileSize End Sub Sub CopySourceToDest() Dim PrvPercent Dim CurPercent Dim IncPercent Dim TotalCopied PrvPercent = 0 TotalCopied = 0 If fMax > -1 Then oExplorer.Document.Write "Backing up " & fMax + 1 & " files...<br>" oExplorer.document.write "<font color=red>" For f = 0 to fMax FSO.CopyFile arrCopyFrom(f), arrCopyTo(f) TotalCopied = TotalCopied + arrSizes(f) CurPercent = Round(100 * TotalCopied / TotalSize) IncPercent = CurPercent - PrvPercent PrvPercent = CurPercent If IncPercent > 0 Then For n = 0 to IncPercent oExplorer.Document.Write " " wscript.sleep 10 Next End If Next Else oExplorer.Document.Write "No files need to be backed up...<br>" End If End Sub Function IIf( expr, truepart, falsepart ) IIf = falsepart

If expr Then IIf = truepart End Function Sub QuitScript() oExplorer.Quit Set oExplorer = Nothing Set FSO = Nothing Wscript.Quit End Sub Sub ShowSyntax() Wscript.Echo "SyncBack.VBS /S:<source path> /D:<destination path> [/RD] [/RF] [/T:<num>] " Wscript.Echo " " Wscript.Echo " All switches are case insensitive as well as 'order insensiti ve'. Directory " Wscript.Echo " names with embedded spaces are supported either with or witho ut surrounding " Wscript.Echo " quotes (single or double), however, if they contain multiple adjacent spaces " Wscript.Echo " they must be quoted. Spaces after the ':' are ignored. " Wscript.Echo " " Wscript.Echo " /RD specifies to remove extraneous directories (folders) from the " Wscript.Echo " destination path if found. " Wscript.Echo " " Wscript.Echo " /RF specifies to remove extraneous files from the destination path if found. " Wscript.Echo " " Wscript.Echo " /T:<num> Optional switch to specify number of seconds to leav e final " Wscript.Echo " screen display visible. Defaults to 10 seconds if no t specified. " Wscript.Echo " " Wscript.Echo " The /S: and /D: switches are optional. When not used, the fi rst parameter " Wscript.Echo " on the command line not preceded by '/' will be taken as the source path and " Wscript.Echo " the second parameter not preceded by '/' will be taken as the destination " Wscript.Echo " path. " Wscript.Echo " " Wscript.Echo " The use of the '-' character in lieu of '/' is not supported. " Wscript.Echo " " '''''''''''' "0...0....1....1....2....2....3....3....4....4....5....5....6... .6....7....7...7" '''''''''''' "1...5....0....5....0....5....0....5....0....5....0....5....0... .5....0....5...9"

End Sub