I needed a simple method to backup a small folder using 7-Zip on a regular basis without installing extra software. I wanted to be able to leave it running daily and have it remove old backups.
I chose VBScript to complete this task, re-learning a few things from the last time I used the language.
The script shall backup C:\Customer to S:\Backup\backup_DATE.7z. It will also delete old backups, only keeping the first 5. The directories and number of kept backups are variables, check the code comments.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
' Backup folder using 7-Zip ' Written by Steve Allison 2014 - [email protected] Dim fso, rs, shell ' File System Object Set fso = CreateObject("Scripting.FileSystemObject") ' RecordSet Set rs = CreateObject("Ador.Recordset") ' Shell Set shell = CreateObject("WScript.Shell") Const adVarChar = 200 Const adDate = 7 srcFolder="C:\Customer" dstFolder="S:\Backup" backupName="backup" zipEXE="C:\Program Files\7-Zip\7z.exe" ' Number of files to keep iNum = 5 ' Get the date in the correct order. Why does vbscript suck so hard at date formatting? Function getDateString() d = ZeroPad(Day(Now()), 2) m = ZeroPad(Month(Now()), 2) y = Year(Now()) getDateString = y & m & d End Function ' No printf() in VBScript it seems Function ZeroPad(int, length) If Len(int) < length Then ZeroPad = Right(String(length, "0") & int, length) End If End Function ' Sanity checking If Not fso.FolderExists(srcFolder) Then Wscript.Echo "Aborted. Source folder does not exist: " & srcFolder Wscript.Quit End If If Not fso.FolderExists(dstFolder) Then Wscript.Echo "Aborted. Destination folder does not exist: " & dstFolder Wscript.Quit End If If Not fso.FileExists(zipEXE) Then Wscript.Echo "Aborted. 7-Zip program does not exist: " & zipEXE Wscript.Quit End If ' Create suffix of date-time backupFileDate = getDateString() & "-" & replace(FormatDateTime(now,4),":","") ' File extension backupFileExt = ".7z" ' Backup path without extension backupFilePre = dstFolder & "/" & backupName & "_" & backupFileDate ' Full backup path backupFile = backupFilePre & backupFileExt ' More sanity checking n = 1 Do While fso.FileExists(backupFile) ' Add integeer to file, loop until it doesn't already exist backupFile = backupFilePre & "_" & ZeroPad(n, 2) & backupFileExt n = n + 1 Loop '''' Zip Source Folder ' Create shell command shCommand = """" & zipEXE & """ a -r """ & backupFile & """" ' Change to source directory shell.CurrentDirectory = srcFolder & "/" ' Run 7-Zip in shell shVal = shell.Run(shCommand,4,true) ' Check 7-Zip exit code If shVal > 1 Then Wscript.Echo "7-Zip failed with error code: " & shVal Wscript.Quit End If '''' Remove old backup files ' Add required fields to recordset With rs.Fields .append "filepath", adVarChar, 255 .append "datelastmodified", adDate End With ' Get folder object set rsFolder=fso.getfolder(dstFolder) ' List folder contents to RecordSet With rs .open For Each rsFile in rsFolder.files .addnew array("filepath","datelastmodified"), array(rsFile.path,rsFile.datelastmodified) .update Next End With ' Loop through folder listing recordset i=0 If Not (rs.EOF and rs.BOF) then ' Sort by last modified, newest first rs.Sort = "datelastmodified desc" ' Move recordset pointer to first record rs.MoveFirst ' Loop through recordset Do While Not rs.EOF ' get path from recordset dFile = fso.GetFile(rs.Fields("filepath")) ' get filename from path dFileName = fso.GetFileName(dFile) ' Check if backupName is in the filename if InStr(1, dFileName, backupName, 1) Then i=i+1 ' wait until >iNum matches if i > iNum Then ' Delete file, ignore errors On Error Resume Next fso.DeleteFile rs.Fields("filepath"), true On Error Goto 0 End If End If rs.MoveNext Loop End If Wscript.Echo "Backup complete at " & backupFile |
Download
backup-folder.vbs (3.4 KiB, 3,237 hits)
Neat script, thanks for sharing!
thanks buddy
Bug in zeropad function:
‘ No printf() in VBScript it seems
Function ZeroPad(int, length)
If Len(int) < length Then
ZeroPad = Right(String(length, "0") & int, length)
End If
End Function
should be
' No printf() in VBScript it seems
Function ZeroPad(val, length)
If Len(val) < length Then
ZeroPad = Right(String(length, "0") & val, length)
Else
ZeroPad = val
End If
End Function
How can I compress only by extension?
example, only xls files?