Thursday, 17 July 2008

Zip-File.ps1

<#
.SYNOPSIS
    This script zips a particular file into an existing archive (zip file).
.DESCRIPTION
    For this script, c:\foo\test.zip must exist
    The script zips c:\test2.txt into c:\foo\test.zip
.NOTES
    File Name  : Zip-File.ps1
    Author     : Thomas Lee - tfl@psp.co.uk
    Requires   : PowerShell V2 CTP3
.LINK
    http://pshscripts.blogspot.co.uk/2008/07/zip-fileps1.html
.EXAMPLE
#>

##
# Start of Script
##

# Get shell object and check to see that file exists
$sh = new-object -com shell.application
$fl = get-childitem c:\foo\test.zip -Ea 0
if (!$fl)
 {
   'File does not exist - stopping'
   return
 }

# Get Zip File to zip into
$targetzip = $sh.namespace("C:\Foo\test.zip")   # where the item is to go

# Copy a file into this zip
$targetzip.copyhere("C:\foo\test.txt")

# Check it now exists
ls c:\foo\test.zip
# End of Script

3 comments:

Alex said...

At operation with zip files recommend to use modern tools,on example this application-repairing zip file with cyclic redundancy error,I finded it in google when I had been problems with zip file,utility is free as far as I know,it can extract needed document from corrupted archive and repair it,will successfully recover all documents,works faster and recovers more documents, than other services,program works under all supported versions of Microsoft Windows operating system,will mark all files for zip crc failed repair and zip archive crc repair,repair zip files crc error and you'd like to save recovered data,also possible to select your Zip file from the list of previously recovered archives.

Alex said...

I received on mail zip file,but it was corrupted,accidentally in one inet portal I found-zip files and does not appear to be a valid archive.And program helped me and it has free status,moreover program for extracting zip files 2gb Microsoft,also can extract your documents from corrupted Zip files.

Som DT said...

Don't use copyhere method anywhere in your code. It produces GUI Message-box and you will never know why your script failed.
So script might lead to an inconsistent state.