我腦子里有一個想法,我會盡力解釋我想要做什么。我有一個腳本,它基本上需要一堆檔案并將它們復制到一個新位置并重命名它們,非常簡單。隨著檔案數量的增加,滾動并檢查每一行以確保檔案復制成功變得乏味。這是我為每個檔案使用的代碼示例:
if ( $(Try { Test-Path $source.trim() } Catch { $false }) ) {
Write-Host "Source file was found, now copying."
mv -v -f $source $filedest
Write-Host "Source has been copied, moving onto the next section.!!!!!"
}
Else {
Write-Host "Source file was NOT found, moving onto the next section.XXXXX"
}
我在腳本開頭呼叫了所有變數,并使用唯一的變數名復制這種格式。所以它的作用是讓我尋找'!!!!!!' 或“XXXXX”以查看檔案是否已復制。
我想做的是讓整個檔案運行,然后在底部有一些總結,上面寫著:
成功:15
失敗次數:2
失敗檔案名:
檔案 7.csv
檔案 12.csv
uj5u.com熱心網友回復:
使用 2 個變數來跟蹤成功計數和未找到的檔案串列:
$successCount = 0
$failedFileNames = @()
# ...
if ( $(Try { Test-Path $source.trim() } Catch { $false }) ) {
Write-Host "Source file was found, now copying."
mv -v -f $source $filedest
Write-Host "Source has been copied, moving onto the next section.!!!!!"
# update success counter
$successCount
}
else {
Write-Host "Source file was NOT found, moving onto the next section.XXXXX"
# no file found, add source value to list of failed file names
$failedFileNames = $source
}
然后,當您準備好編譯報告時,只需使用Count失敗檔案名陣列的 :
Write-Host "Success: $successCount"
Write-Host "Failure: $($failedFileNames.Count)"
Write-Host "Failed file(s):"
$failedFileNames |Write-Host
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/454130.html
