我是PowerShell的新手,一直試圖循環訪問CSV文件並返回每一行的列數。將該列數與第一行進行比較,發現它不相等。在這種情況下,用逗號替換逗號。然後創建一個包含更改的新文件。循環遍歷CSV文件並驗證每行的列數
$csvColumnCount = (import-csv "a CSV file" | get-member -type NoteProperty).count
$CurrentFile = Get-Content "a CSV file" |
ForEach-Object { $CurrentLineCount = import-csv "a CSV file" | get-member -type NoteProperty).count
$Line = $_
if ($csvColumnCount -ne $CurrentLineCount)
{ $Line -Replace "," , "" }
else
{ $Line } ;
$CurrentLineCount++} |
Set-Content ($CurrentFile+".out")
Copy-Item ($CurrentFile+".out") $ReplaceCSVFile
那麼有什麼不工作? – DeanOC
我認爲它不包括每行正確的列數。我正在使用的測試CSV文件的行數少於標題行列。該腳本正在刪除整個文件中的逗號。 – leo
你想測試一行中的任何屬性是空的還是缺失?然後改變這些行? – xXhRQ8sD2L7Z