Skip to content

Instantly share code, notes, and snippets.

@f-steff
Created October 11, 2019 19:38
Show Gist options
  • Save f-steff/c866ab74bc3934b9159a65b3d7841846 to your computer and use it in GitHub Desktop.
Save f-steff/c866ab74bc3934b9159a65b3d7841846 to your computer and use it in GitHub Desktop.
Batch sub-function to create a zip archive from a folder.
:: === Main code:
:: My answer to https://stackoverflow.com/questions/17546016/how-can-you-zip-or-unzip-from-the-script-using-only-windows-built-in-capabiliti/58013665#58013665
call :ZipUp "C:\Some\Path" "C:\Archive.zip"
:: === SubRoutines:
:ZipUp
::Arguments: Source_folder, destination_zip
(
echo:Set fso = CreateObject^("Scripting.FileSystemObject"^)
echo:InputFolder = fso.GetAbsolutePathName^(WScript.Arguments.Item^(0^)^)
echo:ZipFile = fso.GetAbsolutePathName^(WScript.Arguments.Item^(1^)^)
echo:
echo:' Create empty ZIP file.
echo:CreateObject^("Scripting.FileSystemObject"^).CreateTextFile^(ZipFile, True^).Write "PK" ^& Chr^(5^) ^& Chr^(6^) ^& String^(18, vbNullChar^)
echo:
echo:Set objShell = CreateObject^("Shell.Application"^)
echo:Set source = objShell.NameSpace^(InputFolder^).Items
echo:objShell.NameSpace^(ZipFile^).CopyHere^(source^)
echo:
echo:' Keep script waiting until compression is done
echo:Do Until objShell.NameSpace^( ZipFile ^).Items.Count = objShell.NameSpace^( InputFolder ^).Items.Count
echo: WScript.Sleep 200
echo:Loop
)>_zipup.vbs
CScript //Nologo _zipup.vbs "%~1" "%~2"
del _zipup.vbs
goto :eof
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment