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.txt
set TEMPDIR=C:\temp738
rmdir %TEMPDIR%
mkdir %TEMPDIR%
xcopy /s %FILETOZIP% %TEMPDIR%
echo Set objArgs = WScript.Arguments > _zipIt.vbs
echo InputFolder = objArgs(0) >> _zipIt.vbs
echo ZipFile = objArgs(1) >> _zipIt.vbs
echo CreateObject("Scripting.FileSystemObject").CreateTextFile(ZipFile, True).Write "PK" ^& Chr(5) ^& Chr(6) ^& String(18, vbNullChar) >> _zipIt.vbs
echo Set objShell = CreateObject("Shell.Application") >> _zipIt.vbs
echo Set source = objShell.NameSpace(InputFolder).Items >> _zipIt.vbs
echo objShell.NameSpace(ZipFile).CopyHere(source) >> _zipIt.vbs
echo wScript.Sleep 2000 >> _zipIt.vbs
CScript _zipIt.vbs %TEMPDIR% C:\someArchive.zip
pause
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.
1can you utilize Powershell or WSH scripting? that might be the only way to use Windows' builtin zip handling from the commandline. otherwise, as Molly points out, you need a 3rd-party tool. – quack quixote – 2010-02-19T19:59:27.190
1so you send someone a batch file and you can not send him some tiny statically linked gzip.exe? – akira – 2010-02-19T22:16:31.360
The OP's question is an excellent one (@quackquixote 's strange accusation notwithstanding). Since Windows does provide this as a single click under SendTo, there ought to be a command usable in a BAT file. So it's a good question even if the answer is No and one has to (ridiculously) resort to using a third-party tool that may or may not be equivalent. – Jon Coombs – 2014-12-24T00:33:05.703
This is link by Tomas has a well written script to zip contents of a folder.
To make it work just copy the script into a batch file and execute it by specifying the folder to be zipped(source).
No need to mention destination directory as it is defaulted in the script to Desktop ("%USERPROFILE%\Desktop")
3The simpliest would be, in a cmd prompt :
powershell.exe Compress-Archive file-to-zip.txt zippedfile.zip
(it works with folder too) – ThomasGuenet – 2017-09-05T08:33:05.530