Sign in to follow this  
Followers 0
NoobieAutoitUser

Read from two text files, and output one text file

24 posts in this topic

Howdy all.

I could you someones help here. I have two text files.

Show1.txt

01x01 one

01x02 one

01x03 one

Show2.txt

01x01 two

01x02 two

01x03 two

I would like to merge these two files into one file.

Output.txt

01x01 one

01x01 two

01x02 one

01x02 two

01x03 one

01x03 two

I've read through the help file. Also have been spending time in the forums. I'm not sure what to do here. I see references to put the data in an "array", but I have no clue what an "array" is, let alone how to manipulate the data in an "array".

Also how does one edit there post to put "solved" in the title. Last time I posted I could not figure out how to do that.

Again thanx for any help here.

Share this post


Link to post
Share on other sites



NoobieAutoitUser,

A good array tutorial can be found here in the Wiki.

As to your question, you can do it like this: ;)

#include <Array.au3>
#include <File.au3>

; Get the files into arrays
Global $aFile_Show1, $aFile_Show2
_FileReadToArray("Show1.txt", $aFile_Show1)
_FileReadToArray("Show2.txt", $aFile_Show2)

; And display them to show you what they look like
_ArrayDisplay($aFile_Show1)
_ArrayDisplay($aFile_Show2)

; Prepare the new array big enough to hold both files
Global $aFile_New[$aFile_Show1[0] * 2]

; Now transfer the lines
For $i = 1 To $aFile_Show1[0]
    $aFile_New[(2 * $i) - 2] = $aFile_Show1[$i]
    $aFile_New[(2 * $i) - 1] = $aFile_Show2[$i]
Next

; And here is the result

_ArrayDisplay($aFile_New)

; Which we write to disk
_FileWriteFromArray("Output.txt", $aFile_New)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

$sStr = FileRead("Show1.txt") & @CRLF & FileRead("Show2.txt")
$hFile = FileOpen(@DesktopDir & "\Result.txt", 2)
If Not @Error Then
    FileWrite($hFile, $sStr)
EndIf
FileClose($hFile)
ShellExecute(@DesktopDir & "\Result.txt")


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

@Melba23

Thanx for your reply. I checked out the link about "arrays". Wow talk about intimidating a n00bie ;) From what I gathered that is a good tutorial. I shall reference that in the future as I build more complex scripts.

Now for the bad news.

I am getting an error on the code you posted.

The error is here

"$aFile_New[(2 * $i) - 1] = $aFile_Show2[$i]"

The error message is this:

"Error: Array variable has incorrect number of subscripts or subscript dimension range exceeded."

From my limited understanding of whats going on, it looks like "$aFile_Show2" has not been "declared".

Any more input, or help will be appreciated.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Hello NoobieAutoiUser,

The variable has been declared, that error message means that assigned variable, does not have an array or does not have array correct to the size being called in the loop.

check this in your script

_FileReadToArray("Show1.txt", $aFile_Show1)

did you change "Show1.txt" to your file path?

If not you will want to change this line as well to show your path:

_FileReadToArray("Show2.txt", $aFile_Show2)

It is really difficult to know what is wrong, if you don't show your version of the script, or atleast an example of it.

Realm

Edited by Realm

My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry.  

Share this post


Link to post
Share on other sites

@Realm

#include <Array.au3>
#include <File.au3>

; Get the files into arrays
Global $aFile_Show1, $aFile_Show2
_FileReadToArray("Show1.txt", $aFile_Show1)
_FileReadToArray("Show2.txt", $aFile_Show2)

; And display them to show you what they look like
_ArrayDisplay($aFile_Show1)
_ArrayDisplay($aFile_Show2)

; Prepare the new array big enough to hold both files
Global $aFile_New[$aFile_Show1[0] * 2]

; Now transfer the lines
For $i = 1 To $aFile_Show1[0]

    $aFile_New[(2 * $i) - 2] = $aFile_Show1[$i]
    $aFile_New[(2 * $i) - 1] = $aFile_Show2[$i]
