I have a code like below. In the source folder I have folders, sub folders and files.
I need to zip the entire source folder and place the zip file in to some location, while zipping I need to skip one specific folder. But when am trying with below its not working. I need to have only one zip file without specific folder in that.
Could anyone please suggest a way on this.
Anyone know why “test-path $file -pathtype leaf” doesn’t always work in this case? (I also tried $myfile.) I’m trying “ls -r foo | .\myzip foo.zip”. EDIT: “$file | test-path -pathtype leaf” works every time. EDIT2: I see, the string version of $file wasn’t the full path. Corrected script.
# myzip.ps1
# doesn't zip empty folders
Param([parameter(ValueFromPipeline=$True)]$file,
[parameter(position=0)]$zipfile)
Begin {
if (test-path $zipfile) {
'zipfile exists'
exit 1
}
Add-Type -AssemblyName 'system.io.compression.filesystem'
# update or create
$zip = [System.IO.Compression.ZipFile]::Open($zipfile,'create')
}
Process {
if ($file | test-path -pathtype leaf) {
$rel = $file | resolve-path -relative
$rel = $rel -replace '^\.\\','' # take .\ off front
$rel = $rel -replace '^\./','' # osx take ./ off front
# destination, sourcefilename, entryname, compressionlevel
[System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($zip,
$rel, $rel, 'optimal')
} elseif ($file | test-path -pathtype container) {
"$file is container"
} else {
"$file is unknown"
}
}
End {
$zip.dispose()
}