Powershell:写入文件-非WLINE
我有一堆文件夹名要写入文件。使用替换。我的脚本如下所示:Powershell:写入文件-非WLINE,powershell,replace,output,Powershell,Replace,Output,我有一堆文件夹名要写入文件。使用替换。我的脚本如下所示: $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 } 我想在关键字Begin之后将文件夹写在一行中,用逗号分隔。如何格式化此文件 试试这个: $file = "C:\Temp\out.txt"
$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
}
我想在关键字Begin
之后将文件夹写在一行中,用逗号分隔。如何格式化此文件 试试这个:
$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
您可能必须在
where object
中添加扩展筛选器,因为PSIsContainer
对于存档文件可能计算为true
。谢谢。稍加修改后,join完成了job@user2531765直到现在我才注意到,您想在“开始”下面写新行,并保留旧条目。不管怎样,我很高兴它有帮助:)。