Click here to Skip to main content
14,934,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

Does anyone know the command line to compress a folder to zip? Like on our default Windows OS, even we don't install 3rd party software to compress, we also can compress the folder by right click the folder/file and Send To --> Compressed (Zipped)

Is it possible to compress the folder? I don't want to install any 3rd party software to compress. I want to use Windows Compressed. If anyone here know the link, kindly give me..tq :)
Posted
Updated 10-Apr-21 8:29am

If you really want to do it without using any 3-rd party compress utility, with just the batch, you can use Windows Script Host (WSH):
http://en.wikipedia.org/wiki/Windows_Script_Host[^].

You can find a good number of code samples, this is just one of them: http://stackoverflow.com/questions/11100696/how-to-compress-in-a-batch-file[^].

The sample of batch file for this script is just one line (in case you did not recognize it):
CScript zip.vbs C:\test3 C:\someArchive.zip


You can also easily use WSH through PowerShell, without using VBScript or JavaScript:
http://en.wikipedia.org/wiki/Powershell[^].

If you are interested how to do it with PowerShell, I'll need some time to find a references, so please ask about it.

—SA
   
Comments
Luiey Ichigo 20-Feb-13 22:10pm
   
Hi Sergey,

I already use the code from your link at stakeflow by running the batch with vb script anddddddddd it's work. thank man. you rock. +5 for u..

I've no experience in PowerShell. Looking to it's code make me feel dizzy. LOL :) better with vbs

Instead of that, the compress only folder, but what if i want to compress multi extension file in a single compress. Means *.dll and *.ini will be compress into Application.zip (example)..is it possible? or I just copy the file into new folder and compress the new folder only?

What's your recommended?
   
My pleasure. If so, will you accept the answer formally (green button)?

As to PowerShell, it needs some attention and time to get into it. I'll tell you, I never saw anything better, in terms of the beauty and convenience of scripting language (of course, no need to compare with compiling languages), debugger, and the method of binding the APIs. One just need to get familiar with that stuff. I was myself very skeptical, before v.3.

However, even batch language, despite the brain-damaging features and lack of debugging, is better then many think; did you know that it even got method calls?

Now, compressing... What's the problem? Not to create an intermediate directory? I think, it's possible; you can use the directory where the zip file is supposed to be created.

Look at this sample:

http://www.techcoil.com/blog/handy-vbscript-functions-for-dealing-with-zip-files-and-folders/

zip.CopyHere works with Variant d.Items representing items to be packed. You can calculate it in VBScript or JavaScript based on the criteria you want, say, collect item by item in a loop, or something like that.

—SA
unzip in batch

@echo off
if "%1"=="" goto end

setlocal
set TEMPDIR=%TEMP%\ZIP
set FILETOZIP=%1
set OUTPUTZIP=%2.zip
if "%2"=="" set OUTPUTZIP=%1.zip

:: preparing VBS script
echo Set objArgs = WScript.Arguments > _zipIt.vbs
echo InputFolder = objArgs(0) >> _zipIt.vbs
echo ZipFile = objArgs(1) >> _zipIt.vbs
echo Set fso = WScript.CreateObject("Scripting.FileSystemObject") >> _zipIt.vbs
echo Set objZipFile = fso.CreateTextFile(ZipFile, True) >> _zipIt.vbs
echo objZipFile.Write "PK" ^& Chr(5) ^& Chr(6) ^& String(18, vbNullChar) >> _zipIt.vbs
echo objZipFile.Close >> _zipIt.vbs
echo Set objShell = WScript.CreateObject("Shell.Application") >> _zipIt.vbs
echo Set source = objShell.NameSpace(InputFolder).Items >> _zipIt.vbs
echo Set objZip = objShell.NameSpace(fso.GetAbsolutePathName(ZipFile)) >> _zipIt.vbs
echo if not (objZip is nothing) then >> _zipIt.vbs
echo objZip.CopyHere(source) >> _zipIt.vbs
echo wScript.Sleep 12000 >> _zipIt.vbs
echo end if >> _zipIt.vbs

@ECHO Zipping, please wait...
mkdir %TEMPDIR%
xcopy /y /s %FILETOZIP% %TEMPDIR%
WScript _zipIt.vbs %TEMPDIR% %OUTPUTZIP%
del _zipIt.vbs
rmdir /s /q %TEMPDIR%

@ECHO ZIP Completed.
:end
   
Comments
Richard Deeming 12-Apr-21 6:49am
   
An unformatted, unexplained code-dump which uses the same technique as the accepted solution from 2013 does not add anything to this discussion.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900