This blog has moved to http://ThePowerShellGuy.com
Greetings /\/\o\/\/
This function will add files to a "Compressed (zipped) Folder" or will make a new ZIP file if the file does not exists.
you can use the Shell.application COM Object to add files or folders to a Compressed folder, so you can also use the CopyGUI ScriptMethod from
Update-TypeData (Democracy to the types) , to add files or folders to a ZIP file, as this uses this object also.
(gi test.txt).CopyGUI("c:\mow.zip")
MSH>(gi .).copygui
Script : $name= $args[0]
#$target = (read-host "Enter Target: ")
$(new-object -com shell.application).NameSpace($name).CopyHere($this.fullname)
trap {"Error Copying"; continue }
OverloadDefinitions : {System.Object CopyGUI();}
MemberType : ScriptMethod
TypeOfValue : System.Object
Value : System.Object CopyGUI();
Name : CopyGUI
IsInstance : False
But only if this ZIP file does already exist.
the Out-Zip function will also Create a New ZIP file (Compressed Folder) , if it does not exist.
I made it to take file or directory objects from the pipeline so you can use it like this :
# add all textfiles in Dir to Mow.zip
ls *.txt | out-zip c:\mow.zip
#add Monad directory to zipfile
cd g:\monad
gi . | outzip c:\mow.zip
(more like a filter)
the script looks like this :
# Out-Zip.msh
# creates out-Zip function
# /\/\o\/\/ 2006
# http://mow001.blogspot.com
function out-zip {
Param([string]$path)
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)
$input | foreach {$zipfile.CopyHere($_.fullname)}
}
enjoy,
gr /\/\o\/\/
Tags : Monad msh