I know that a lot has been extended to zipping files using Powershell, but I cannot find a method that does exactly what I need.
I want the zip folder AND the files in the .zip folder without the parent folder inside the zip. So, for example, I have a folder named STUFF that contains files / folders, I want to put it in the STUFF.zip folder. This folder structure will then be STUFF.zip> files / folders NOT STUFF.zip> STUFF> files / folders, as I am currently getting this code ...
function CountZipItems( [__ComObject] $zipFile) { If ($zipFile -eq $null) { Throw "Value cannot be null: zipFile" } Write-Host ("Counting items in zip file (" + $zipFile.Self.Path + ")...") [int] $count = CountZipItemsRecursive($zipFile) Write-Host ($count.ToString() + " items in zip file (" ` + $zipFile.Self.Path + ").") return $count } function CountZipItemsRecursive( [__ComObject] $parent) { If ($parent -eq $null) { Throw "Value cannot be null: parent" } [int] $count = 0 $parent.Items() | ForEach-Object { $count += 1 If ($_.IsFolder -eq $true) { $count += CountZipItemsRecursive($_.GetFolder) } } return $count } function IsFileLocked( [string] $path) { If ([string]::IsNullOrEmpty($path) -eq $true) { Throw "The path must be specified." } [bool] $fileExists = Test-Path $path If ($fileExists -eq $false) { Throw "File does not exist (" + $path + ")" } [bool] $isFileLocked = $true $file = $null Try { $file = [IO.File]::Open( $path, [IO.FileMode]::Open, [IO.FileAccess]::Read, [IO.FileShare]::None) $isFileLocked = $false } Catch [IO.IOException] { If ($_.Exception.Message.EndsWith( "it is being used by another process.") -eq $false) { Throw $_.Exception } } Finally { If ($file -ne $null) { $file.Close() } } return $isFileLocked } function GetWaitInterval( [int] $waitTime) { If ($waitTime -lt 1000) { return 100 } ElseIf ($waitTime -lt 5000) { return 1000 } Else { return 5000 } } function WaitForZipOperationToFinish( [__ComObject] $zipFile, [int] $expectedNumberOfItemsInZipFile) { If ($zipFile -eq $null) { Throw "Value cannot be null: zipFile" } ElseIf ($expectedNumberOfItemsInZipFile -lt 1) { Throw "The expected number of items in the zip file must be specified." } Write-Host -NoNewLine "Waiting for zip operation to finish..." Start-Sleep -Milliseconds 1000
Using
Remove-Item "H:\STUFF.zip" [IO.DirectoryInfo] $directory = Get-Item "H:\STUFF" ZipFolder $directory
Complete the loan here for this code. I really appreciate any help I get, this ability is critical to my project! Unfortunately, I cannot use the community extension module, because this module is not installed on the other PC on which it will be launched.
Thanks!
source share