Solved Merging two txt files using batch script

March 13, 2020 at 05:06:09
Specs: Windows 10, 4GB RAM
Hi guys, am back again with a question.

Let's say i have a text file named test.txt containing the following words

Banana
Book
Duvet

And another text file named test2.txt containing the following words

Fruit
Stationery
Bedding

How do i merge this two file into another file call it merge.txt and to look as so,

Banana ->Fruit
Book ->Stationery
Duvet ->Bedding


See More: Merging two txt files using batch script

Reply ↓  Report •

#1
March 13, 2020 at 12:26:20
✔ Best Answer
Just reading each file line by line and adding them into array in order to access each line by its index

@echo off
Title Merging two txt files using batch script
Set /a "C1=0"
Set /a "C2=0"
Set "OutPutFile=%~dp0merge.txt"
If Exist "%OutPutFile%" Del "%OutPutFile%"
Setlocal EnableDelayedExpansion

@for /f "tokens=* delims=" %%a in ('Type "test1.txt"') Do (
	Set /a C1+=1
	Set "ONE[!C1!]=%%a"
)

@for /f "tokens=* delims=" %%a in ('Type "test2.txt"') Do (
	Set /a C2+=1
	Set "TWO[!C2!]=%%a"
)

Cls & color 0B
@For /L %%i in (1,1,%C1%) Do (
	echo !ONE[%%i]! -^> !TWO[%%i]!
	echo !ONE[%%i]! -^> !TWO[%%i]!>>"%OutPutFile%"
)
Timeout /T 1 /nobreak>nul & Start "" "%OutPutFile%"


Reply ↓  Report •

#2
March 15, 2020 at 21:42:06
Thanks @Hackoo worked perfectly

Reply ↓  Report •
Related Solutions


Ask Question