我想運行一個Powershell腳本來刪除目錄中的某些文件。該名稱必須包含blahblahblah
,因爲我可以刪除它。字符串比較沒有比較
在我的測試子目錄中,我有幾個這樣的文件名。然而,當我執行下面的代碼:
$itemsToDelete = get-childItem -path $pathName | where {$_.Name -contains blahblahblah"}
它不選擇任何項目。
我有三重檢查,以確保在執行腳本時我處於正確的目錄中。
編輯
因爲已經向我指出。我錯誤地使用了contains
。
$itemsToDelete = get-childItem -path $pathName | where {($_.Name).Contains("blahblahblah")}
正確治癒了我的困惑。
'-contains' [不像你可能認爲它應該工作](http://technet.microsoft.com/en-us/library/hh847759.aspx)。這不是你的錯 - 經營者的名字是誤導性的。無論如何,只需使用'-match'。 – 2014-10-10 15:26:21
你是對的。 '{($ _。Name).Contains(「blahblahblah」)'。這工作。謝謝。如果你想發表一個答案,我會很樂意除外。 – Johnrad 2014-10-10 15:31:10