Simple Text-File Vertical-Merge


By: nbrane
December 15, 2013

Just take two files and paste them together side-by-side, using fixed column widths (defaults to 40). f/e, file1 (left-column), file 2 (right column), file3 (output result), and col.width (20)

FILE1:
file1-line1
file1-line2

FILE2:
file2-L1
file2-L2
file2-L3

COMMANDLINE:
ZIPPER FILE1 FILE2 FILE3 20

OUTPUT ('FILE3'):
file1-line1         file2-L1
file1-line2         file2-L2
                    file2-L3

BATCH CODE:
@echo off & setlocal enabledelayedexpansion
:: prototype: performs a "zipper" operation (vertical merge) of two files
if "%1" equ "" (
echo useage: ZIPPER left-column-file right-column-file result-file [padding]
echo 'padding' is the number of spaces you want for fixed column separation.
goto :eof
)

:: lft-hand column file
set file1=%1
:: rt-hand column file
set file2=%2
:: dest. file result
set file3=%3
:: padding between columns
set p=%4%
if not defined p set p=40
set sk=
set c=0
set pad=                                                                     .
set pad=!pad:~0,%p%!

if exist %file3% (set v=APPENDING TO) else (set v=CREATING)
echo %v% %file3%

for /f "tokens=*" %%a in (%file1%) do (
set x=%%a%pad%
set x=!x:~0,%p%!
call :xx
set /a c+=1
set sk=skip=!c!
)
for /f "%sk% tokens=*" %%a in (%file2%) do >>%file3% echo.%pad%%%a
goto :eof

:xx
for /f "%sk% tokens=*" %%b in (%file2%) do (
set out=!x!%%b
>>%file3% echo !x!%%b
goto :eof
)
>>%file3% echo !x!


Need more help?
Describe your Problem
Example: Hard Drive Not Detected on My PC

Ask Question