Quantcast
Viewing all articles
Browse latest Browse all 14

Answer by Peter Mortensen for Can you zip a file from the command prompt using ONLY Windows' built-in capability to zip files?

Here is an all batch file solution (a variation of my other answer) that will zip a file named c:\ue_english.txt and put it in C:\someArchive.zip:

set FILETOZIP=c:\ue_english.txtset TEMPDIR=C:\temp738rmdir %TEMPDIR%mkdir %TEMPDIR%xcopy /s %FILETOZIP% %TEMPDIR%echo Set objArgs = WScript.Arguments > _zipIt.vbsecho InputFolder = objArgs(0) >> _zipIt.vbsecho ZipFile = objArgs(1) >> _zipIt.vbsecho CreateObject("Scripting.FileSystemObject").CreateTextFile(ZipFile, True).Write "PK" ^& Chr(5) ^& Chr(6) ^& String(18, vbNullChar) >> _zipIt.vbsecho Set objShell = CreateObject("Shell.Application") >> _zipIt.vbsecho Set source = objShell.NameSpace(InputFolder).Items >> _zipIt.vbsecho objShell.NameSpace(ZipFile).CopyHere(source) >> _zipIt.vbsecho wScript.Sleep 2000 >> _zipIt.vbsCScript  _zipIt.vbs  %TEMPDIR%  C:\someArchive.zippause

Write access is required to the parent of the folder stored in TEMPDIR. As this is often not the case for the root of drive C TEMPDIR may have to be changed.

Write access is also required for the folder the .bat script is in (as it generates a file there).

Also, please note that the file extension for the compressed file must be .zip. Attempts to use another extension may result in a script error. Instead, generate the .zip file and rename it.


Viewing all articles
Browse latest Browse all 14

Trending Articles