簡體   English   中英

Powershell 3.0:搜索特定文件名並將名稱發送到.txt文件

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM