2012-10-30 8 views
0

언제 내가 아래를 실행하고 .csv 파일이없는 경우 경로가 null이라는 오류가 표시됩니다. 삭제할 항목이 없으므로 스크립트를 중지하지 않지만 Powershell이 ​​RED 캐릭터를 보여주기를 좋아하고 그 사람들이 괴물 인 것처럼 뭔가 잘못되었다고 생각하게 만듭니다!오류 메시지를 표시하지 않습니다.

어떻게 스크립트를 실행 시키지만 오류를 억제합니까?

CODE :

$files = removeallcsv 

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

하나 라이너 :

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

답변

1

당신은 If 문을 사용할 수 있습니다 :

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

당신은 본질적으로 Remove-Item에 $ null로 전달하고,이를 대신하려고

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


Remove-Item (removeallcsv) 
관련 문제