Wednesday, October 29, 2008

Back up files that have been modified today, and tar (winrar, zip) them!


' This script would backup all folders (including their subfolders) and files
' that are under the path specified in the objStartFolder variable.
'
' Only those files that have been modified today would be backed up.
'
' Note: if you don't want the files in .zip (or .rar) to be full path as where
' the files were original stored (using relative path in .zip file), make sure you put this script in the same
' directory of the files you want to back up. Otherwise, you would need to
' modify this script to suit your need.
'
' You can run this script in more than three ways:
' Way 1: cmd > cscript.exe script_name.vbs
' Way 2: double click on the script_name.vbs
' Way 3: run the script in scheduled tasks.

Dim MyParentPath
Dim MySubFolderFilePath

MySubFolderFilePath = ""

Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "C:\Documents and Settings\Danny\Desktop\tmp"

' Specify the full path where the .zip will be stored, including .zip file extension and .zip file name.
ZipFileExtension = ".rar" ' can be either .zip or .rar
ZipFileName = "C:\DB_Backup_" & dateFormatLeadingZero( date() ) & ZipFileExtension

Set objFolder = objFSO.GetFolder(objStartFolder)
MyParentPath = objFolder.Path

'Wscript.Echo MyParentPath & "___"

Set colFiles = objFolder.Files
For Each objFile in colFiles
' Wscript.Echo """" & objFolder.Path & objFile.Name & """"
' Wscript.Echo """" & objFolder.Name & "\" & objFile.Name & """"
MySubFolderFilePath = MySubFolderFilePath & """" & objFile.Name & """" & " "
Next

ShowSubfolders objFSO.GetFolder(objStartFolder)

Wscript.Echo MySubFolderFilePath

Set wshShell = WScript.CreateObject ("WSCript.shell")
wshshell.run """C:\Program Files\WinRAR\WinRAR.exe"" a " & ZipFileName & " " & MySubFolderFilePath,0,True
set wshshell = Nothing

Sub ShowSubFolders(Folder)
For Each Subfolder in Folder.SubFolders
' Wscript.Echo Subfolder.Path
Set objFolder = objFSO.GetFolder(Subfolder.Path)
Set colFiles = objFolder.Files
For Each objFile in colFiles
' Wscript.Echo """" & Subfolder.Path & objFile.Name & """"
' Wscript.Echo """" & objFile.ParentFolder & "\" & objFile.Name & """"
If objFile.DateLastModified >= Date() Then
MySubFolderFilePath = MySubFolderFilePath & """" & Mid( objFile.ParentFolder & "\" & objFile.Name, Len(MyParentPath) + 2 ) & """" & " "
End If
Next
ShowSubFolders Subfolder
Next
End Sub

' Display a date with leading zero.
Function dateFormatLeadingZero( theDate )
dateFormatLeadingZero = DatePart("yyyy",theDate) _
& Right("0" & DatePart("m",theDate), 2) _
& Right("0" & DatePart("d",theDate), 2)
End Function

No comments: