Wrote a vbscript to backup folders and files. Just thought I would share it. Cut and paste to notpad and save it as backup.vbs and edit the variables backUpDrive, backUpLocation and toBeBackedUpArray to what ever you want.
It will take a fair while the first time you run it especialy if you have a lot of files. After the first run it only replaces files that have been modified so will run quite quickly.
Use at your own risk. No resposability accepted if it goes tits up!
It will take a fair while the first time you run it especialy if you have a lot of files. After the first run it only replaces files that have been modified so will run quite quickly.
Use at your own risk. No resposability accepted if it goes tits up!
Code:
' VBScript that backs up files. Replaces files that have been modified.
Option Explicit
Dim backUpLocation
Dim backUpDrive
' Set the location of the backup device and folder
backUpDrive = "D:"
backUpLocation = "\backup"
Dim toBeBackedUpArray
Dim arraySize
Dim loopCounter
' Array of folders to be backed up
toBeBackedUpArray = Array("C:\Documents and Settings\Dave\My Documents", "C:\Documents and Settings\Laura\My Documents")
' Get the array size
arraySize = UBound(toBeBackedUpArray)
' Initialise the loop counter
loopCounter = 0
Dim oFSO
Dim file
Dim oFileA
Dim oFileB
Dim destinationFile
Dim originalFolder
Dim destinationFolder
Dim currentDirectoryForBackup
Dim date1, date2
' Create the file system object used to copy the files
Set oFSO = CreateObject("Scripting.FileSystemObject")
Dim objShell
Set objShell = CreateObject("Wscript.Shell")
Dim noOfFilesBackedUpNew
Dim noOfFilesUpdated
noOfFilesBackedUpNew = 0
noOfFilesUpdated = 0
' If the root folder does not exist then create it.
If Not oFSO.FolderExists(backUpLocation) Then
objShell.Run("cmd /c mkdir """ & backUpDrive & backUpLocation &""""), 0, TRUE
End if
'Loop through the locations to be backed up.
While loopCounter <= arraySize
currentDirectoryForBackup = toBeBackedUpArray(loopCounter)
destinationFolder = backUpDrive & backUpLocation & Mid(currentDirectoryForBackup, 3, len(currentDirectoryForBackup))
'WScript.Echo destinationFolder
' If the root folder does not exist then create it.
If Not oFSO.FolderExists(destinationFolder) Then
objShell.Run("cmd /c mkdir """ & destinationFolder &""""), 0, TRUE
End if
' Create/replace files in the root dir for backup
For Each file In oFSO.GetFolder(currentDirectoryForBackup).Files
CreateReplaceFiles(file)
Next
' Creates folders that don't already exist
For Each originalFolder In oFSO.GetFolder(currentDirectoryForBackup).SubFolders
CreateFolders(originalFolder)
Next
' Incrament the loop counter
loopCounter = loopCounter + 1
Wend
WScript.Echo "Done " & noOfFilesBackedUpNew & " new files backed up. " & noOfFilesUpdated & " modified files replaced."
'--------------------------------------------------------------------------------------------
' Recursive subroutine creates folders that don't already exist.
Sub CreateFolders(originalFolder)
destinationFolder = backUpDrive & backUpLocation & Mid(originalFolder, 3, len(originalFolder))
' If the folder does not exist then create it.
If Not oFSO.FolderExists(destinationFolder) Then
On Error Resume Next
oFSO.CreateFolder(destinationFolder)
If Err.Number <> 0 then
WScript.Echo"Error Backingup folder " & originalFolder
End If
On Error Goto 0
End if
' Create/replace files in each folder
For Each file In oFSO.GetFolder(originalFolder).Files
CreateReplaceFiles(file)
Next
' Recursive part of the subroutine to create subfolders.
For Each originalFolder In oFSO.GetFolder(originalFolder).SubFolders
CreateFolders(originalFolder)
Next
End Sub
'--------------------------------------------------------------------------------------------
' Subroutine to create or replace files
Sub CreateReplaceFiles(file)
destinationFile = backUpDrive & backUpLocation & Mid(file, 3, len(file))
'WScript.Echo destinationFile
' If the file does not exist then copy it.
If Not oFSO.FileExists(destinationFile) Then
On Error Resume Next
oFSO.CopyFile file, destinationFile
If Err.Number <> 0 then
WScript.Echo "Error Backing up file " & file
Else
noOfFilesBackedUpNew = noOfFilesBackedUpNew+1
End If
On Error Goto 0
Else
Set oFileA = oFSO.GetFile(file)
date1 = oFileA.DateLastModified
Set oFileB = oFSO.GetFile(destinationFile)
date2 = oFileB.DateLastModified
' If the file is newer then overwrite the backed up file
If DateDiff("s",date2,date1) > 0 then
On Error Resume Next
oFSO.CopyFile file, destinationFile, true
If Err.Number <> 0 then
WScript.Echo "Error Backing up file " & file
Else
noOfFilesUpdated = noOfFilesUpdated+1
End If
On Error Goto 0
End if
End if
End Sub
'--------------------------------------------------------------------------------------------
'CleanUp
Set backUpLocation = Nothing
Set backUpDrive = Nothing
Set toBeBackedUpArray = Nothing
Set arraySize = Nothing
Set loopCounter= Nothing
Set oFSO = Nothing
Set file = Nothing
Set oFileA = Nothing
Set oFileB = Nothing
Set destinationFile = Nothing
Set originalFolder = Nothing
Set destinationFolder = Nothing
Set date1 = Nothing
Set date2 = Nothing
Set objShell = Nothing
Set noOfFilesBackedUpNew = Nothing
Set noOfFilesUpdated = Nothing
Last edited: