我编写了以下代码,用于将.zip文件解压缩到temp:
function Expand-ZIPFile($file, $destination)
{
$shell = new-object -com shell.application
$zip = $shell.NameSpace($file)
foreach ($item in $zip.items()) {
$shell.Namespace($destination).copyhere($item)
}
}
Expand-ZIPFile -file "*.zip" -destination "C:\temp\CAP"
但是我收到了以下错误:
PS C:\Users\v-kamoti\Desktop\CAP> function Expand-ZIPFile($file, $destination)
{
$shell = new-object -com shell.application
$zip = $shell.NameSpace($file)
foreach ($item in $zip.items()) {
$shell.Namespace($destination).copyhere($item)
}
}
Expand-ZIPFile -file "*.zip" -destination "C:\temp\CAP"
You cannot call a method on a null-valued expression.
At line:5 char:19
+ foreach($item in $zip.items())
+ ~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
答案 0 :(得分:7)
Get-ChildItem 'path to folder' -Filter *.zip | Expand-Archive -DestinationPath 'path to extract' -Force
需要ps v5
答案 1 :(得分:1)
您必须在以下调用中明确提供完整路径(没有通配符):
$shell.NameSpace($file)
您可以像这样重写您的功能:
function Expand-ZIPFile($file, $destination)
{
$files = (Get-ChildItem $file).FullName
$shell = new-object -com shell.application
$files | %{
$zip = $shell.NameSpace($_)
foreach ($item in $zip.items()) {
$shell.Namespace($destination).copyhere($item)
}
}
}
答案 2 :(得分:0)
如果要为每个zip文件创建一个新文件夹,则可以使用以下方法:
#input variables
$zipInputFolder = 'C:\Users\Temp\Desktop\Temp'
$zipOutPutFolder = 'C:\Users\Temp\Desktop\Temp\Unpack'
#start
$zipFiles = Get-ChildItem $zipInputFolder -Filter *.zip
foreach ($zipFile in $zipFiles) {
$zipOutPutFolderExtended = $zipOutPutFolder + "\" + $zipFile.BaseName
Expand-Archive -Path $zipFile.FullName -DestinationPath $zipOutPutFolderExtended
}