Next

; And here is the result

_ArrayDisplay($aFile_New)

; Which we write to disk
_FileWriteFromArray("Output.txt", $aFile_New)

This is the code I am using from "Melba23".

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

NoobieAutoitUser,

You need to change the paths in these 2 lines:

_FileReadToArray("Show1.txt", $aFile_Show1)

_FileReadToArray("Show2.txt", $aFile_Show2)

Change "Show1.txt" to the path were your first file is, and "Show2.txt" to the path of your second file.

Realm

Edited by Realm

My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry.  

Share this post


Link to post
Share on other sites

Updated code with the paths changed. The files are both in the same folder as each other, do I need to put them in different folders?

#include <Array.au3>
#include <File.au3>

; Get the files into arrays
Global $aFile_Show1, $aFile_Show2
_FileReadToArray("E:\Autoit\Show1.txt", $aFile_Show1)
_FileReadToArray("E:\Autoit\Show2.txt", $aFile_Show2)

; And display them to show you what they look like
_ArrayDisplay($aFile_Show1)
_ArrayDisplay($aFile_Show2)

; Prepare the new array big enough to hold both files
Global $aFile_New[$aFile_Show1[0] * 2]

; Now transfer the lines
For $i = 1 To $aFile_Show1[0]

    $aFile_New[(2 * $i) - 2] = $aFile_Show1[$i]
    $aFile_New[(2 * $i) - 1] = $aFile_Show2[$i]
Next

; And here is the result

_ArrayDisplay($aFile_New)

; Which we write to disk
_FileWriteFromArray("Output.txt", $aFile_New)

Share this post


Link to post
Share on other sites

NoobieAutoitUser,

In order for your script to properly work, you need both of those files in the path your asking it to read from, which would be at 'E:\Autoit\Show1.txt' and 'E:\Autoit\Show2.txt' for the second file.


My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry.  

Share this post


Link to post
Share on other sites

Ok both files are in same folder (they have been the whole time). Thanx for your helping in this matter.

Directory of E:\Autoit

10/18/2010  05:34 PM    <DIR>          .
10/18/2010  05:34 PM    <DIR>          ..
09/21/2010  01:13 PM    <DIR>          AutoIt-Temp
09/06/2010  12:31 PM    <DIR>          bru
10/18/2010  02:08 PM    <DIR>          Bulk_Rename
10/18/2010  05:34 PM                 0 dir.txt
10/18/2010  02:08 PM    <DIR>          Hulu
09/06/2010  12:30 PM    <DIR>          new-autoit-crap
10/18/2010  04:43 PM             1,522 Output.txt
09/06/2010  12:30 PM    <DIR>          Playlist
10/18/2010  05:02 PM               231 playlist.au3
10/18/2010  05:23 PM               726 Playlist_Maker_(MPC).au3
10/18/2010  02:09 PM               759 show1.txt <--------------------------------------------------------
10/18/2010  02:09 PM               557 show2.txt <--------------------------------------------------------
10/12/2010  05:02 PM               420 temp-playlist-maker.au3
10/12/2010  04:17 PM               545 temp.mpcpl.txt
10/18/2010  02:51 PM               347 test.au3
10/18/2010  02:09 PM               557 test.txt
10/18/2010  02:54 PM               113 working.au3
09/06/2010  12:30 PM    <DIR>          Yahoo-autoit
              11 File(s)          5,777 bytes
               9 Dir(s)   5,158,113,280 bytes free

Share this post


Link to post
Share on other sites

NoobieAutoitUser,

I am glad you got it working ;)

Thanx for your helping in this matter.

My pleasure, I had the Time!

Realm


My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry.  

Share this post


Link to post
Share on other sites

Lol, there has been a misunderstanding. The script still gives the same error. I have both the files in the same folder. I'm not understanding what I am missing here.

To recap I am still getting this error message:

