Jump to content
boltonebob

Finding a Word in a file from the bottom up and outputting the contents of the line that the word is on

Recommended Posts

boltonebob

This is the first time I have posted on here, but I have used this site so much in the pursuit of furthering my AutoIT knowledge. I could not find the answer to what I was looking to do and have been messing around with it for some time without success. I am hoping someone will take mercy on me and help explain this to me.

 

I have a .txt document that data is added to in the same format each time, by an AutoIT GUI and script. The contents of the text document are as this example:

 

Car 1 checked out by Ben

Car 2 checked out by Rob

Car 3 checked out by Sonja

Car 4 checked out by Emma

Car 2 returned by Rob

Car 5 checked out by Sam

Car 1 returned by Ben

Car 4 returned by Emma

 

I already have the data going in to this file, what I am trying to do is add a button to my program that when I select 'Car 5' from the drop down and then hit the Audit button, the program reads the text file from the bottom up to find Car 5 and then display the line 'Car 5 checked out by Sam'.

I am having great difficulty, I've been trying to do it with a mix of FileReadLine and Arrays. I can get bits of what I want to work but it is one big mess. I haven't managed to find a tutorial on what I am looking to do. Can someone show me a simple example of how this could be done?

 

Any help would be greatly appreciated!

Share this post


Link to post
Share on other sites
AutoBert

 

36 minutes ago, boltonebob said:

I haven't managed to find a tutorial on what I am looking to do.

read this tut: https://www.autoitscript.com/wiki/Arrays

and about for..to..next.. step in helpfile. If you need more help, post a runable (reproducer) script, which shows your issue(s).

Edited by AutoBert

Share this post


Link to post
Share on other sites
water

What's the maximum size of the text file you need to process?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
boltonebob

It will not be too big, only adding 3 lines of text a week, similar to what I have in my example. It is just a basic program as I am still learning, hence why I am on here asking for guidance.

Share this post


Link to post
Share on other sites
boltonebob
22 minutes ago, water said:

What's the maximum size of the text file you need to process?

It will not be too big, only adding 3 lines of text a week, similar to what I have in my example. It is just a basic program as I am still learning, hence why I am on here asking for guidance.

Share this post


Link to post
Share on other sites
boltonebob
28 minutes ago, AutoBert said:

 

read this tut: https://www.autoitscript.com/wiki/Arrays

and about for..to..next.. step in helpfile. If you need more help, post a runable (reproducer) script, which shows your issue(s).

Thank you, I will take a look. I've been trying all day with different guides on pieces of what I am trying to do, but nothing that actually does what I am trying to do. Somehow I am failing to link it together correctly. I feel like I am flailing around in the wind at the moment.

Share this post


Link to post
Share on other sites
water

Then I suggest something like this (untested):

#include <File.au3>
Global $aFile, $sCar = "Car 5"
FileReadToArray("FilePath of the file to read", $aFile)
For $i = $aFile[0] to 1 Step -1
    If StringLeft($aFile[$i], StringLen($sCar) + 16) = $sCar & " checked out by " Then
        ConsoleWrite($aFile[$i])
        Exitloop
    EndiF
Next

 

  • Like 1

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
AutoBert

I prefer StringInStr:

#include <File.au3>

Global $aCars, $sCar = "Car 5"
_FileReadToArray('carsharing.txt', $aCars)
$sFound = _SearchLastState($sCar)
MsgBox($MB_ICONINFORMATION, "Last Status for "&$sCar&": ", $sFound)

Func _SearchLastState($sCar)
    For $i = $aCars[0] To 1 Step -1
        If StringInStr($aCars[$i], $sCar) Then Return $aCars[$i]
    Next
EndFunc   ;==>_SearchLastState

I used carsharing.txt

Edited by AutoBert
  • Like 1

Share this post


Link to post
Share on other sites
boltonebob

Thank you both very much for the guidance. I am going to try both methods for learning purposes. I really appreciate the fact you both took the time to help me!

Share this post


Link to post
Share on other sites
water

:) 


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
boltonebob

Would you be able to guide me on one more thing with an example. I swear this is the last thing, just trying to get a good grasp on this use of Array... If I was to do similar and search from the bottom up but once the 'Car 5' is found in the string, then inspect the 15th character for either an 'I' or an 'O'. If the 15th Character is an 'I' then display 'Car 5 is In',  and if the 15th character is an 'O' then display 'Car 5 is out' (The 15th Char would always be I or O).

Example data:

Car 1 checked out by Ben

Car 2 checked out by Rob

Car 3 checked In by Sonja

Car 5 checked out by Sam

Car 2 checked In by Rob

Car 4 checked out by Sam

 

Share this post


