Merging multiple CSV files into one using PowerShell

后端 未结 11 1631
慢半拍i
慢半拍i 2020-11-27 17:47

Hello I\'m looking for powershell script which would merge all csv files in a directory into one text file (.txt) . All csv files have same header which is always stored in

11条回答
  •  悲&欢浪女
    2020-11-27 17:59

    Here is a version also using System.IO.File,

    $result = "c:\temp\result.txt"
    $csvs = get-childItem "c:\temp\*.csv" 
    #read and write CSV header
    [System.IO.File]::WriteAllLines($result,[System.IO.File]::ReadAllLines($csvs[0])[0])
    #read and append file contents minus header
    foreach ($csv in $csvs)  {
        $lines = [System.IO.File]::ReadAllLines($csv)
        [System.IO.File]::AppendAllText($result, ($lines[1..$lines.Length] | Out-String))
    }
    

提交回复
热议问题