我使用下面的命令輸出目錄的內容到一個txt文件:如何使用Powershell從文本文件中刪除回車符?
$SearchPath="c:\searchpath"
$Outpath="c:\outpath"
Get-ChildItem "$SearchPath" -Recurse | where {!$_.psiscontainer} | Format-Wide -Column 1'
| Out-File "$OutPath\Contents.txt" -Encoding ASCII -Width 200
我到底是什麼了,當我做到這一點是我需要的信息的TXT文件,但它增加了許多回車我不需要,使輸出更難以閱讀。
這是什麼樣子:
c:\searchpath\directory
name of file.txt
name of another file.txt
c:\searchpath\another directory
name of some file.txt
這使得需要大量的滾動一個txt文件,但實際的信息並不多,一百行通常少了很多。
我想它看起來像:
c:\searchpath\directory
nameoffile.txt
c:\searchpath\another directory
another file.txt
這是我到目前爲止已經試過,不工作
$configFiles=get-childitem "c:\outpath\*.txt" -rec
foreach ($file in $configFiles)
{
(Get-Content $file.PSPath) |
Foreach-Object {$_ -replace "'n", ""} |
Set-Content $file.PSPath
}
我也試過「R但無論選擇離開該文件不變。
的另一種嘗試:
Select-String -Pattern "\w" -Path 'c:\outpath\contents.txt' | foreach {$_.line}'
| Set-Content -Path c:\outpath\contents2.txt
當我運行結尾沒有設置內容該字符串,它顯示的樣子,因爲我需要它在ISE,但只要我添加使用Set-Content在最後,它再次回到我不需要它們的回車。
這裏的一些有趣的事情,如果我創建了幾個回車和一些標籤的文本文件,然後如果我用我一直在使用相同的-replace腳本,但使用t to replace the tabs, it works perfect. But
r和n do not work. It's almost as though it doesn't recognize them as escape characters. But if I add
r和`n的txt文件然後運行腳本,它仍然不會取代任何東西。似乎不知道如何處理它。
工作,謝謝大家的幫助。 – user1417978