none
simple script to zip up a bunch of files RRS feed

  • Question

  • Using Win-7 Pro, x86-64 ...

    This has got to be simpler than the solutions I've seen so far.

    I have a directory with several dozen files.  I'd like to create a zip file backup of that directory on a regular basis.  I'd like to script that so that I can have the scheduler run it without my having to manually do anything (in fact, I'd plan on scheduling it to run on the weekend, when I'm not even in the office).   On my Linux boxes, it's a simple one-liner:

    zip /my_backup_directory/myzipfile /my_source_directory/*
    Everything I've found so far for doing this in Windows either requires some additional software (not part of the basic Windows installation), or several dozen lines of vbscript.  Surely I'm overlooking something simple.

    Thursday, March 13, 2014 5:28 PM

Answers

  • You're not missing anything.

    Windows does not have a command-line zip tool. Explorer has shell objects for creating and unzipping zip files, but they are not designed for automation.

    The best solution I can suggest is to copy a freeware utility to the computer and use it from the command line.


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by edstevensdba Friday, March 14, 2014 12:36 PM
    Thursday, March 13, 2014 5:35 PM
    Moderator

All replies

  • You're not missing anything.

    Windows does not have a command-line zip tool. Explorer has shell objects for creating and unzipping zip files, but they are not designed for automation.

    The best solution I can suggest is to copy a freeware utility to the computer and use it from the command line.


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by edstevensdba Friday, March 14, 2014 12:36 PM
    Thursday, March 13, 2014 5:35 PM
    Moderator
  • 7Zip has a nice command line version that you can use:

    http://7-zip.org/download.html

    EDIT: You can use this if you've pushed .Net 4.5 to your machines:

    http://msdn.microsoft.com/en-us/library/system.io.compression.zipfile%28v=vs.110%29.aspx


    Don't retire TechNet! - (Don't give up yet - 12,700+ strong and growing)


    Thursday, March 13, 2014 5:57 PM
  • If you have Net 4.5 installed you can use the Net zipper classes to quickly and natively zip a folder.


    ¯\_(ツ)_/¯

    Thursday, March 13, 2014 7:10 PM
  • Here are a whole bunch of zippers that have been created in the last month.  Take your pick.

    http://gallery.technet.microsoft.com/site/search?query=zip&f%5B0%5D.Value=zip&f%5B0%5D.Type=SearchText&ac=4


    ¯\_(ツ)_/¯


    • Edited by jrv Thursday, March 13, 2014 7:12 PM
    Thursday, March 13, 2014 7:12 PM
  • Go with whatever creates the least amount of friction in your environment.

    However, I definitely do not recommend trying to zip files in a script using the shell objects. The shell objects run asynchronously and have no error checking. They are not designed for automation.


    -- Bill Stewart [Bill_Stewart]

    Thursday, March 13, 2014 7:18 PM
    Moderator
  • You can do it with powershell and the zip package dot net class:

    It's a code from David in California - see http://gallery.technet.microsoft.com/scriptcenter/Zip-Files-in-a-Folder-cc5bc494

    # Search the $LogFolder directory for all .log files. 
        $objLogFolderContents = Get-ChildItem $LogFolder -Include *.log -recurse 
         
        # Open the zip archive file. 
        $ZipArchiveFile = [System.IO.Packaging.ZipPackage]::Open($strZipFileLocation, [System.IO.FileMode]"OpenOrCreate", [System.IO.FileAccess]"ReadWrite"
     
        # For each file in the $LogFolder directory, add it to the $strZipFileLocation archive file. 
        ForEach ($File In $objLogFolderContents
        { 
             If ($File.lastwritetime.month -eq $GetDateMonthPrior
            { 
                # Define the URI of the file to be zipped, and then include it in the CreatePart method. 
                $strFileName = split-path $File -Leaf 
                # Write-Host "strFileName" $strFileName # This can be enabled for troubleshooting. 
                $FileToZip = "/" + $strFileName 
                # Write-Host "FileToZip" $FileToZip # This can be enabled for troubleshooting. 
                $Part = $ZipArchiveFile.CreatePart($FileToZip"application/zip", [System.IO.Packaging.CompressionOption]"Maximum"
     
                # The $File variable will be used by the System.IO.File class to locate and read the file to be zipped. 
                # Then, add the $File to the $ZipFileArchive.    
                $Bytes=[System.IO.File]::ReadAllBytes($File
                $Stream=$Part.GetStream() 
                $Stream.Write($Bytes, 0, $Bytes.Length) 
                $Stream.Close() 
                $CreateEntryFromFileStatus = $
     
                If ($CreateEntryFromFileStatus -eq $True)  
                { 
                    Remove-Item $File 
                } 
            } 
        } 
     
        # Close the $ZipArchiveFile. 
        $ZipArchiveFile.Close()

    Thursday, March 13, 2014 9:44 PM
  • You're not missing anything.

    Windows does not have a command-line zip tool. Explorer has shell objects for creating and unzipping zip files, but they are not designed for automation.

    The best solution I can suggest is to copy a freeware utility to the computer and use it from the command line.


    -- Bill Stewart [Bill_Stewart]


    Why am I shocked and at the same time not surprised? A one-line 'zip' command vs. 21 lines (not counting comments) of vb script.  I guess I'll evaluate the free 3d part tools.  Thanks for confirming.
    Friday, March 14, 2014 12:36 PM