Concatinating .csv files columns

Such script can be used to combine two .csv files horizontally.

hqdefault.jpg

To run it you need insert it into some .bat file and input 3 parameters into CMD (first file path, second file path and output path)

combine.bat “input1.csv” “input2.csv” “all.csv”

@echo off
setlocal
if x%3 equ x goto usage

set tmpFile=%tmp%\JoiningTxt-%random%.csv
set tmpFile2=%tmp%\JoiningTxt2-%random%.csv
set sep=;

rem Set up the first file
copy %1 %tmpFile% > NUL
shift

:loop
rem append each file in turn
if x%2 equ x goto complete
call :Append %tmpFile% %1
shift
goto loop

:Append
rem Concatenate the 2 files together. The setlocals are to allow special chars to work ! ^ etc
(
for /f “tokens=*” %%a in (%1) do (
setlocal DISABLEDELAYEDEXPANSION
set “FirstFileLine=%%a”
setlocal ENABLEDELAYEDEXPANSION
set /p SecondFileLine=
echo !FirstFileLine!!sep!!SecondFileLine!>>%tmpFile2%
endlocal
endlocal
)
)<%2
copy %tmpFile2% %1 > NUL
del %tmpFile2%
goto :EOF

:complete
copy %tmpFile% %1 > NUL
del %tmpFile%
goto end

:usage
echo Usage:
echo %0 file1In file2In file…In fileOut
echo You must have at least 2 in files
goto end

:end
endlocal

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.