使用 Powershell 删除文件夹中多个 csv 文件中的空行

Delete empty rows in Multiple csv files in a Folder by using Powershell

使用 PowerShell 删除文件夹中多个 CSV 文件中的空行

这是我正在尝试的代码,但它正在写入空文件。不确定这段代码有什么问题。任何建议,将不胜感激。谢谢。

$dest= "C:\Test"
Get-ChildItem $dest *.csv | ForEach-Object {
Get-Content $_.FullName | Where { $_.Replace(",","").trim() -ne "" }  | Out-File $_.FullName
}

此行为是预期的,如果读取和写入同一文件,Get-ContentOut-File 不应共享同一管道。或者,用括号将 Get-Content 括起来,或者将内容存储在变量中,然后写入文件。

$dest = "C:\Test"
Get-ChildItem $dest *.csv | ForEach-Object {
    (Get-Content $_.FullName) | Where { $_.Replace(",","").trim() -ne "" } |
    Out-File $_.FullName
}

# OR

$dest = "C:\Test"
Get-ChildItem $dest *.csv | ForEach-Object {
    (Get-Content $_.FullName | Where { $_.Replace(",","").trim() -ne "" }) |
    Out-File $_.FullName
}

# OR

$dest = "C:\Test"
Get-ChildItem $dest *.csv | ForEach-Object {
    $content = Get-Content $_.FullName | Where { $_.Replace(",","").trim() -ne "" }
    Out-File -InputObject $content -FilePath $_.FullName
}