The error is here

"$aFile_New[(2 * $i) - 1] = $aFile_Show2[$i]"

The error message is this:

"Error: Array variable has incorrect number of subscripts or subscript dimension range exceeded."

Any help, or insight into what I am missing will be appreciated.

The whole code is below.

#include <Array.au3>
#include <File.au3>

; Get the files into arrays
Global $aFile_Show1, $aFile_Show2
_FileReadToArray("E:\Autoit\Show1.txt", $aFile_Show1)
_FileReadToArray("E:\Autoit\Show2.txt", $aFile_Show2)

; And display them to show you what they look like
_ArrayDisplay($aFile_Show1)
_ArrayDisplay($aFile_Show2)

; Prepare the new array big enough to hold both files
Global $aFile_New[$aFile_Show1[0] * 2]

; Now transfer the lines
For $i = 1 To $aFile_Show1[0]

    $aFile_New[(2 * $i) - 2] = $aFile_Show1[$i]
    $aFile_New[(2 * $i) - 1] = $aFile_Show2[$i]
Next

; And here is the result

_ArrayDisplay($aFile_New)

; Which we write to disk
_FileWriteFromArray("Output.txt", $aFile_New)

Directory listing showing "show1.txt, and "show2.txt" are in the same folder.

Directory of E:\Autoit

10/18/2010  05:34 PM    <DIR>          .
10/18/2010  05:34 PM    <DIR>          ..
09/21/2010  01:13 PM    <DIR>          AutoIt-Temp
09/06/2010  12:31 PM    <DIR>          bru
10/18/2010  02:08 PM    <DIR>          Bulk_Rename
10/18/2010  05:34 PM                 0 dir.txt
10/18/2010  02:08 PM    <DIR>          Hulu
09/06/2010  12:30 PM    <DIR>          new-autoit-crap
10/18/2010  04:43 PM             1,522 Output.txt
09/06/2010  12:30 PM    <DIR>          Playlist
10/18/2010  05:02 PM               231 playlist.au3
10/18/2010  05:23 PM               726 Playlist_Maker_(MPC).au3
10/18/2010  02:09 PM               759 show1.txt  <---------------------
10/18/2010  02:09 PM               557 show2.txt  <---------------------
10/12/2010  05:02 PM               420 temp-playlist-maker.au3
10/12/2010  04:17 PM               545 temp.mpcpl.txt
10/18/2010  02:51 PM               347 test.au3
10/18/2010  02:09 PM               557 test.txt
10/18/2010  02:54 PM               113 working.au3
09/06/2010  12:30 PM    <DIR>          Yahoo-autoit
              11 File(s)          5,777 bytes
               9 Dir(s)   5,158,113,280 bytes free

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

NoobieAutoitUser,

Are you still getting the same error as before?

Also change this

_FileWriteFromArray("Output.txt", $aFile_New)

Change "Output.txt" to the path with which you want the new file to save.

make the path changes, try again, and let me know what errors you are getting.

Realm

Edit: sorry I overlooked, you are getting the same error, it could be something to do with your file.

Edited by Realm

My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry.  

Share this post


Link to post
Share on other sites

if a text file contains the array delimiter then it will throw everything off. I still say the simplest is to just read the two files into a single string. If he needs to do anything with the contents of the string, then you worry about using an array but so far he hasn't indicated the need for that. All he asked was to get 2 files into 1.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

NoobieAutoitUser,

Run this script, to check line count on each file, this is the only other place, I think you have an error!.

#include <File.au3>

$ct1 = _FileCountLines("E:\Autoit\Show1.txt")
$ct2 = _FileCountLines("E:\Autoit\Show2.txt")

MsgBox(0,'Line Counts','Show1.txt = ' & $ct1 ' Lines' & @CRLF & 'Show2.txt = ' & $ct2 & ' Lines')

Tell me if they match. if not which one is larger?


My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry.  

Share this post


Link to post
Share on other sites

