Jump to content

Searching a .txt file


Recommended Posts

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)

Link to comment
Share on other sites

  • Developers

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 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.
  :)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

  • Developers

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 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.
  :)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • 2 weeks later...

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.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...