' Listing 2: BackupFile.vbs Option Explicit Dim strSourceFile, strTargetDir, nMaxTargetSize ' ===> MODIFY THIS SECTION WITH YOUR VALUES <==== Const SOURCE_FILE = "C:\source\myfile.mdb" Const TARGET_DIR = "d:\target" Const MAX_TARGET_DIR_SIZE = 5 ' ===> END MODIFICATION SECTION <==== ' ===> DO NOT MODIFY CODE BELOW THIS POINT. <=== Dim fso Set fso = CreateObject("Scripting.FileSystemObject") ' Make sure the source file exists. If Not fso.FileExists(SOURCE_FILE) Then WScript.Echo "ERROR: " & SOURCE_FILE & " does not exist!" WScript.Quit 1 End If ' Make sure the target directory exists. If Not fso.FolderExists(TARGET_DIR) Then WScript.Echo "ERROR: " & TARGET_DIR & " does not exist!" WScript.Quit 1 End If ' ******* BEGIN CALLOUT A ******* ' Generate the new filename and make sure it's unique. Dim strNewFileName strNewFileName = GetNewFileName(SOURCE_FILE, 0) ' Make sure the new filename doesn't already exist. ' If it does, append numbers. If fso.FileExists(TARGET_DIR & "\" & strNewFileName) Then Dim i i = 1 Do strNewFileName = GetNewFileName(SOURCE_FILE, i) i = i + 1 Loop Until Not fso.FileExists(TARGET_DIR & "\" & _ strNewFileName) End If ' ******* END CALLOUT A ******* ' Copy the file to the target folder. WScript.Echo "Copying " & SOURCE_FILE & " to " & _ TARGET_DIR & "\" & strNewFileName fso.CopyFile SOURCE_FILE, TARGET_DIR & "\" & _ strNewFileName ' ******* BEGIN CALLOUT B ******* ' Check the target folder. If it contains more than one file ' and the size of the folder is MB > MAX_TARGET_DIR_SIZE, ' keep on deleting the oldest file until the size complies with ' MAX_TARGET_DIR_SIZE or only one file is left. Dim oFolder Set oFolder = fso.GetFolder(TARGET_DIR) If oFolder.Files.Count > 1 Then Dim nFolderSizeMB nFolderSizeMB = (oFolder.Size / (1024 * 1024)) If nFolderSizeMB > MAX_TARGET_DIR_SIZE Then Dim strOldestFile Do strOldestFile = FindOldestFile(TARGET_DIR) WScript.Echo "DELETING FILE: " & strOldestFile fso.DeleteFile TARGET_DIR & "\" & strOldestFile, True nFolderSizeMB = (oFolder.Size / (1024 * 1024)) Loop Until ((nFolderSizeMB < MAX_TARGET_DIR_SIZE) _ OR (oFolder.files.count = 1)) End If End If ' ******* END CALLOUT B ******* Set fso = Nothing WScript.Quit 0 ' ******* BEGIN CALLOUT C ******* ' The FindOldestFile function's code Function FindOldestFile(strDirName) Dim file, oldestFileName, oldestFileDate oldestFileName = "UNKNOWN" oldestFileDate = Now For Each file in fso.GetFolder(strDirName).files If DateDiff("s",file.DateLastModified,oldestFileDate) > 0 Then oldestFileName = file.Name oldestFileDate = file.DateLastModified End If Next FindOldestFile = oldestFileName End Function ' ******* END CALLOUT C ******* ' ******* BEGIN CALLOUT D ******* ' The GetNewFileName function's code Function GetNewFileName(strFilename, suffix) Dim nMonth, nDay nMonth = Month(Now) If nMonth < 10 Then nMonth = "0" & nMonth End If nDay = Day(Now) If nDay < 10 Then nDay = "0" & nDay End If If suffix <> "0" Then GetNewFileName = fso.GetBaseName(strFileName) & _ YEAR(NOW) & nMonth & nDay & "-" & suffix & "." & _ fso.GetExtensionName(strFileName) Else GetNewFileName = fso.GetBaseName(strFileName) & _ YEAR(NOW) & nMonth & nDay & "." & _ fso.GetExtensionName(strFileName) End If End Function ' ******* END CALLOUT D *******