Merging multiple CSV files into one using PowerShell

后端 未结 11 1548
慢半拍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))
    }
    
    0 讨论(0)
  • 2020-11-27 18:00

    The following batch script is very fast. It should work well as long as none of your CSV files contain tab characters, and all source CSV files have fewer than 64k lines.

    @echo off
    set "skip="
    >summary.txt (
      for %%F in (*.csv) do if defined skip (
        more +1 "%%F"
      ) else (
        more "%%F"
        set skip=1
      )
    )
    

    The reason for the restrictions is that MORE converts tabs into a series of spaces, and redirected MORE hangs at 64k lines.

    0 讨论(0)
  • 2020-11-27 18:02
    $pathin = 'c:\Folder\With\CSVs'
    $pathout = 'c:\exported.txt'
    $list = Get-ChildItem -Path $pathin | select FullName
    foreach($file in $list){
        Import-Csv -Path $file.FullName | Export-Csv -Path $pathout -Append -NoTypeInformation
    }
    
    0 讨论(0)
  • 2020-11-27 18:13

    If you're after a one-liner you can pipe each csv to an Import-Csv and then immediately pipe that to Export-Csv. This will retain the initial header row and exclude the remaining files header rows. It will also process each csv one at a time rather than loading all into memory and then dumping them into your merged csv.

    Get-ChildItem -Filter *.csv | Select-Object -ExpandProperty FullName | Import-Csv | Export-Csv .\merged\merged.csv -NoTypeInformation -Append
    
    0 讨论(0)
  • 2020-11-27 18:14

    Try this, it worked for me

    Get-Content *.csv| Add-Content output.csv
    
    0 讨论(0)
  • 2020-11-27 18:15

    Your Batch file is pretty inefficient! Try this one (you'll be surprised :)

    @echo off
    ECHO Set working directory
    cd /d %~dp0
    ECHO Deleting existing combined file
    del summary.txt
    setlocal
    for %%i in (*.csv) do set /P "header=" < "%%i" & goto continue
    :continue
    
    (
       echo %header%
       for %%i in (*.csv) do (
          for /f "usebackq skip=1 delims=" %%j in ("%%i") do echo %%j
       )
    ) > summary.txt
    

    How this is an improvement

    1. for /f ... in ('type "%%i"') requires to load and execute cmd.exe in order to execute the type command, capture its output in a temporary file and then read data from it, and this is done with each input file. for /f ... in ("%%i") directly reads data from the file.
    2. The >> redirection opens the file, appends data at end and closes the file, and this is done with each output *line*. The > redirection keeps the file open all the time.
    0 讨论(0)
提交回复
热议问题