[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3690
  • Last Modified:

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
0
tristanpeers
Asked:
tristanpeers
1 Solution
 
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
 
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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
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

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now