我有一個包含多個 zip 檔案的檔案夾。我需要將它們全部提取到與初始檔案相同的子檔案夾中(但沒有“.zip”)為此我創建了以下腳本:
$archives = Get-ChildItem . -Filter *.zip
foreach($zip in $archives)
{
Expand-Archive $zip.FullName -DestinationPath "{$zip.BaseName}"
}
如果我運行它,當前檔案夾中的第一個檔案會被正確解壓,但對于每個后續檔案,都會創建該檔案夾,但會出現錯誤“Expand-Archive:路徑''要么不存在,要么不是有效的檔案系統路徑.” 檔案夾是空的,沒有解壓。我在每次迭代時在除錯器中檢查 $zip 物件,這似乎是正確的。
知道為什么會發生嗎?
謝謝!
uj5u.com熱心網友回復:
嘗試:
$archives = Get-ChildItem C:\temp -Filter *.zip
foreach($zip in $archives)
{
Expand-Archive $zip.FullName -DestinationPath "$($zip.DirectoryName)\$($zip.BaseName)"
}
更新
測驗包含通配符(如“[”)的名稱時,我們必須在 PowerShell 中對它們進行轉義
示例,測驗檔案夾 test[3] 是否存在且不包含轉義字符
C:\> Test-Path C:\temp\test[3]
False
C:\> Test-Path 'C:\temp\test`[2`]'
True
由于不支持通配符,我們需要修改腳本來處理像 test[2].zip 這樣的名稱
更新后的代碼將如下所示:
$archives = Get-ChildItem C:\temp -Filter *.zip
foreach($zip in $archives)
{
$destination = "$($zip.DirectoryName)\$($zip.BaseName)"
$destinationWithEscapedName = [WildcardPattern]::Escape($destination)
$zipName = [WildcardPattern]::Escape($zip.FullName)
Expand-Archive $zipName -DestinationPath $destination
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/362445.html
標籤:电源外壳
上一篇:您可以在Azure管道YAML檔案中的行內powershell腳本中使用函式嗎?
下一篇:以紅色寫入空值
