uteotw Posted November 17, 2005 Posted November 17, 2005 Hi Super programming and AutoIt n00b here. I have 2 files with 3 lines each (as an example) but there could morelines in any of the files. I want to read: file1 line1, then read file2 line1 / line 2 / line 3, then file1 line2, then file2 line1 / line2 / line3, then file1 line3, then file2 line1 / line2 / line3. Here is what I wrote: $file1 = FileOpen("file1.txt", 0) If $file1 = -1 Then MsgBox(0, "Error", "Unable to open file1.") Exit EndIf $file2 = FileOpen("file2.txt", 0) If $file2 = -1 Then MsgBox(0, "Error", "Unable to open file2.") Exit EndIf While 1 $file1line = FileReadLine($file1) If @error = -1 Then ExitLoop MsgBox(0, "File 1 Line Read is:", $file1line) While 1 $file2line = FileReadLine($file2) If @error = -1 Then ExitLoop MsgBox(0, "File 2 Line Read is:", $file2line) WEnd Wend FileClose($file1) FileClose($file2) But what this does is: read file1 line1, then file2 line1 / line2 / line 3, then file1 line2, then file1 line3. It does not loop through file2 again? I don't know what I'm missing? Anyone can help? Thanks uteotw
Developers Jos Posted November 17, 2005 Developers Posted November 17, 2005 ;IsString(Number("A")) $file1 = FileOpen("file1.txt", 0) If $file1 = -1 Then MsgBox(0, "Error", "Unable to open file1.") Exit EndIf While 1 $file1line = FileReadLine($file1) If @error = -1 Then ExitLoop MsgBox(0, "File 1 Line Read is:", $file1line) $file2 = FileOpen("file2.txt", 0) If $file2 = -1 Then MsgBox(0, "Error", "Unable to open file2.") Exit EndIf While 1 $file2line = FileReadLine($file2) If @error = -1 Then ExitLoop MsgBox(0, "File 2 Line Read is:", $file2line) WEnd FileClose($file2) WEnd FileClose($file1) SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past.
BigDod Posted November 17, 2005 Posted November 17, 2005 Try $file1 = FileOpen("file1.txt", 0) If $file1 = -1 Then MsgBox(0, "Error", "Unable to open file1.") Exit EndIf $file2 = FileOpen("file2.txt", 0) If $file2 = -1 Then MsgBox(0, "Error", "Unable to open file2.") Exit EndIf While 1 $file1line = FileReadLine($file1) If @error = -1 Then ExitLoop MsgBox(0, "File 1 Line Read is:", $file1line) WEnd While 1 $file2line = FileReadLine($file2) If @error = -1 Then ExitLoop MsgBox(0, "File 2 Line Read is:", $file2line) WEnd FileClose($file1) FileClose($file2) Time you enjoyed wasting is not wasted time ......T.S. Elliot Suspense is worse than disappointment................Robert Burns God help the man who won't help himself, because no-one else will...........My Grandmother
uteotw Posted November 17, 2005 Author Posted November 17, 2005 And the winner is JdeB, your script works perfectly. I understand now that I should have included the file2open/file2close inside the first (file1) while loop. I get the concept now. These local, global variable are messing my mind up, but I'm getting there. Thanks BigDod too, but your script but it reads each file completly one after the othe which isn't what I tried to achieve. But your help is much appriciated. Thanks uteotw
herewasplato Posted November 17, 2005 Posted November 17, 2005 (edited) If you use arrays, you won't have to read the file more than once. What are you doing with these file reads, comparing data? Edit: The humor in my original version of this post might not come thru.... so I removed it... the humor that is. Edited November 17, 2005 by herewasplato [size="1"][font="Arial"].[u].[/u][/font][/size]
uteotw Posted November 18, 2005 Author Posted November 18, 2005 (edited) If you use arrays, you won't have to read the file more than once.What are you doing with these file reads, comparing data?Edit: The humor in my original version of this post might not come thru.... so I removed it... the humor that is.I'm sure you can crack a joke...Yes, I compare data. I'm not sure on how to load the file as an Array. I understand it would avoid reading the file at each loop but I'm not clear yet on how to get all the file2 lines into an array and then read from that array... n00b alert!My script is working now, the files it deal with are really small so it's fine for the moment but if anyone can show me how to get them into an Array I wouldn't mind.tauteotw Edited November 18, 2005 by uteotw
herewasplato Posted November 18, 2005 Posted November 18, 2005 (edited) c:\temp\file1.txt contains this: line 1 line 2 line 3 c:\temp\file2.txt contains this: file 2 line 1 file 2 line 2 file 2 line 3 Take a look at the help file for FileRead. You can read the entire file into one variable with one line of code: $file1txt = FileRead("c:\temp\file1.txt", FileGetSize("c:\temp\file1.txt")) After running that line above, the variable named $file1txt contains this: line 1 line 2 line 3 (carriage returns and all) Take a look at the help file for StringSplit. You can turn $file1txt into an array with one line of code: $file1txtARRAY = StringSplit($file1txt, @CRLF, 1) After running that line above, you have data like rows in a spreadsheet: $file1txtARRAY[0] = 3 $file1txtARRAY[1] = line 1 $file1txtARRAY[2] = line 2 $file1txtARRAY[3] = line 3 The rest is magic: $file1 = FileOpen("c:\temp\file1.txt", 0) If $file1 = -1 Then MsgBox(0, "Error", "Unable to open file1.") Exit EndIf $file2 = FileOpen("c:\temp\file2.txt", 0) If $file2 = -1 Then MsgBox(0, "Error", "Unable to open file2.") Exit EndIf $file1txt = FileRead("c:\temp\file1.txt", FileGetSize("c:\temp\file1.txt")) $file2txt = FileRead("c:\temp\file2.txt", FileGetSize("c:\temp\file2.txt")) $file1txtARRAY = StringSplit($file1txt, @CRLF, 1) $file2txtARRAY = StringSplit($file2txt, @CRLF, 1) For $i = 1 To $file1txtARRAY[0] MsgBox(0, "AutoIt", _ "Value of file1txtARRAY[" & _ $i & "] is " & @CRLF & _ $file1txtARRAY[$i]) For $ii = 1 To $file2txtARRAY[0] MsgBox(0, "AutoIt", _ "Value of file2txtARRAY[" & _ $ii & "] is " & @CRLF & _ $file2txtARRAY[$ii]) Next Next FileClose($file1) FileClose($file2)I assume that you have your code to compare the two arrays... if not, post back. hope this helps... As for the humor, it was something like: Now for your next leap - arrays - ..... It just did not sound all that funny after a reread and it might insult by implying that the concept of arrays would be a leap for you. I had to be overly cautious in several e-mails at work today I guess that the same caution spilled over into the forum. Edit: Does uteotw = Until the End of the World? Edited November 18, 2005 by herewasplato [size="1"][font="Arial"].[u].[/u][/font][/size]
uteotw Posted November 18, 2005 Author Posted November 18, 2005 Wow that's cool... thanks for your help. I'll tinker with this. So far the concept hasn't leaped into me just yet So it does look like magic to me. My learning process is pretty much: monkey see, monkey do, monkey tries to understand. And you're correct, uteotw stands for Until the End of the World.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now