我尝试将包括新 xlsx 文件的一堆档案解压到 TMP 文件夹中,然后处理该文件夹并将其删除。但事情就是不按我的意愿发展。
$spath = "C:\_PS\TestFolder"
$destination=”C:\TestFolder\Testzip"
Get-ChildItem $spath -include *.xlsx -Recurse | foreach-object {
# Remove existing TMP folder and create a new one
Remove-Item -Recurse -Force $destination
New-Item $destination -type directory
$archiveFile = $_.fullname | out-string -stream
"Extract this: " + $archiveFile
"To here: " + $destination
$shellApplication = new-object -com shell.application
$zipPackage = $shellApplication.NameSpace($archiveFile)
$destinationFolder = $shellApplication.NameSpace($destination)
$destinationFolder.CopyHere($zipPackage.Items())
}
它总是给我这些错误
Exception calling "NameSpace" with "1" argument(s): "Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))" At C:\_PS\FindCC.ps1:62 char:46
+ $zipPackage = $shellApplication.NameSpace <<<< ($archiveFile)
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ComMethodTargetInvocation You cannot call a method on a null-valued expression. At C:\_PS\FindCC.ps1:64 char:50
+ $destinationFolder.CopyHere($zipPackage.Items <<<< ())
+ CategoryInfo : InvalidOperation: (Items:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
答案1
您的脚本对于 ZIP 文件运行良好:
Get-ChildItem $spath -include *.zip -Recurse
我不确定你为什么要使用:
-include *.xlsx
因为这是排除您的存档文件。如果您想要移动XLSX 文件,那么您将需要编写另一段代码块来处理文件移动。
答案2
function Extract-Zip
{
param([string]$zipfilename, [string] $destination)
if(test-path($zipfilename))
{
$shellApplication = new-object -com shell.application
$zipPackage = $shellApplication.NameSpace($zipfilename)
$destinationFolder = $shellApplication.NameSpace($destination)
$destinationFolder.CopyHere($zipPackage.Items())
}
else
{
Write-Host $zipfilename "not found"
}
}
函数的使用:
假设您要将名为“myzip.zip”的 zip 文件解压到目录“C:\myFolder”。
请确保“C:\myFolder”目录存在,然后运行Extract-Zip C:\myzip.zip C:\myFolder