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
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
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. >>
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.