{"id":1088,"date":"2014-06-30T21:58:30","date_gmt":"2014-06-30T21:58:30","guid":{"rendered":"http:\/\/www.nooblet.org\/blog\/?p=1088"},"modified":"2018-01-06T17:17:24","modified_gmt":"2018-01-06T17:17:24","slug":"using-vbscript-to-create-dated-backups-with-7-zip","status":"publish","type":"post","link":"https:\/\/www.nooblet.org\/blog\/2014\/using-vbscript-to-create-dated-backups-with-7-zip\/","title":{"rendered":"Using VBScript to Create Dated Backups with 7-Zip"},"content":{"rendered":"<p>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.<\/p>\n<p>I chose VBScript to complete this task, re-learning a few things from the last time I used the language.<\/p>\n<p>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.<\/p>\n<pre class=\"lang:vb decode:true \" >' Backup folder using 7-Zip\r\n' Written by Steve Allison 2014 - steve@allison.im\r\n\r\nDim fso, rs, shell\r\n\r\n' File System Object\r\nSet fso = CreateObject(\"Scripting.FileSystemObject\")\r\n' RecordSet\r\nSet rs = CreateObject(\"Ador.Recordset\")\r\n' Shell\r\nSet shell = CreateObject(\"WScript.Shell\")\r\n\r\nConst adVarChar = 200\r\nConst adDate = 7\r\n\r\nsrcFolder=\"C:\\Customer\"\r\ndstFolder=\"S:\\Backup\"\r\nbackupName=\"backup\"\r\nzipEXE=\"C:\\Program Files\\7-Zip\\7z.exe\"\r\n\r\n' Number of files to keep\r\niNum = 5\r\n\r\n' Get the date in the correct order. Why does vbscript suck so hard at date formatting?\r\nFunction getDateString()\r\n    d = ZeroPad(Day(Now()), 2)\r\n    m = ZeroPad(Month(Now()), 2)    \r\n    y = Year(Now())\r\n    getDateString = y &amp; m &amp; d\r\nEnd Function\r\n\r\n' No printf() in VBScript it seems\r\nFunction ZeroPad(int, length)\r\n\tIf Len(int) &lt; length Then\r\n\t\tZeroPad = Right(String(length, \"0\") &amp; int, length)\r\n\tEnd If\r\nEnd Function\r\n\r\n' Sanity checking\r\nIf Not fso.FolderExists(srcFolder) Then\r\n\tWscript.Echo \"Aborted. Source folder does not exist: \" &amp; srcFolder\r\n\tWscript.Quit\r\nEnd If\r\nIf Not fso.FolderExists(dstFolder) Then\r\n\tWscript.Echo \"Aborted. Destination folder does not exist: \" &amp; dstFolder\r\n\tWscript.Quit\r\nEnd If\r\nIf Not fso.FileExists(zipEXE) Then\r\n\tWscript.Echo \"Aborted. 7-Zip program does not exist: \" &amp; zipEXE\r\n\tWscript.Quit\r\nEnd If\r\n\r\n' Create suffix of date-time\r\nbackupFileDate = getDateString() &amp; \"-\" &amp; replace(FormatDateTime(now,4),\":\",\"\")\r\n\r\n' File extension\r\nbackupFileExt = \".7z\"\r\n\r\n' Backup path without extension\r\nbackupFilePre = dstFolder &amp; \"\/\" &amp; backupName &amp; \"_\" &amp; backupFileDate\r\n\r\n' Full backup path\r\nbackupFile = backupFilePre &amp; backupFileExt\r\n\r\n' More sanity checking\r\nn = 1\r\nDo While fso.FileExists(backupFile)\r\n\t' Add integeer to file, loop until it doesn't already exist\r\n\tbackupFile = backupFilePre &amp; \"_\" &amp; ZeroPad(n, 2) &amp; backupFileExt\r\n\tn = n + 1\r\nLoop\r\n\r\n'''' Zip Source Folder\r\n' Create shell command\r\nshCommand = \"\"\"\" &amp; zipEXE &amp; \"\"\" a -r \"\"\" &amp; backupFile &amp; \"\"\"\"\r\n' Change to source directory\r\nshell.CurrentDirectory = srcFolder &amp; \"\/\"\r\n' Run 7-Zip in shell\r\nshVal = shell.Run(shCommand,4,true)\r\n\r\n' Check 7-Zip exit code\r\nIf shVal &gt; 1 Then\r\n\tWscript.Echo \"7-Zip failed with error code: \" &amp; shVal\r\n\tWscript.Quit\r\nEnd If\r\n\r\n'''' Remove old backup files\r\n' Add required fields to recordset\r\nWith rs.Fields\r\n\t.append \"filepath\", adVarChar, 255\r\n\t.append \"datelastmodified\", adDate\r\nEnd With\r\n\r\n' Get folder object\r\nset rsFolder=fso.getfolder(dstFolder)\r\n\r\n' List folder contents to RecordSet\r\nWith rs\r\n    .open\r\n    For Each rsFile in rsFolder.files\r\n        .addnew array(\"filepath\",\"datelastmodified\"), array(rsFile.path,rsFile.datelastmodified)\r\n        .update\r\n    Next\r\nEnd With\r\n\r\n' Loop through folder listing recordset\r\ni=0\r\nIf Not (rs.EOF and rs.BOF) then\r\n\t' Sort by last modified, newest first\r\n\trs.Sort = \"datelastmodified desc\"\r\n\t' Move recordset pointer to first record\r\n\trs.MoveFirst\r\n\t' Loop through recordset\r\n\tDo While Not rs.EOF\r\n\t\t' get path from recordset\r\n\t\tdFile = fso.GetFile(rs.Fields(\"filepath\"))\r\n\t\t' get filename from path\r\n\t\tdFileName = fso.GetFileName(dFile)\r\n\t\t' Check if backupName is in the filename\r\n\t\tif InStr(1, dFileName, backupName, 1) Then\r\n\t\t\ti=i+1\r\n\t\t\t' wait until &gt;iNum matches\r\n\t\t\tif i &gt; iNum Then\r\n\t\t\t\t' Delete file, ignore errors\r\n\t\t\t\tOn Error Resume Next\r\n\t\t\t\tfso.DeleteFile rs.Fields(\"filepath\"), true\r\n\t\t\t\tOn Error Goto 0\r\n\t\t\tEnd If\r\n\t\tEnd If\r\n\t\trs.MoveNext\r\n\tLoop\r\nEnd If\r\n\r\nWscript.Echo \"Backup complete at \" &amp; backupFile<\/pre>\n<p><b>Download<\/b><br \/>\n<img decoding=\"async\" src=\"\/blog\/wp-content\/plugins\/wp-downloadmanager\/images\/ext\/unknown.gif\" alt=\"\" title=\"\" style=\"vertical-align: middle;\" \/>&nbsp;&nbsp;<strong><a href=\"https:\/\/www.nooblet.org\/blog\/download\/backup-folder.vbs\">backup-folder.vbs<\/a><\/strong> (3.4 KiB, 3,979 hits)<br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":641,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[226,225,234],"class_list":["post-1088","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-windows","tag-7-zip","tag-vbscript","tag-windows"],"_links":{"self":[{"href":"https:\/\/www.nooblet.org\/blog\/wp-json\/wp\/v2\/posts\/1088","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.nooblet.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nooblet.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nooblet.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nooblet.org\/blog\/wp-json\/wp\/v2\/comments?post=1088"}],"version-history":[{"count":5,"href":"https:\/\/www.nooblet.org\/blog\/wp-json\/wp\/v2\/posts\/1088\/revisions"}],"predecessor-version":[{"id":1175,"href":"https:\/\/www.nooblet.org\/blog\/wp-json\/wp\/v2\/posts\/1088\/revisions\/1175"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.nooblet.org\/blog\/wp-json\/wp\/v2\/media\/641"}],"wp:attachment":[{"href":"https:\/\/www.nooblet.org\/blog\/wp-json\/wp\/v2\/media?parent=1088"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nooblet.org\/blog\/wp-json\/wp\/v2\/categories?post=1088"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nooblet.org\/blog\/wp-json\/wp\/v2\/tags?post=1088"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}