eidolon74 Posted July 8, 2004 Posted July 8, 2004 I want to search a txt file which is create for a value represented by $variable1. My thought was to have the program read the file line by line, and compare each line to see if the line read ($line) is not equal to $variable1. If they are not equal I want an error message to run. I think my loop is wrong or something it keeps reading just the first line of the text file. Any help is greatly appreciated. $file = FileOpen("c:\hl7wis\names.txt", 0) ; Check if file opened for reading OK If $file = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf While 1 $line = FileReadLine($file) MsgBox(0, $line, $variable1) If $line<>$variable1 Then ExitLoop MsgBox(0, "Error", "The Database name entered is not valid") Wend wend FileClose($file)
Developers Jos Posted July 8, 2004 Developers Posted July 8, 2004 (edited) What is the value of $VARIABLE1 ? $FILE = FileOpen("c:\hl7wis\names.txt", 0) ; Check if file opened for reading OK If $FILE = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf While 1 $LINE = FileReadLine($FILE) if @error then ExitLoop; reached end-of-file MsgBox(0, $LINE, $VARIABLE1) If $LINE <> $VARIABLE1 Then MsgBox(0, "Error", "The Database name entered is not valid") EndIf Wend FileClose($FILE) Edited July 8, 2004 by JdeB 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.
eidolon74 Posted July 8, 2004 Author Posted July 8, 2004 This is where $variable1 is derived. $bLoop = 1 While $bLoop = 1 global $variable1 = InputBox ( 'HL7 Interface Installer', "Please input the name of the Wisdom/32 Database", '') If $variable1='' Then MsgBox(4096, "Error", "You must enter a valid database name.") exit endif RunWait ( 'c:\hl7wis\datachk.bat ' & $variable1,'',@SW_HIDE) If @error = 1 Then MsgBox(4096, "HL7 Interface Installer", "Exit?") Exit
eidolon74 Posted July 8, 2004 Author Posted July 8, 2004 Here is some more details. Any help is appreciated. $bLoop = 1 While $bLoop = 1 global $variable1 = InputBox ( 'HL7 Interface Installer', "Please input the name of the Wisdom/32 Database", '') If $variable1='' Then MsgBox(4096, "Error", "You must enter a valid database name.") exit endif RunWait ( 'c:\hl7wis\datachk.bat ' & $variable1,'',@SW_HIDE) If @error = 1 Then MsgBox(4096, "HL7 Interface Installer", "Exit?") Exit endif $file = FileOpen("c:\hl7wis\names.txt", 0) ; Check if file opened for reading OK If $file = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf While 1 $line = FileReadLine($file) ;MsgBox(0, $line, $variable1) If $line=$variable1 Then ExitLoop If @error = -1 Then ExitLoop wend wend MsgBox(0, "Error", "The Database name entered is valid") FileClose($file)
Developers Jos Posted July 8, 2004 Developers Posted July 8, 2004 (edited) not sure if this is what you are looking for but it migth give some ideas: While 1 Global $VARIABLE1 = InputBox('HL7 Interface Installer', "Please input the name of the Wisdom/32 Database", '') If $VARIABLE1 = '' Then MsgBox(4096, "Error", "You must enter a valid database name.") ContinueLoop; goback to top EndIf RunWait('c:\hl7wis\datachk.bat ' & $VARIABLE1, '', @SW_HIDE) If @error = 1 Then $ans=MsgBox(4100, "HL7 Interface Installer", "Exit?") if $ans=6 then Exit; exit on Yes ContinueLoop ; goback to top EndIf $FILE = FileOpen("c:\hl7wis\names.txt", 0) ; Check if file opened for reading OK If $FILE = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf While 1 $LINE = FileReadLine($FILE) If @error Then; end of file test , must be right after filereadline FileClose($FILE) ExitLoop; goback to top EndIf ;MsgBox(0, $line, $variable1) If $LINE = $VARIABLE1 Then ExitLoop 2; exit both loops Wend Wend MsgBox(0, "ok", "The Database name entered is valid") EDIT: Changed last ContinueLoop to ExitLoop Edited July 8, 2004 by JdeB 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.
WLGades Posted July 8, 2004 Posted July 8, 2004 Just looking at your first post, the reason why it only exits after one line is you're only checking the first line. You have If $line<>$variable1 Then ExitLoop which just checks if the first line equals the line you're searching for, then exits. What you probably want is If @error = -1 Then ExitLoop which exits when you've hit EOF. Check the FileReadLine function for more info.
eidolon74 Posted July 9, 2004 Author Posted July 9, 2004 I see that, but I need some way to verify is the line which has been read is the value I am looking for. Theoretically, if it gets to the end of the file without finding a matching value then I want it to display an error.
Davman Posted July 22, 2004 Posted July 22, 2004 So, when it DOES find a matching value: $foundval = 1 and If @error = -1 Then If $foundval = 1 Then ;We did find Else ;We didnt find, throw a msgbox error up EndIf Sitting comfortably behind the code.
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