简体   繁体   中英

Powershell: Write to File -NoNewLine

I have a bunch of foldernames that I want to write to a file. Using replace. My script looks like this:

$file = "C:\Temp\out.txt"
$dirs = gci "C:\Temp\dirs"

foreach ($dir in $dirs){
$d = $dir.Name
(gc $file) -replace "Begin", "Begin`r`n$d" | sc $file
}

I want to write the Folders in one Line, separated by commas, after keyword Begin . How can I format this?

Try this:

$file = "C:\Temp\out.txt"
$dirs = gci "C:\Temp\dirs" | Where-Object{$_.PSIsContainer -eq $true}

New-Item $file -ItemType "file" -Force
"Begin" | Out-File $file 

$dirs -join "," | Out-File $file -Append

You'd probably have to add extension filter in where-object , because PSIsContainer may evaluate to true for archive files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM