Jump to content

Searching text file for variable...


Recommended Posts

Hi I was wondering if someone could check out the code below and help me out.

What I am trying to acheive is a prompt (Inputbox?) for a users first name and surname. This prompt should then go to a text file with a list of names in and check that the user exists. If the user exists (the exact first name and surname are recognised) then details should be recorded in a CSV file along with date, time etc. If the user doesn't exist (the first name and surname are not recognised in the text file) then message should inform the user of this then go back to the Inputbox prompt.

It works to some extent, but it doesn't look for an exact name match!

FileInstall ( "c:\black.jpg", "c:\", 1)

SplashImageOn ("", "c:\black.jpg", 2000, 2000, "", "", 3)

While 1
While 1
$Name = InputBox ("Name Logger v2.0", "Please enter your name in the space below", "", " M")
If @error <> 1 Then ExitLoop
WEnd
    
$Source = FileOpen ("C:\test.txt", 0)

If $Source = -1 Then
MsgBox (0, "Error", "Unable to open the file")
Exit
EndIf

While 1
$Search = FileReadLine ($Source)
If @error = -1 Then ExitLoop
If StringInStr ($Search, $Name, 1) Then
FileWriteLine ("C:\record.csv", @HOUR & ":" & @MIN & "," & @MDAY & "/" & @MON & "/" & @YEAR & "," & $Name)
Exit
EndIf
WEnd

FileClose ($Source)

MsgBox (0, "Name not found", "Please enter your name")

WEnd

SplashOff ()

Many thanks in advance

Steve

Link to comment
Share on other sites

It works to some extent, but it doesn't look for an exact name match!

StringInStr ($Search, $Name, 1) is going to return the position of the substring. The precision of the StringInStr function is dependent upon the substring you are searching for. If you specify "John Doe" in the input box, then your program will only log the name if "John Doe" exists in the same case somewhere in the input file. If there is nothing else on the line in your test.txt file other than the user's full name, then I would suggest using Richard Robertson's suggestion and replace the StringInStr with $Search = $Name.

Certifications: A+, Network+, Security+, Linux+, LPIC-1, MCSA | Languages: AutoIt, C, SQL, .NETBooks: AutoIt v3: Your Quick Guide - $7.99 - O'Reilly Media - September 2007-------->[u]AutoIt v3 Development - newbie to g33k[/u] - Coming Soon - Fate Publishing - Spring 2013UDF Libraries: SkypeCOM UDF Library | ADUC Computers OU Cleanup | Find PixelChecksumExamples: Skype COM Examples - Skype4COMLib Examples converted from VBS to AutoIt
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...