[英]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-Content
和Out-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.