I have a batch file that outputs a text file. I thought it would be nice if I could zip it up too.

This will be used in an uncontrolled environment, so I can't make assumptions about the presence of third-party software products such as 7-Zip, etc. This needs to use Windows' now-built-in capability to zip files.

link|improve this question
can 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 Feb 19 '10 at 19:59
so you send someone a batch file and you can not send him some tiny statically linked gzip.exe? – akira Feb 19 '10 at 22:16
feedback

5 Answers

up vote 7 down vote accepted

Here is an all BAT 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%
    copy %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).

link|improve this answer
1  
ouch. that's... just painful to see. +1 for heroic effort to satisfy an increasingly ridiculous set of requirements. – quack quixote Feb 22 '10 at 23:27
@quack I was holding out in the hope of some obscure command that I didn't know about. I can see how you might find that a difficult requirement if one doesn't exist, didn't mean to put anybody out. – Aaron Bush Mar 2 '10 at 13:56
@Peter Mortensen Wow... I think I have to accept that don't I? – Aaron Bush Mar 2 '10 at 13:57
@PeterMortensen: It may or may not be interesting to see how much this is simplified with Powershell. Here is a 5 line example, but there also exists a write-zip cmdlet that can be downloaded from the Pscx. – Tom Wijsman Nov 29 '11 at 13:54
feedback

It is possible to zip files without installation of any additional software (I have tested it). The solution is:

Run this in a command-line window to create a zip file named C:\someArchive.zip containing all files in folder C:\test3:

CScript  zip.vbs  C:\test3  C:\someArchive.zip

Where file zip.vbs contains:

'Get command-line arguments.
Set objArgs = WScript.Arguments
InputFolder = objArgs(0)
ZipFile = objArgs(1)

'Create empty ZIP file.
CreateObject("Scripting.FileSystemObject").CreateTextFile(ZipFile, True).Write "PK" & Chr(5) & Chr(6) & String(18, vbNullChar)

Set objShell = CreateObject("Shell.Application")

Set source = objShell.NameSpace(InputFolder).Items

objShell.NameSpace(ZipFile).CopyHere(source)

'Required!
wScript.Sleep 2000

I haven't tested it for paths and file names containing spaces. It may work if quotes are put around the command line parameters.


How it works: the built-in zip functionality in Windows (Windows XP and later?) is exposed through COM interfaces from the Windows shell , explorer.exe - that is the "Shell.Application" part. This COM interface can be used from a VBScript script because such a script can access COM components. To make the script fully self-contained it creates an empty ZIP file to get started (one could also create an empty ZIP file and copy it to the target system along with the VBScript script).

VBScript has been installed by default in every desktop release of Microsoft Windows since Windows 98.

CScript.exe is part of Windows Script Host. Windows Script Host is distributed and installed by default on Windows 98 and later versions of Windows. It is also installed if Internet Explorer 5 (or a later version) is installed.

link|improve this answer
+1 Good solution that will "Just Run", but I choose BAT for several other reasons not listed. So I would prefer a BAT solution. – Aaron Bush Feb 22 '10 at 13:30
2  
@Aaron Bush: this script and Beaner's both provide a command that zips a file on the commandline, using windows builtin functions, as requested. you can add that command to a batchfile like any other. – quack quixote Feb 22 '10 at 23:24
feedback

If you are open to using PowerShell, zip capabilities are available in .NET 2.0 (PowerShell is .NET). Here's an a example (source) credit to Mike Hodnick:

########################################################
# out-zip.ps1
#
# Usage:
#    To zip up some files:
#       ls c:\source\*.txt | out-zip c:\target\archive.zip $_
#
#    To zip up a folder:
#       gi c:\source | out-zip c:\target\archive.zip $_
########################################################

$path = $args[0]
$files = $input

if (-not $path.EndsWith('.zip')) {$path += '.zip'} 

if (-not (test-path $path)) { 
  set-content $path ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18)) 
} 

$ZipFile = (new-object -com shell.application).NameSpace($path) 
$files | foreach {$zipfile.CopyHere($_.fullname)}
link|improve this answer
1  
please post your example here, when possible, so a broken link doesn't mean a useless answer. – quack quixote Feb 20 '10 at 11:44
@Beaner +1 Good tip and example. – Aaron Bush Feb 22 '10 at 13:29
feedback

If you are able to install the Resource Kit Tools, you will find a command line tool called COMPRESS that can create compressed archive files like zip.

Microsoft (R) File Compression Utility  Version 5.00.2134.1
Copyright (C) Microsoft Corp. 1990-1999.  All rights reserved.

Compresses one or more files.

COMPRESS [-r] [-d] [-z] Source Destination
COMPRESS -r [-d] [-z] Source [Destination]

  -r            Rename compressed files.
  -d            Update compressed files only if out of date.
  -zx           LZX compression.
  -z            MS-ZIP compression.
  -zq[n]        Quantum compression and optional level
                (in range 1-7, default is 4).
  Source        Source file specification.  Wildcards may be used.
  Destination   Destination file | path specification.
                Destination may be a directory.
                If Source is multiple files and -r is not specified,
                Destination must be a directory.
link|improve this answer
+1 Doesn't really solve things, but still good to know about. – Aaron Bush Feb 22 '10 at 13:28
feedback

You can eliminate the risk of timing out during compression by polling for existence of the compression dialog window. This method also handles the user cancelling out of the compression window.

objShell.NameSpace(ZipFile).CopyHere(source)

' Wait for compression window to open
set scriptShell = CreateObject("Wscript.Shell")
Do While scriptShell.AppActivate("Compressing...") = FALSE   
   WScript.Sleep 500 ' Arbitrary polling delay
Loop  

' Wait for compression to complete before exiting script
Do While scriptShell.AppActivate("Compressing...") = TRUE   
   WScript.Sleep 500 ' Arbitrary polling delay
Loop
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.