Concatenate text in batch files

I have to write a batch file that reads lines from two separate text files and concatenates them with 4 spaces in between and then outputs  this into another text file. This has to be on a schedule so it must be able to run without human intervention . e.g.

File1.txt contains the line "ABCDEFGH"
File2.txt contains the line "123456789"

Output text file needs to look like:

ABCDEFGH~~~~123456789                 (where ~ is a space)

Thanks
tristanpeersAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

For-SoftCommented:
You can merge 2 strings in quite simple way:

SET STR1=ABCDEFGH
SET STR2=123456789
ECHO %STR1%    %STR2%>>output.txt

The bigger problem is to process 2 files in the same time.
I believe it could be necesary to use VB Script to do it. Especialy if you are going to use older than 2000 windows version.
0
DrWarezzCommented:
Try this batch file:


:----8<------------------------------ FileName.bat -----------------------------------------:
@ECHO OFF

SET FILE1=File1.txt
SET FILE2=File2.txt

SET OUTPUT=File3.txt

for /f "tokens=* delims=" %%A in (%FILE1%) do SET CONTENT1=%%A
for /f "tokens=* delims=" %%B in (%FILE2%) do SET CONETNT2=%%B

ECHO %CONTENT1%    %CONTENT2% > %OUTPUT%
:----8<------------------------------ FileName.bat -----------------------------------------:

Note, if you want to add the lines to the file (after any content that's already in the output file), then use this line:
ECHO %CONTENT1%    %CONTENT2% >>%OUTPUT%
instead of:
ECHO %CONTENT1%    %CONTENT2% > %OUTPUT%

gL,
[r.D]
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
For-SoftCommented:
Well. I believe you will get olny 1 last line concatenated as a result. It will not work because:

for /f "tokens=* delims=" %%A in (%FILE1%) do SET CONTENT1=%%A
will process the whole file and the last line of te file will be left in CONTENT1 variable.

for /f "tokens=* delims=" %%B in (%FILE2%) do SET CONETNT2=%%B
Exactly the same as above.

So this will concatenate the last line only.

It will be necesary to keep 2 files opened at the same time to do the job. I do not have any idea how to do it with the batch script.
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

SteveGTRCommented:
DrWarezz's code will work, but it's got a type:

for /f "tokens=* delims=" %%B in (%FILE2%) do SET CONTENT2=%%B

Good Luck,
Steve
0
DrWarezzCommented:
:-) Ah yes.
0
For-SoftCommented:
Ok. I thought tristanpeers wants to to merge many lines from one file with lines from another file. My mistake.
0
tristanpeersAuthor Commented:
Thanks guys for all your help. DrWarezz's solution works great... thanks again.

Tristan
0
DrWarezzCommented:
8-) ThanQ.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.