powershellcsvbatch-file

Merging multiple CSV files into one using PowerShell


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 a first row of every file. So I need to take header from the first file, but in rest of the files the first row should be skipped. I was able to find batch file which is doing exactly what I need, but I have more than 4000 csv files in a single directory and it takes more than 45 minutes to do the job.

@echo off
ECHO Set working directory
cd /d %~dp0
Deleting existing combined file
del summary.txt
setlocal ENABLEDELAYEDEXPANSION
set cnt=1
for %%i in (*.csv) do (
 if !cnt!==1 (
 for /f "delims=" %%j in ('type "%%i"') do echo %%j >> summary.txt
) else (
 for /f "skip=1 delims=" %%j in ('type "%%i"') do echo %%j >> summary.txt
 )
 set /a cnt+=1
 )

Any suggestion how to create powershell script which would be more efficient than this batch code?

Thank you.

John


Solution

  • This will append all the files together reading them one at a time:

    get-childItem "YOUR_DIRECTORY\*.txt" 
    | foreach {[System.IO.File]::AppendAllText
     ("YOUR_DESTINATION_FILE", [System.IO.File]::ReadAllText($_.FullName))}
    
    # Placed on seperate lines for readability
    

    This one will place a new line at the end of each file entry if you need it:

    get-childItem "YOUR_DIRECTORY\*.txt" | foreach
    {[System.IO.File]::AppendAllText("YOUR_DESTINATION_FILE", 
    [System.IO.File]::ReadAllText($_.FullName) + [System.Environment]::NewLine)}
    

    Skipping the first line:

    $getFirstLine = $true
    
    get-childItem "YOUR_DIRECTORY\*.txt" | foreach {
        $filePath = $_
    
        $lines =  $lines = Get-Content $filePath  
        $linesToWrite = switch($getFirstLine) {
               $true  {$lines}
               $false {$lines | Select -Skip 1}
    
        }
    
        $getFirstLine = $false
        Add-Content "YOUR_DESTINATION_FILE" $linesToWrite
        }