我试图解压一堆包括新的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
您的脚本对ZIP文件正常工作:
Get-ChildItem $spath -include *.zip -Recurse
我不确定你为什么使用:
-include *.xlsx
因为这是排除你的档案文件。 如果你想移动 XLSX文件,那么你需要编写另一个代码块来处理文件移动。
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" } }
使用function:
假设你想提取名为“myzip.zip”的zip文件到“C:\ myFolder”目录下。
确保“C:\ myFolder”目录存在,然后运行Extract-Zip C:\myzip.zip C:\myFolder