More Backing up via VBScript
October 14th, 2004A few days late, and perhaps not in it’s most efficient form (well not perhaps . . .) Here is the script that copies a directory from one place to another. Slight enhancements enable it to keep the previous two backups and to keep a relatively well formed log detailing start and finish. Enjoy!
I use it to copy my ‘My Documents’ folder to a networked drive. I make no guarantees that it will work for you, in fact, I can almost guarantee that it will often fail, since it does so on a relatively regular basis for me, most likely because my wireless connection which it uses to do the copying is a bit spotty on service at times.
‘A quick script by Elliot Anders to backup a folder to another drive.
‘This script will keep 2 copies of the backed up folder.
‘It deletes the old copy, renames the more recent copy as the old one and copies
‘the current source folder to become the new backup.
‘”'’Configure the folders here:
sourceFolder = “C:\Documents and Settings\USERNAME\My Documents”
previousBackup = “K:\Previous My Documents”
currentBackup = “K:\Current My Documents”
logFile = “K:\backupTheStore.log” ‘This should probably be located on
the drive with the backups but not in the backup folder
‘”'’Do not make changes below this line”””’
Dim FSO, logFileStream, tempFileStream
Dim strSrcDir
Set logFSO = CreateObject(”Scripting.FileSystemObject”)
Set tempFSO = CreateObject(”Scripting.FileSystemObject”)
Set backupFSO = CreateObject(”Scripting.FileSystemObject”)
‘”'’Make a log file if this is the first time
if logFSO.FileExists(logFile) = false Then
logFSO.CreateTextFile logFile, True
Set initialize = logFSO.openTextFile(logFile, 2)
firstText = “Log Created at ” & Now
initialize.writeLine firstText
initialize.close
End If
‘”'’Tell the log that we are starting
beginningText = “Backup of ” & sourceFolder & ” began at: ” & Now
‘”’Error here
tmp = “C:\temp\tmp.txt”
if tempFSO.FileExists(tmp) = false Then
tempFSO.CreateTextFile tmp, True
End if
Set tempFileStream = tempFSO.OpenTextFile( tmp, 2)
tempFileStream.WriteLine beginningText
tempFileStream.close
Dim tmp, logFile
‘Create FileSystemObject
Set appendFSO = CreateObject(”Scripting.FileSystemObject”)
‘Open file for appending, don’t create
Set ftop = appendFSO.OpenTextFile( tmp, 8, false)
‘Open for reading
Set fbot = appendFSO.OpenTextFile( logFile, 1, true)
‘Append the contents of second file to first
ftop.Write fbot.ReadAll
fbot.close
ftop.close
appendFSO.DeleteFile(logFile)
appendFSO.CopyFile tmp, logFile
‘”'’Delete the old backup
If backupFSO.FolderExists(previousBackup) Then
backupFSO.DeleteFolder previousBackup, true
End If
‘”'’Move the new to be the new old
If backupFSO.FolderExists(currentBackup) Then
backupFSO.MoveFolder currentBackup, previousBackup
Set f = backupFSO.CreateFolder(currentBackup)
End If
‘”'’Copy the source folder to the backup location
backupFSO.CopyFolder sourceFolder, currentBackup
‘”'’Tell the log we are done
endText = “Backup of ” & sourceFolder & ” was Successfully Completed at: ” & Now
Set tempFileStream = tempFSO.OpenTextFile( tmp, 2)
tempFileStream.WriteLine endText
tempFileStream.close
‘Open file for appending, don’t create
Set ftop = appendFSO.OpenTextFile( tmp, 8, false)
‘Open for reading
Set fbot = appendFSO.OpenTextFile( logFile, 1, true)
‘Append the contents of second file to first
ftop.Write fbot.ReadAll
fbot.close
ftop.close
appendFSO.DeleteFile(logFile)
appendFSO.MoveFile tmp, logFile