Sign in to follow this  
Followers 0
ScriptCrafter

FileReadLine woooos

13 posts in this topic

Is there a way to get FileReadLine to read the whole file line by line?

If FileReadLine ($FILE) = "[[" & $HSTS & "]]" Then

MsgBox (0, "HOST DUPLICATE ALERT", "You already are using this hostname", 1000)

This is only reading the first line of the file :-(

Any $HSTS that are further in the file are not identified as duplicate

-ScriptCrafter

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Is there a way to get FileReadLine to read the whole file line by line?

If FileReadLine ($FILE) = "[[" & $HSTS & "]]" Then

MsgBox (0, "HOST DUPLICATE ALERT", "You already are using this hostname", 1000)

This is only reading the first line of the file :-(

Any $HSTS that are further in the file are not identified as duplicate

-ScriptCrafter

Why not just use _FileReadToArray() each line will be an element in the array, and you can loop through each one and search for what you want. Edited by SmOke_N

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

With out being so obvious about my lack of coding skills :-)

How would that look, I have a hard time with arrays.

Im sure if I get some pointers with this, I can go from there

-ScriptCrafter

Share this post


Link to post
Share on other sites

Look at the example of _FileReadToArray() in the help file, replace the MsgBox() in the For/Next loop with a conditional If/Then statement with StringInStr() then put whatever conditions in there you want to apply. Try something out, if it's failing... post your honest attempt... most of us can tell when you are really trying ... and are more than willing to give you hand from that point.


[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Have you tried this type of script:

$FileToRead=FileOpen("foo.txt",0)
While 1
  $CurrentLine=FileReadLine($FileToRead)
  If @error Then ExitLoop
  If $CurrentLine = "[[" & $HSTS & "]]" Then
    MsgBox (0, "HOST DUPLICATE ALERT", "You already are using this hostname", 1000)
    ;exitloop here if you want to stop reading the file when it finds a duplicate hostname
  EndIf
WEnd
FileClose($FileToRead)
MsgBox(0,"Done","Done searching")
?

It looks like from the scrap you posted that this is what you're going for, but I can't tell if your $file variable holds a filename (which will cause it to only read the first line) or a file HANDLE created by a call to FileOpen().

SmOke's solution will probably be faster, but if this is your only problem, you can use your existing script by only adding a FileOpen() line.

Edit: Forgot to FileClose() in my snippet

Edited by james3mg

"There are 10 types of people in this world - those who can read binary, and those who can't.""We've heard that a million monkeys at a million keyboards could produce the complete works of Shakespeare; now, thanks to the Internet, we know that is not true." ~Robert Wilensky0101101 1001010 1100001 1101101 1100101 1110011 0110011 1001101 10001110000101 0000111 0001000 0001110 0001101 0010010 1010110 0100001 1101110

Share this post


Link to post
Share on other sites

DIM $LINES

$HSTS = GUICtrlRead ($HSTBOX)

$RAMS = GuiCtrlRead ($RAMBOX)

$IPCB = GUICtrlRead ($IPCBOX)

$IOSB = GUICtrlRead ($IOSBOX)

$WHOLEFILE = FileRead($FILE)

If $FILE = -1 Then

MsgBox(0, "Error", "Unable to open file.")

Exit

EndIf

_FileReadToArray ($FILE, $LINES)

For $i = 1 To $LINES[0]

If StringInStr ($LINES, "[[" & $HSTS & "]]") Then

MsgBox (0, "HOST DUPLICATE ALERT", "You already are using this hostname", 1000)

EndIF

Next

This doesnt seem to be working.

Also appears to be looping the file

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

DIM $LINES

$HSTS = GUICtrlRead ($HSTBOX)

$RAMS = GuiCtrlRead ($RAMBOX)

$IPCB = GUICtrlRead ($IPCBOX)

$IOSB = GUICtrlRead ($IOSBOX)

$WHOLEFILE = FileRead($FILE)

If $FILE = -1 Then

MsgBox(0, "Error", "Unable to open file.")

Exit

EndIf

_FileReadToArray ($FILE, $LINES)

For $i = 1 To $LINES[0]

If StringInStr ($LINES, "[[" & $HSTS & "]]") Then

MsgBox (0, "HOST DUPLICATE ALERT", "You already are using this hostname", 1000)

EndIF

Next

This doesnt seem to be working.

Also appears to be looping the file

$File has to be the literal path... don't use FileOpen (remove that).

Edit:

Wait, that confused me :whistle:

Can you upload a test text file?

Edited by SmOke_N

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

DIM $LINES

$HSTS = GUICtrlRead ($HSTBOX)

$RAMS = GuiCtrlRead ($RAMBOX)

$IPCB = GUICtrlRead ($IPCBOX)

$IOSB = GUICtrlRead ($IOSBOX)

$WHOLEFILE = FileRead($FILE)

If $FILE = -1 Then

MsgBox(0, "Error", "Unable to open file.")

Exit

EndIf

_FileReadToArray ($FILE, $LINES)

For $i = 1 To $LINES[0]

If StringInStr ($LINES, "[[" & $HSTS & "]]") Then

MsgBox (0, "HOST DUPLICATE ALERT", "You already are using this hostname", 1000)

EndIF

Next

This doesnt seem to be working.

Also appears to be looping the file

Is this your whole script? Where are you telling it what $FILE is?

Posting snippets of much larger code wont get you good answers. I recommend either posting the whole script, or making a small example script showing your issue. The example for _FileReadToArray() in the help file should give you a good starting point.


[u]Helpful tips:[/u]If you want better answers to your questions, take the time to reproduce your issue in a small "stand alone" example script whenever possible. Also, make sure you tell us 1) what you tried, 2) what you expected to happen, and 3) what happened instead.[u]Useful links:[/u]BrettF's update to LxP's "How to AutoIt" pdfValuater's Autoit 1-2-3 Download page for the latest versions of Autoit and SciTE[quote]<glyph> For example - if you came in here asking "how do I use a jackhammer" we might ask "why do you need to use a jackhammer"<glyph> If the answer to the latter question is "to knock my grandmother's head off to let out the evil spirits that gave her cancer", then maybe the problem is actually unrelated to jackhammers[/quote]

