Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

read 2 text files and write to the 3rd one

Posted on 2011-10-18
9
Medium Priority
?
296 Views
Last Modified: 2012-05-12
I have 2 text files:

TextFile1.txt contains only one entry
TextFile2.txt contains multiple entries, unknown number

I would like a script (I am open to the most feasible suggestions) that will write in TextFile3.txt as follows:

TextFile_1_Entry1 TextFile2_Entry_1
TextFile_1_Entry1 TextFile2_Entry_2
TextFile_1_Entry1 TextFile2_Entry_3
TextFile_1_Entry1 TextFile2_Entry_4
.
.
.
.
TextFile_1_Entry1 TextFile2_Entry_n

Basically, in excell this can be done with the following function:
=PROPER(A21&" "&B21)

I am looking for a batch script or other form of script that I could execute in the folder which contains TextFile1 and TextFile2 , a scrip that will write the output in TextFile3 by default.

Thank you!
0
Comment
Question by:Barbulescu
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 36986732
Can you upload two sample text files? The files should closely represent the actual data.
0
 

Author Comment

by:Barbulescu
ID: 36986745
Hi

Attached you can find the sample files.

Thank you!
Temp.zip
0
 
LVL 43

Accepted Solution

by:
Saqib Husain, Syed earned 1000 total points
ID: 36986835
Try this macro

Sub combine1n2()
Open "D:\Documents and Settings\Saqib\My Documents\Downloads\Temp\Temp\TextFile1.txt" For Input As #1
Input #1, prefix
Close 1
Open "D:\Documents and Settings\Saqib\My Documents\Downloads\Temp\Temp\TextFile2.txt" For Input As #1
suffix = Input(LOF(1), 1)
suffix = Split(suffix, vbCrLf)
Close 1
For i = 0 To UBound(suffix)
suffix(i) = prefix & " " & suffix(i)
Next i
Open "D:\Documents and Settings\Saqib\My Documents\Downloads\Temp\Temp\output.txt" For Output As #1
suffix = Join(suffix, vbCrLf)
Print #1, suffix
Close 1
End Sub
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Expert Comment

by:ScriptAddict
ID: 36986931
If you need a VBS or powershell script let me know.
0
 
LVL 59

Assisted Solution

by:Bill Prew
Bill Prew earned 1000 total points
ID: 36987008
Here's a BAT approach that should work.

@echo off
if exist "output.txt" del "output.txt"
for /F "usebackq tokens=*" %%A in ("textfile1.txt") do (
  for /F "usebackq tokens=*" %%B in ("textfile2.txt") do (
    echo %%A %%B>>"output.txt"
  )
)

Open in new window

~bp
0
 
LVL 11

Expert Comment

by:ScriptAddict
ID: 36987071
Wow, so simple,  I had kind of written batch files off a while ago.  

Perhaps they are worth a second look.
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 36987096
Yes, they can be quite powerful once you get some of the basic building blocks down.

~bp
0
 

Author Closing Comment

by:Barbulescu
ID: 36987201
Thank you for your support.
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 36987372
Welcome, glad that was useful, thanks for the feedback.

~bp
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question