@Realm

I am getting a error message with the script you posted. It says the error is at "line 6"

The error message is "Error in expression". Is there a way to pipe error messages to a text file?

Do the contents of "show1.txt, and "show2.txt" to have an equal amount of lines. Because they do not. One has 12 (show1.txt) entries, while the other has 7 (show2.txt) entries.

Again thanx for your help in this. I am pulling my hair out trying to understand what is wrong here.

Share this post


Link to post
Share on other sites

This is my goal:

Show1.txt

01x01 one

01x02 one

01x03 one

Show2.txt

01x01 two

01x02 two

01x03 two

I would like to merge these two files into one file.

Output.txt

01x01 one

01x01 two

01x02 one

01x02 two

01x03 one

01x03 two

See how I am trying to stagger the output? I am trying to make a play-list (can't seem to find one out there in freeware land. Plus once I tweak this, I can "up" it here for other peeps that might like it. Its for Media Player Classic).

Again thanx for your help in this matter.

Share this post


Link to post
Share on other sites

#18 ·  Posted (edited)

I like it how people write the scripts for someone with 12 posts or less. But when I asked kinda the same thing the other day I just got told what func to look at.

All you know he could of had 300+ posts and just made a new account to ask this.

People help people with less posts alot more.

_FileReadToArray

_FileWriteFromArray

_ArrayInsert

_ArrayDelete

maybe _ArrayConcatenate

Edited by hot202

Share this post


Link to post
Share on other sites

NoobieAutoitUser,

That was what I was afraid of, one of the files are shorter than the other. That would create the error you received.

Try This:

#include <Array.au3>
#include <File.au3>

; Get the files into arrays
Global $aFile_Show1, $aFile_Show2
_FileReadToArray("E:\Autoit\Show1.txt", $aFile_Show1)
_FileReadToArray("E:\Autoit\Show2.txt", $aFile_Show2)

; And display them to show you what they look like
_ArrayDisplay($aFile_Show1)
_ArrayDisplay($aFile_Show2)

; Prepare the new array big enough to hold both files
Global $aFile_New[$aFile_Show1[0]+$aFile_Show2[0]]

; Declare the final line by the larger of the 2 files.
If $aFile_Show2 > $aFile_Show1 Then
    $lastLine = $aFile_Show2[0]
Else
    $lastLine = $aFile_Show1[0]
EndIf


; Now transfer the lines
Global $new_Row = 0
For $i = 1 To $lastLine
    $new_Row +=1
    If $aFile_Show1 >= $i Then $aFile_New[$new_Row] = $aFile_Show1[$i]
    $new_Row +=1
    If $aFile_Show2 >= $i Then $aFile_New[$new_Row] = $aFile_Show2[$i]
Next

; And here is the result

_ArrayDisplay($aFile_New)

; Which we write to disk
_FileWriteFromArray("Output.txt", $aFile_New)

Don't forget to change the last line's path to where you would like it.

Realm


My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry.  

Share this post


Link to post
Share on other sites

@Realm

Thanx again for trying to help. If time, and money were not an issue I would love to take a class on how to do this ;) Moving on, I tried your updated code. I got an error on line 30. I then made both of the text files (show1.txt, and show2.txt) have the same amount of lines (24). I tried your script again, and got an error on line 30.

I went back to the original script. Tried with non-even lines, and got the same error as before. I then made both of the text files (show1.txt, and show2.txt) have the same amount of lines (24). Ran the original script (to my surprise) it worked perfectly.

After all the hell, I am happy that I have something that works. Now I need to move on to figuring out how to "tweak" the code to work with different length files. I am thinking that I need to "parse" the text files before they go into the array (I wish I would have known that having different length files would cause all this headache). Do you have any pointers on where to start on this? I can see me relying on this heavily as I watch a lot of TV stuff on my computer. I would really like to make this a "nice" program, that I can give away as freeware.

Again thanx to everyone that helped to contribute to this cause.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0