Share this post


Link to post
Share on other sites

Func BUILD ()

DIM $LINES

If $FILE = -1 Then

MsgBox(0, "Error", "Unable to open file.")

Exit

EndIf

_FileReadToArray ($FILE, $LINES)

For $i = 1 To $LINES[0]

If StringInStr ($LINES, "[[" & $HSTS & "]]") Then

MsgBox (0, "HOST DUPLICATE ALERT", "You already are using this hostname", 1000)

Else

FileWriteLine ($FILE, "[[" & $HSTS & "]]")

EndIf

Next

EndFunc

This is the function

If the hostname exists in the file, dont allow for it to be written.

If the hostname doesnt exist in the file, then allow it to be written.

For those questioning $FILE

$FILE = C:/LOCATIONOFMYFILE

-ScriptCrafter

Share this post


Link to post
Share on other sites

Func BUILD ()

DIM $LINES

If $FILE = -1 Then

MsgBox(0, "Error", "Unable to open file.")

Exit

EndIf

_FileReadToArray ($FILE, $LINES)

For $i = 1 To $LINES[0]

If StringInStr ($LINES, "[[" & $HSTS & "]]") Then

MsgBox (0, "HOST DUPLICATE ALERT", "You already are using this hostname", 1000)

Else

FileWriteLine ($FILE, "[[" & $HSTS & "]]")

EndIf

Next

EndFunc

This is the function

If the hostname exists in the file, dont allow for it to be written.

If the hostname doesnt exist in the file, then allow it to be written.

For those questioning $FILE

$FILE = C:/LOCATIONOFMYFILE

-ScriptCrafter

And the file I requested?

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

Am I missing something here or isn't it obvious that the error is in:

If StringInStr ($LINES, "[[" & $HSTS & "]]") Then

You will need four extra characters in this line, which are: [$i] (not necessarily in that order :whistle:) and come right after $LINES ? Or am I spoiling a delicate psychological experiment?


Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites

Am I missing something here or isn't it obvious that the error is in:

If StringInStr ($LINES, "[[" & $HSTS & "]]") Then

You will need four extra characters in this line, which are: [$i] (not necessarily in that order :whistle:) and come right after $LINES ? Or am I spoiling a delicate psychological experiment?

You're not missing it... I just wasn't going to answer unless more information was given... I find if someone provides all the information to help them they get up and running fully in a matter of moments rather than hours/days of the cloak and dagger approach... so I just sit back until they get fed up, or someone else chimes in and gives them all the answers with a lower expectation percentage of the information or work expected.

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

I really thank you guys, especially you Smoke N for the help.

I gave just about all the information that was needed, other then the button that loads that function.

I was able to get through it with the _FileReadToArray Help file, and some of the pointers you gave. I can agree to the not just blindly handing out answers and pointing folks in the right direction approach.

Thanks Again!

-ScriptCrafter

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