I wrote this vbscript for a friend of mine to backup important directories to another hard drive. I figured someone else might enjoy it so here it is. The MyTimeStamp was written by Rubens Almeida and I tweaked it so it uses month, day, and 4 digit year instead of day, month, and 2 digit year.
Update1 The destination folder now contains the source folders. I.E. source=c:\test Dest=D:\backup, After backup D:\backup\test\
Update2 Add the time to the destination folder if the user wants it.
Code:
Option Explicit
' Change DestDrv to the drive letter of the external passport drive
' Add or subtract sourcefolders and change the sourcefolders(2) to the correct number
Const DestDrv = "C:\"
Const DateAndTime = False ' Change to True if you want the date and time in the destination folder
Dim SourceFolders(2)
SourceFolders(0) = "C:\test\test2"
SourceFolders(1) = "C:\test2"
SourceFolders(2) = "C:\test3\test4"
Dim fso
Dim DestDrvObj, DestDrvFreespc, DestDrvFreespcAfter
Dim DestDirectory, objFolder
Dim Logg, logName
Dim indx1
Set fso = CreateObject("Scripting.FileSystemObject")
Set DestDrvObj = fso.GetDrive(DestDrv)
DestDrvFreespc = DestDrvObj.FreeSpace
DestDirectory = DestDrv & MyTimeStamp(Date())
If fso.FolderExists(DestDirectory) Then
msgbox("The destination folder already exists.")
WScript.quit
Else
Set objFolder = fso.CreateFolder(DestDirectory)
logName = DestDirectory & "\log.txt"
End If
Set logg = fso.OpenTextFile(logName, 8, True)
logg.WriteLine "Backup started at " & Now() & vbNewLine
For indx1 = 0 to UBound(SourceFolders, 1)
copyme SourceFolders(indx1), DestDirectory
Next
logg.WriteLine "Backup completed at " & Now() & vbNewLine
DestDrvFreespcAfter = DestDrvObj.FreeSpace
logg.WriteLine "Output Drive Freespace: Before " & FormatNumber(DestDrvFreespc/1048576, 1) &_
" GB After: " & FormatNumber(DestDrvFreespcAfter/1048576, 1) & " GB"
Set logg = Nothing
Set objFolder = Nothing
Set DestDrvObj = Nothing
Set fso = Nothing
Wscript.quit
function MyTimeStamp(curTime)
dim myTime, myDay, myMonth, myYear
dim myHour, myMin
myTime = Now()
myDay = Datepart("d", myTime)
if Len(myDay) = 1 then myDay = "0" & myDay
myMonth = DatePart("m", myTime)
if Len(myMonth) = 1 then myMonth = "0" & myMonth
myYear = DatePart("yyyy", myTime)
'if Len(myYear) = 4 then myYear = Right(myYear,2)
if DateAndTime = True then
myHour = DatePart("h", myTime)
myMin = DatePart("n", myTime)
wscript.echo myMin
wscript.echo myTime
if Len(myHour) = 1 then myHour = "0" & myHour
if Len(myMin) = 1 then myMin = "0" & myMin
myTimeStamp = myMonth & myDay & myYear & "_" & myHour & "-" & myMin
else
myTimeStamp = myMonth & myDay & myYear
end if
myTimeStamp = Cstr(myTimeStamp)
End function
Sub copyme(Src, Dest)
Dim thisfolder, item, Spath, Dpath
Set thisfolder = fso.GetFolder(Src)
If Not fso.FolderExists(Dest & "\" & thisfolder.Name) then fso.CreateFolder(Dest & "\" & thisfolder.Name)
For each item in thisfolder.Files
Spath = fso.BuildPath(Src, item.name)
' On error resume next
fso.CopyFile Spath, Dest & "\" & thisfolder.Name & "\" & item.Name
If Err.Number <> 0 then Wscript.echo "Error: " & Err.Number & " on " & Spath
on Error Goto 0
Next
For each item in thisfolder.SubFolders
Dpath = fso.BuildPath(Dest, thisfolder.Name)
copyme item.Path, Dpath
Next
Set thisfolder=Nothing
End Sub