Problem :
i have found this powershell script online and i would like to run this from a batchfile. Can anyone give me the correct syntax for this script.
And if possible make it possible to input multiple directories the script wil zip.
########################################################
# out-zip.ps1
#
# Usage:
# To zip up some files:
# ls c:source*.txt | out-zip c:targetarchive.zip $_
#
# To zip up a folder:
# gi c:source | out-zip c:targetarchive.zip $_
########################################################
$path = $args[0]
$files = $input
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)
$files | foreach {$zipfile.CopyHere($_.fullname)}
So what I actually need is the cmd file that says this (preferably with extra folders)
gi c:source | out-zip c:targetarchive.zip $_
Thanks, Kim!
Solution :
I had the same problem with only a 1kb zip file being created. I found that I had do include a -noexit in the command:
powershell.exe -noexit -Command “gi c:source | C:usersKimDpcumentsWindowsPowerShellout-zip.ps1 c:targetarchive.zip”
Granted, this seems to cause some other issues when trying to use this in a batch file because it leaves powershell open. There are some other solutions out there that I’m investigating that will either just wait for a set amount of time for the “zip” to complete or that actually compares the source files with the completed zip file before exiting.
I cleaned up the PowerShell script file for you:
<#
.SYNOPSIS
Zip up files and folders
.EXAMPLE
To zip up some files:
ls c:source*.txt | out-zip.ps1 c:targetarchive.zip
.EXAMPLE
To zip up a folder:
gi c:source | out-zip c:targetarchive.zip
#>
param(
[parameter(Mandatory=$TRUE,ValueFromPipeline=$TRUE)] $files,
[parameter(Mandatory=$TRUE,position=0)] [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([System.IO.Path]::GetFullPath($path))
$files | %{$zipfile.CopyHere($_.fullname)}
Assuming out-zip.ps1 is saved to C:usersKimDpcumentsWindowsPowerShellout-zip.ps1
, below is the batch file you need.
@echo off
powershell.exe -Command "gi c:source | C:usersKimDpcumentsWindowsPowerShellout-zip.ps1 c:targetarchive.zip"