[英]Powershell 3.0: Searching specific file names and sending the names to a .txt file
這是我的代碼:
Get-ChildItem 'R:\Source\Path' |
ForEach-Object { $_.Name -notlike '*condition*' } > 'R:\Destination\Path\File.txt'
該代碼在某種程度上可以正常工作。 除了不將文件名復制到目標位置外,而是根據條件的狀態將true或false寫入文本文件。 因此,在我希望包含.txt文件的名稱列表的地方,我卻看起來像這樣:
True
True
False
True
False
False
...等等 ...
我做錯了什么?
要過濾輸出,請使用Where-Object
而不是ForEach-Object
:
Get-ChildItem |Where-Object {$_.Name -notlike "*.zip"} > output.txt
要獲取文件的完整路徑,請使用Select-Object -ExpandProperty
:
Get-ChildItem |Where-Object {$_.Name -notlike "*.zip"} |Select-Object -ExpandProperty FullName > output.txt
或更改原始ForEach-Object -notlike
的邏輯,以實際上對-notlike
的結果采取行動 :
Get-ChildItem |ForEach-Object {
if($_.Name -notlike "*.zip"){
$_.FullName
}
} > output.txt
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.