Link to post
Share on other sites
water
#include <File.au3>
Global $aFile, $sCar = "Car 5"
FileReadToArray("FilePath of the file to read", $aFile)
For $i = $aFile[0] to 1 Step -1
    If StringLeft($aFile[$i], StringLen($sCar)) = $sCar Then
        If StringMid($aFile[$i], 15, 1) = "i" Then
            ConsoleWrite($sCar & " is In" & @CRLF)
        Else
            ConsoleWrite($sCar & " is Out" & @CRLF)
        EndIf
        Exitloop
    EndiF
Next

NB: This only works for "Carx" where x is a one-digit number. It doesn't work for "Car10" etc.

Edited by water
  • Like 1

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
jchd

Another way:

#include <String.au3>

;~ Local $sFile = "Cars In-Out.txt"
;~ Local $sText = FileRead($sFile)

; for testing purpose, say instead that the file content is:
Local $sText = _
    "Car 1 checked out by Ben" & @CRLF & _
    "Car 2 checked out by Rob" & @CRLF & _
    "Car 3 checked In by Sonja" & @CRLF & _
    "Car 5 checked out by Sam" & @CRLF & _
    "Car 2 checked In by Rob" & @CRLF & _
    "Car 4 checked out by Sam" & @CRLF & _
    "Car 2 checked out by Joe" & @CRLF & _
    "Car 1 checked in by Ben" & @CRLF & _
    "Car 123 checked Out by Sonja" & @CRLF

Local $iCarSearched = 1
Local $aFound = StringRegExp($sText, "(?ims)Car " & $iCarSearched & "\b\N*(In|Out)\N*(?!.*Car " & $iCarSearched & "\b)", 1)
If Not @error Then ConsoleWrite("Car " & $iCarSearched & " is " & _StringProper($aFound[0]) & @LF)

 

  • Like 1

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
boltonebob

Thank you again, both of you! These examples are going to allow me to progress. I really do appreciate the help on this forum and thank you both for being willing to help a novice on the right path! :)

Share this post


Link to post
Share on other sites
boltonebob
3 hours ago, jchd said:

Another way:

#include <String.au3>

;~ Local $sFile = "Cars In-Out.txt"
;~ Local $sText = FileRead($sFile)

; for testing purpose, say instead that the file content is:
Local $sText = _
    "Car 1 checked out by Ben" & @CRLF & _
    "Car 2 checked out by Rob" & @CRLF & _
    "Car 3 checked In by Sonja" & @CRLF & _
    "Car 5 checked out by Sam" & @CRLF & _
    "Car 2 checked In by Rob" & @CRLF & _
    "Car 4 checked out by Sam" & @CRLF & _
    "Car 2 checked out by Joe" & @CRLF & _
    "Car 1 checked in by Ben" & @CRLF & _
    "Car 123 checked Out by Sonja" & @CRLF

Local $iCarSearched = 1
Local $aFound = StringRegExp($sText, "(?ims)Car " & $iCarSearched & "\b\N*(In|Out)\N*(?!.*Car " & $iCarSearched & "\b)", 1)
If Not @error Then ConsoleWrite("Car " & $iCarSearched & " is " & _StringProper($aFound[0]) & @LF)

I am trying to follow along with this one and need a little advice. This is in a different format to I am used to but I am very keen to understand it. When I copy your test and run it, it runs but nothing happens. Reading through the script I don't see where I am asking it to search for a given car. Where am I instructing the script that it is searching for Car 5?

 

Share this post


Link to post
Share on other sites
mikell

Here is a similar one, maybe a little easier  :)

#include <String.au3>

;~ Local $sFile = "Cars In-Out.txt"
;~ Local $sText = FileRead($sFile)

; for testing purpose, say instead that the file content is:
Local $sText = _
    "Car 1 checked out by Ben" & @CRLF & _
    "Lorry checked out by Rob" & @CRLF & _
    "Car 3 checked In by Sonja" & @CRLF & _
    "Car 5 checked out by Sam" & @CRLF & _
    "Lorry checked In by Rob" & @CRLF & _
    "Car 4 checked out by Sam" & @CRLF & _
    "Lorry checked out by Joe" & @CRLF & _
    "Car 1 checked in by Ben" & @CRLF & _
    "Car 123 checked Out by Sonja" & @CRLF

Local $sCarSearched = "lorry"  ; "Car 1"

$sFound = StringRegExpReplace($sText, '(?is).*' & $sCarSearched & '\h\N*(In|Out).*', "$1")
Msgbox(0,"", $sCarSearched & " is " & $sFound)

 

Share this post


Link to post
Share on other sites
boltonebob

Thanks again! This community is great. I hope in the future (one day) I can contribute the way you guys do.

Share this post


Link to post
Share on other sites
water

I'm sure you soon will b able to contribute. The longer you work with AutoIt, the more questions you ask the btter you get.
Even the longest journey begins with the first step.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
mikell
47 minutes ago, water said:

the more questions you ask the btter you get.

the bitter ?  :blink:

the butter ?  :drool:

Share this post


Link to post
Share on other sites
water

"". My kyboard has a problm with th -ky. I man: e-key ;)

  • Like 1

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

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

×