Jump to content
Sign in to follow this  
atnextc

Trouble With FileWrite Working Even Using File Close

Recommended Posts

atnextc

So i'm testing this scenario to put into a bigger script.  Basically I have an issue where, i have a string i'm evaluating 3 times in this example, and its being written to a file.  Then i'm looping it to say if the string matches what's in the file that was written to, to close another file.  Bottom line is, my MSGbox testing seems to be working correctly, but my output is produced 3 times in my file instead of only just 1 time.

Any help would be appreciated.

#include <file.au3>
#include <String.au3>

$str= "xe-5/1/0"

FileDelete("fpc.txt")
FileDelete("hardwareadd.txt")
$I = 0
Do
fileopen("fpc.txt",1)
fileopen("hardwareadd.txt",1)
$fpc = _StringBetween($str, "-", "/", 3)


If $I = 0 Then
filewrite("fpc.txt",$fpc[0])
fileclose("fpc.txt")
EndIf

if $I >=1 Then
If $fpc[0] = fileread("fpc.txt") Then
MsgBox(0,$I&" Test",$fpc[0])
fileclose("hardwareadd.txt")
EndIf
EndIf


filewrite("hardwareadd.txt", "Install FPC into slot " & $fpc[0]&@CRLF)
fileclose("hardwareadd.txt")
$I = $I +1
Until $I =3
Edited by atnextc

Share this post


Link to post
Share on other sites
kylomas

:D atnextc,

Quick glance, you are mixing handles and names in your file ops.  Use one or the other.  And file open returns a handle that you will use later to access the file.  See the Help file.

Have not looked at your logic yet.

edit: can you explain what you want to do in words, your code is giving me a headache (or maybe it's the f%&king storm blowing outside)... :D  

edit2: you are writing three lines because your file routine is inside the loop.  The fact that you close the file is not significant since you subsequently write to it using the filename (I assume that you are referring to hardwareadd.txt having three lines).

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
kylomas

atnextc,

Example of file usage...

#include <file.au3>
#include <String.au3>
#include <array.au3>

$str= "xe-5/1/0"

FileDelete("fpc.txt")
FileDelete("hardwareadd.txt")

local $hFPC = fileopen("fpc.txt",1)
if $hFPC = -1 then
    msgbox(0,'ERROR','File Open error for fpc.txt')
    Exit
endif

$fpc = _StringBetween($str, "-", "/", 3)        ;   the 4th parm is true or false, what are you trying to do?

filewrite($hFPC,$fpc[0] & ' I was written using a handle')
fileclose($hFPC)

filewrite("hardwareadd.txt", $fpc[0] & ' I was written using a filename')

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
atnextc
kylomas yes, basically i was under the assumption that since I was closing the file hardwareadd.txt, if the contents of it, "in this case 5", matched the array fpc[0] which is also 5 would not be able to be written to.  Basically i'm trying to have it look at the array value and the first time through write it to fpc.txt, then on subsequent runs, it will look to see if the value of the array matches the contents of fpc.txt if so then do nothing and by that I mean make it to where the filewrite doesn't happen to hardwareadd.txt, if not then write whatever to hardwareadd.txt.  Please let me know if this makes more sense.
 
I'll play with your example and see if its accomplishing what i'm looking for.  Thanks for the help.
 
$fpc = _StringBetween($str, "-", "/", 3)        ;   the 4th parm is true or false, what are you trying to do?
What this is doing is grabbing the what is grabbing the first number between - and / in the str which in this case is 5.  Is there a better way to grab this information?
 
 
Edited by atnextc

Share this post


Link to post
Share on other sites
atnextc

So after reading what you put, i re-read what i put and realized my problem.

Moved my filewrite, into the If statement where it belongs, now the line Install FPC into slot 5 only gets written 1 time to the file which is what I was trying to accomplish.

if $I >=1 Then
If $fpc[0] = fileread("fpc.txt") Then
MsgBox(0,$I&" Test",$fpc[0])
fileclose("hardwareadd.txt")
Else
filewrite("hardwareadd.txt", "Install FPC into slot " & $fpc[0]&@CRLF)
fileclose("hardwareadd.txt")
EndIf
EndIf
Edited by atnextc

Share this post


Link to post
Share on other sites
kylomas

Excellent!  Do you get the point about file handles vs. names?

edit: The 4TH parm for _StringBetween indicates casesense (true or false).  Not sure what you think the "3" means.

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
atnextc

yea i knew about handles vs filenames, i just didn't see where i was doing it.  In regards to string between, not sure, so i removed it in the above script and re-ran and still working, so i'll remove it from the other times it exists in my script :)  it was probably one of those, it works so leave it be kinda things.

Thanks for the help.

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  

×