2012-10-30 37 views
0

當我運行下面的命令並且沒有.csv文件時,我得到一個錯誤,指出路徑爲空 - 這是因爲沒有什麼東西需要刪除......它不會停止腳本,但是我讓用戶認爲有什麼問題,因爲PowerShell喜歡展示紅色角色,並且讓人不知所措!壓制錯誤信息

如何讓腳本運行但抑制錯誤?

CODE:

filter removeallcsv([string]$path = '.') 
    { 
     Get-ChildItem $path | Where-Object {$_.Extension -eq ".csv"} | Foreach-Object {$_.fullName} 
    } 


Remove-Item (removeallcsv) 

回答

1

您可以使用If聲明:

$files = removeallcsv 

if ($files) { Remove-Item $files } else {"nothing to delete"} 

一個內膽:

if (($files=removeallcsv)) { Remove-Item $files } else {"nothing to delete"} 
1

你基本上傳遞$ null來Remove-Item,試試這個,而不是:

Get-ChildItem $path | Where-Object {$_.Extension -eq '.csv'} | Remove-Item