Jump to content
Macro Express Forums

Merging 2 text files? No. of lines in text file?


antica

Recommended Posts

Hi

 

I need to do this very often and was wondering if Macro Express can "automate" this for me.

 

I have 2 text files, a.txt and b.txt and I need to merge them together. Problem is the merging is not b.txt appended to a.txt :)

 

I need the 2 files to be merged line by line.

 

Here's an example, suppose a.txt has 5 lines from a1 to a5 and b.txt has 3 lines from b1 to b3.

 

The output file (can be any name) should look like this,

 

a1

b1

a2

b2

a3

b3

a4

a5

 

Can this be done with a macro?

 

Any ideas would be much appreciated.

 

Using text variables to store the 2 files would mean I have to make sure the total no. of lines in the 2 text files are 99 or less but I was hoping to bypass this limitations because there may be a need to increase the no. of text files from 2 to N files!! :o

Link to comment
Share on other sites

Hi,

Here is my humble attempt to do this. I'm not sure if it is the best way of doing it, but it works. I think it would be easy to modify it to work with more than two files.

 

Text File Begin Process: "A.txt"

Variable Modify Integer: %N1% = %N1% + 1

Text File End Process

Text File Begin Process: "B.txt"

Variable Modify Integer: %N2% = %N2% + 1

Text File End Process

If Variable %N1% > variable %N2%

Variable Modify Integer: Copy %N1% to %N3%

Else

Variable Modify Integer: Copy %N2% to %N3%

End If

Variable Modify String: Save %T10% to Text File

Repeat Start (Repeat %N3% times)

Variable Modify Integer: %N4% = %N4% + 1

If Variable %N4% <= variable %N1%

Text File Begin Process: "A.txt"

Text File End Process

Variable Modify String: Append %T1% to Text File

End If

If Variable %N4% <= variable %N2%

Text File Begin Process: "B.txt"

Text File End Process

Variable Modify String: Append %T2% to Text File

End If

Repeat End

 

 

<BTFBEG:001:000001:000000:C:\A.txt><NMVAR:01:01:1:0000001:2:0000001><BTFEND><BTFBEG:001:000001:000000:C:\B.txt><NMVAR:01:02:1:0000002:2:0000001><BTFEND><IFVAR2:5:01:4:N2><NMVAR:07:01:0:0000003:0:0000000><ELSE><NMVAR:07:02:0:0000003:0:0000000><ENDIF><TMVAR2:17:10:00:000:000:C:\merged.txtF><REP3:01:000001:000001:%N3%:0:01:><NMVAR:01:04:1:0000004:2:0000001><IFVAR2:5:04:6:N1><BTFBEG:001:N00004:000001:C:\A.txt><BTFEND><TMVAR2:20:01:00:000:000:C:\merged.txtT><ENDIF><IFVAR2:5:04:6:N2><BTFBEG:002:N00004:000001:C:\B.txt><BTFEND><TMVAR2:20:02:00:000:000:C:\merged.txtT><ENDIF><ENDREP>

Text_file_merge.mxe

Link to comment
Share on other sites

This can indeed be done with Macro Express.

 

You will need to use a Run Macro in Variable expression in order to keep the macro to a reasonable length.

 

If you aren't already somewhat familiar with how Run Macro in Variable works, I highly recommend learning about it. It's made a huge difference in the versatility of my own macros.

 

But I won't make you learn about it right now. Here is the code I would use to do what you need (below the code I have detailed exactly what this macro does so that if you wanted to break it down in order to understand what exactly is going on for use in other macros, you can. If you don't really care about why or how it works, you can just skip it and jump down to the Direct Editor text at the bottom of my post):

 

<CLEARVAR1:T:ALL><TVAR2:01:01:%HOMEDRIVE%%HOMEPATH%\Desktop\a.txt><TVAR2:02:01:%HOMEDRIVE%%HOMEPATH%\Desktop\b.txt><IVAR2:01:01:1><IVAR2:02:01:10><IVAR2:03:01:11><BTFBEG:003:000001:000000:%T1%><TVAR2:04:01:<TVAR2:%N2%:01:%T3%\s>><RUNMACVAR:4><NMVAR:01:02:1:0000002:2:0000002><BTFEND><BTFBEG:003:000001:000000:%T2%><TVAR2:04:01:<TVAR2:%N3%:01:%T3%\s>><RUNMACVAR:4><NMVAR:01:03:1:0000003:2:0000002><BTFEND><TBOX4:T:1:Center000317000278000373:000:LIST%T10%
%T11%
%T12%
%T13%
%T14%
%T15%
%T16%
%T17%
%T18%
%T19%
%T20%
%T21%
%T22%
%T23%
%T24%
%T25%
%T26%
%T27%
%T28%
%T29%>

Link to comment
Share on other sites

I've actually just come up with a slightly more elegant way of merging your two text files. This will save your merged files into a third file as it processes them. It doesn't require any Run Macro in Variable commands, so that makes it somewhat simpler, and it answers the question of "What if I don't know how many lines my a.txt and b.txt files are?" and the even more horrifying: "What? You mean I have to actually type out %T10% - %T29% or more!?!?!?!?!? That's insane!"

 

Without further ado:

 

<CLEARVAR1:T:ALL><TVAR2:01:01:%HOMEDRIVE%%HOMEPATH%\Desktop\a.txt><TVAR2:02:01:%HOMEDRIVE%%HOMEPATH%\Desktop\b.txt><TVAR2:05:01:%HOMEDRIVE%%HOMEPATH%\Desktop\c.txt><IVAR2:01:01:1><IVAR2:04:01:1><IVAR2:02:01:10><IVAR2:03:01:11><BEGCLIP><TEXTTYPE:
><ENDCLIP><TVAR2:06:03:><BTFBEG:003:N00001:000000:%T1%><TMVAR2:08:03:06:000:000:><TMVAR2:20:03:00:000:000:%T5%F><BTFBEG:003:N00004:000001:%T2%><TMVAR2:08:03:06:000:000:><TMVAR2:20:03:00:000:000:%T5%F><NMVAR:01:04:1:0000004:2:0000001><BTFEND><NMVAR:01:01:1:0000001:2:0000001><BTFEND><LAUNCHDEL2:0:01C:\Documents and Settings\Steve's Main\Desktop\c.txt>

Link to comment
Share on other sites

Wow, you guys ROCKS!!

 

Thanks for all the different versions!!

 

You guys certainly put me in a dilemma!! ( on which program to use!! :) )

 

But nonetheless, it certainly has increased my ME knowledge!!

 

Anyway, I want to thank ALL of you for the efforts.

 

REALLY REALLY APPRECIATE IT!!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...