简体   繁体   English

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

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

Delete empty rows in Multiple CSV files in a Folder by using PowerShell使用 PowerShell 删除文件夹中多个 CSV 文件中的空行

This is the code I am trying, But it is writing empty files.这是我正在尝试的代码,但它正在写入空文件。 Not Sure What's wrong with this code.不确定这段代码有什么问题。 Any suggestions would be appreciated.任何建议,将不胜感激。 thank you.谢谢。

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

This behavior is expected, Get-Content and Out-File should not be sharing the same pipeline if reading and writing to the same file.此行为是预期的,如果读取和写入同一文件,则Get-ContentOut-File不应共享同一管道。 Either, surround Get-Content with brackets or store the content in a variable and then write the 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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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