Skip to content

Instantly share code, notes, and snippets.

@wave2
Created June 27, 2014 17:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wave2/6502c9f86001537efa6b to your computer and use it in GitHub Desktop.
Save wave2/6502c9f86001537efa6b to your computer and use it in GitHub Desktop.
VBScript to 7Zip files based on extension (Requires 7Zip)
OPTION EXPLICIT
DIM strExtensionsToZip,strFolder
DIM objFSO, WshShell, MaxAge, IncludeSubFolders
' Folder to zip files
strFolder = "c:\myfolder"
'Include subfolders?
includeSubfolders = true
' File extension to search for
strExtensionsToZip = "txt"
' Max File Age (in Days). Files older than this will be zipped.
maxAge = 14
set objFSO = createobject("Scripting.FileSystemObject")
set WshShell = WScript.CreateObject("WScript.Shell")
ZipFiles strFolder,strExtensionsToZip, maxAge, includeSubFolders
wscript.echo "Finished"
sub ZipFiles(byval strDirectory,byval strExtensionsToZip,byval maxAge,includeSubFolders)
DIM objFolder, objSubFolder, objFile
DIM strExt, oExec, strCommand
set objFolder = objFSO.GetFolder(strDirectory)
for each objFile in objFolder.Files
for each strExt in SPLIT(UCASE(strExtensionsToZip),",")
if RIGHT(UCASE(objFile.Path),LEN(strExt)+1) = "." & strExt then
IF objFile.DateLastModified < (Now - MaxAge) THEN
wscript.echo "Zipping: " & objFile.Path
strCommand = """c:\Program Files\7-Zip\7z.exe"" a -tzip """ & objFSO.GetParentFolderName(objFile) & "\" & objFSO.GetBaseName(objFile) & ".zip"" """ & objFile.Path & """"
WshShell.Run strCommand,0,true
WScript.Sleep 5000
objFile.Delete
exit for
END IF
end if
next
next
if includeSubFolders = true then ' Recursive delete
for each objSubFolder in objFolder.SubFolders
ZipFiles objSubFolder.Path,strExtensionsToZip,maxAge, includeSubFolders
next
end if
end sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment