Skip to content

Instantly share code, notes, and snippets.

@ccritchfield
Last active December 5, 2019 04:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ccritchfield/20793d9d4e3ce5bdc078bd292142d42c to your computer and use it in GitHub Desktop.
Save ccritchfield/20793d9d4e3ce5bdc078bd292142d42c to your computer and use it in GitHub Desktop.
VBS - Zip Up Folder (using FBZip.exe)
--------------------------------------------
Windows Command-line Zip Automation
--------------------------------------------
VBS / VBScript / WScript that uses FBZip.exe to automate zipping
up folders and archiving them into a folder. Runs from DOS commandline,
and can task schedule to fire off and run after-hours for archiving /
back-ups.
Uses FBZip, b/c it's a small free zip program that has easy-to-use
commandline functionality.
Purpose...
At a previous job, we used RoboHelp for an online intranet help system
that CSR's, Sales, etc could quickly look up help on for products,
sales procedures, etc. I got in the habit of zipping up the various
RoboHelp help systems we had as back-ups in case something got borked
up or corrupted (since I wasn't the only one working on them). IT was
doing nightly backups, but it was just easier to have a .zip backup
on-hand in case we needed to make a quick recover. Also, if we were
going to make a massive change, we could just run the .zip routine to
pre-zip a bunch of backups before making the changes. While I have other
scripts using DOS, I have this one using WScript (windows shell script),
which is basically Visual Basic Script (VBS). It runs on commandline just
like a DOS script, so you can task schedule it to fire off as-needed.
You'll need to download and keep the FBZip.exe program in the same folder
as this script for it to run properly, because the script calls on FBZip
to do the zipping.
'''''''''''''''''''''''''''''''''''''''''''''
' SUMMARY
'
' creates a Wscript shell to execute command-line / console commands.
' shell doesn't wait for it's current operation to finish before firing off the next operation.
' so, I have to get each folder to zip separately. if I tried to feed them into the same .zip file
' the zip program might still be busy with the previous folder zip using that same zip file name
' and it causes an error. So, I just bypass that and zip each folder into its own zip file.
' it actually works out ok, because it'll rapidly kick off all the zip sessions, and
' process them all at once instead of having to do them in sequence.
'
'
'''''''''''''''''''''''''''''''''''''''''''''
' GLOBALS - variables used through-out script
Dim s 'Wscript shell to execute console commands with (to execute zip command-line program)
Dim z 'zip file name
Dim d 'formatted date ... yyyy_mm_dd, to date-stamp zip files
'''''''''''''''''''''''''''''''''''''''''''''
' MAIN SCRIPT
'create shell once...sub-process will use it for zipping
set s = CreateObject("Wscript.Shell")
'this just adds a break-line in the log file between each date we're backing stuff up
s.run "cmd /C echo %DATE% %TIME% ------------------------------- >> ..\RoboBackup.log"
'create formatted date we'll stamp on zip file names
d = format_date(Date)
'sub-proc uses shell to run command-line zip to pack each folder into the zip file
'since the shell doesn't wait for the prior command to finish, you'll see all these
'zip windows open up at once, and do their thing concurrently.
zip_folder "cares"
zip_folder "customerrelations"
zip_folder "customerservice"
zip_folder "operations"
zip_folder "provisioning_old"
zip_folder "revenuerecovery"
zip_folder "sales"
zip_folder "sales_tpv"
zip_folder "shared"
zip_folder "tag"
'''''''''''''''''''''''''''''''''''''''''''''
Sub zip_folder(f)
' Sub logs what's zipping, and zips it up
' have to use zip command as if we're in the folder fbzip.exe is running from
' so, we name the .zip file ..\name.zip, so it'll store in the next folder up the tree (_archive)
' for the folder we're zipping up, we have to ..\..\ to go back 2 folder levels to point to it
'
' f = the folder we're copying over
'log what we're zipping
'can't just run echo...have to first run cmd to do console window then run the echo string to it
'using the /C switch with cmd to have it run the command then terminate
'using ..\ to toss the .log file into the _archive folder along with the .zips
'instead of stashing the .log file with the .bat files (too much clutter)
'CHANGE - 7/28/08
'I used to include a date stamp on the file name, but then I started running my sentinel checks at
'midnight. So, Robohelp would update, but sentinel would check for the file at midnight starting the
'new day, thus never finding a 'current day' file. I decided to dump the date stamp in the file name
'and just have a one appending log. Sentinel will check for date changes on the log file now,
'letting us know when it's changed / updated.
' s.run "cmd /C echo %DATE% %TIME% ... zipping..." & f & " >> ..\RoboBackup_" & d & ".log"
s.run "cmd /C echo %DATE% %TIME% ... zipping..." & f & " >> ..\RoboBackup.log"
'zip each folder into _archive as <foldername>_yyyy_mm_dd.zip
s.run "fbzip.exe -a -p -r " & "..\" & f & "_" & d & ".zip ..\..\" & f & "\"
End Sub
'''''''''''''''''''''''''''''''''''''''''''''
Function format_date(MyDate)
' converts date passed to it to "yyyy_mm_dd" format
dim yyyy 'year
dim mm 'month
dim dd 'date
yyyy = Year(MyDate)
'get month, and if it's less than 2 char's long, pad a zero in front of it
mm = Month(MyDate)
mm = string(2 - len(mm),"0") & mm
'get day, and if it's less than 2 char's long, pad a zero in front of it
dd = Day(MyDate)
dd = string(2 - len(dd),"0") & dd
format_date = yyyy & "_" & mm & "_" & dd
End Function
'''''''''''''''''''''''''''''''''''''''''''''
Sub Wait_Seconds(s)
' forces script to wait a number of seconds passed to it
' haven't use this yet, but noticed that some folders weren't getting zipped
' I thought maybe shell was firing off so fast it skipped over some, so
' maybe it needed to wait in between. Gonna let it run a little more to see
' if I do need to use this or not.
Dim t 'var to store seconds since midnight
t = timer 'snapshot current seconds since midnight
Do Until Timer = t + s 'loop until seconds snapshot + seconds to wait has passed
' Do Events 'WSH doesn't support a "Do Events" pause, so just let it loop instead
Loop
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment