HI All,.
I have been searched up for a batch scripts solution to do the below job:
file1.txt which has strings listed as below
"12345"
"23456"
"34567"
"45678"
file2.txt
"abcde"
"bcdef"
"cdefg"
"defgh"
Now I would like to merge this two files as one file, in this file the two list of strings also merged as below
"12345" "abcde"
"23456" "bcdef"
"34567" "cdefg"
"45678" "defgh"
I know there are some other language can do this job easier than batch scripts. Can we do this in batch?
Batch file to merge two column strings from two files into one file
Moderator: DosItHelp
Re: Batch file to merge two column strings from two files into one file
Code: Select all
@echo off
setlocal enabledelayedexpansion
< file2.txt ( FOR /F "delims=" %%G IN (file1.txt) DO (
set /p file2=
echo %%G !file2!
)
)>combined.txt
Re: Batch file to merge two column strings from two files into one file
Thanks a lot! This is the simplest code ever I see for this solution. Just added the space between
Code: Select all
@echo off
setlocal enabledelayedexpansion
< file2.txt ( FOR /F "delims=" %%G IN (file1.txt) DO (
set /p file2=
echo %%G !file2!
)
)>combined.txt
Re: Batch file to merge two column strings from two files into one file
Just wanted to mention that you might get unwanted results, if your example is not representative.
In SQL terms (which is the easiest to describe a possible issue):
If you assume the line number to be the related column to join, then the the solution is a natural inner join.
If that is, what you want, then all is OK, else if "file1.txt" has fewer lines than "file2.txt", then you will lose some lines.
penpen
In SQL terms (which is the easiest to describe a possible issue):
If you assume the line number to be the related column to join, then the the solution is a natural inner join.
If that is, what you want, then all is OK, else if "file1.txt" has fewer lines than "file2.txt", then you will lose some lines.
penpen
Re: Batch file to merge two column strings from two files into one file
Thanks Penpen for your input! I have exactly number of lines in each files.