Created
June 27, 2014 17:52
-
-
Save wave2/6502c9f86001537efa6b to your computer and use it in GitHub Desktop.
VBScript to 7Zip files based on extension (Requires 7Zip)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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