簡體   English   中英

使用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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM