ScriptCrafter Posted March 15, 2007 Share Posted March 15, 2007 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 Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted March 15, 2007 Moderators Share Posted March 15, 2007 (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 -ScriptCrafterWhy 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 March 15, 2007 by SmOke_N 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. Link to comment Share on other sites More sharing options...
ScriptCrafter Posted March 15, 2007 Author Share Posted March 15, 2007 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 Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted March 15, 2007 Moderators Share Posted March 15, 2007 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. 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. Link to comment Share on other sites More sharing options...
james3mg Posted March 15, 2007 Share Posted March 15, 2007 (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 March 15, 2007 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 Link to comment Share on other sites More sharing options...
ScriptCrafter Posted March 15, 2007 Author Share Posted March 15, 2007 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 Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted March 15, 2007 Moderators Share Posted March 15, 2007 (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 Can you upload a test text file? Edited March 15, 2007 by SmOke_N 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. Link to comment Share on other sites More sharing options...
SpookMeister Posted March 15, 2007 Share Posted March 15, 2007 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] Link to comment Share on other sites More sharing options...
ScriptCrafter Posted March 15, 2007 Author Share Posted March 15, 2007 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 Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted March 15, 2007 Moderators Share Posted March 15, 2007 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 -ScriptCrafterAnd the file I requested? 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. Link to comment Share on other sites More sharing options...
SadBunny Posted March 16, 2007 Share Posted March 16, 2007 Am I missing something here or isn't it obvious that the error is in:If StringInStr ($LINES, "[[" & $HSTS & "]]") ThenYou will need four extra characters in this line, which are: [$i] (not necessarily in that order ) 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. Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted March 16, 2007 Moderators Share Posted March 16, 2007 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 ) 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. 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. Link to comment Share on other sites More sharing options...
ScriptCrafter Posted March 16, 2007 Author Share Posted March 16, 2007 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 Link to comment Share on other sites More sharing options...
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