Jump to content

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


Recommended Posts

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!

Link to comment
Share on other sites

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

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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

:) 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

#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

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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)

 

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)

Link to comment
Share on other sites

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?

 

Link to comment
Share on other sites

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)

 

Link to comment
Share on other sites

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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

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