Jump to content

Continue looping in text file until no more match found


Recommended Posts

Hi Experts,

I have my below code (from my old backup) that will get the searched line from the inputbox(), the problem is, it only search the first line and it will not continue until no more match found. See below example.

Example:

Input is "190"

In my database "Tracking.txt"
190, 123, 456, 789
123, 456, 789, 190
456, 789, 190, 123
...
until done searching...

#include <File.au3>

Dim $array

$file = @ScriptDir & "\Tracking.txt"
$find = InputBox('What To Find', 'Type in below what to search for.' & @CRLF & _
        'Lines that match will be shown.', 'Text To Match', '', 200, 140)
$found = 0
$lines = ''

If $file <> '' And $find <> '' Then
    _FileReadToArray($file, $array)
    For $i = 1 To UBound($array) - 1
        If StringInStr($array[$i], $find) Then
            $array[$i] = ''
            $found += 1
            $lines &= $i & ', '
        EndIf
    Next
    MsgBox(64, 'Done', 'Total lines found = ' & $found & @CRLF & _
            'Searched Line(s)= ' & FileReadLine($file, $lines))
Else
    MsgBox(48, 'Error', 'Empty string searched!')
EndIf

 

Thanks in advance experts!

KS15

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Link to comment
Share on other sites

Im not sure what you mean by " it will not continue until no more match found " because for a text such as that one, the script says it has found all 3 lines. So it found all and when there were no more lines it stopped. Maybe i misunderstood the issue?

Maybe this is what you're looking for

#include <File.au3>

Dim $array

$file = @ScriptDir & "\Tracking.txt"
$find = InputBox('What To Find', 'Type in below what to search for.' & @CRLF & _
        'Lines that match will be shown.', 'Text To Match', '', 200, 140)
$found = 0
$lines = ''

If $file <> '' And $find <> '' Then
    _FileReadToArray($file, $array)
    For $i = 1 To UBound($array) - 1
        If StringInStr($array[$i], $find) Then
            $array[$i] = ''
            $found += 1
            $lines &= FileReadLine($file, $i)&@CRLF
        EndIf
    Next
    MsgBox(64, 'Done', 'Total lines found = ' & $found & @CRLF & _
            'Searched Line(s)= ' & $lines)
Else
    MsgBox(48, 'Error', 'Empty string searched!')
EndIf

 

Edited by careca
Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

@careca, thanks for the response and it is indeed showing the whole line based on the input.:lol: Thank you for your help. Cheers!!!

 

26 minutes ago, careca said:

Im not sure what you mean by " it will not continue until no more match found " because for a text such as that one, the script says it has found all 3 lines

Yup, it will show all the lines but it won't able to show all the lines that has the same on what was the input was. I need what you just gave me like the below output. Thanks!

image.png.5a8963957acdcbeb972bd796a8510e39.png

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Link to comment
Share on other sites

You could use _ArrayFindAll for example:

#include <Array.au3>
#include <File.au3>

Global $sFilePath = @ScriptDir & "\Tracking.txt"
If FileExists($sFilePath) = 0 Then Exit MsgBox(16, "Error", $sFilePath & " was not found")
Global $sSearch = InputBox('What To Find', 'Type in below what to search for.' & @CRLF & _
        'Lines that match will be shown.', 'Text To Match', '', 200, 140)
Global $sError = ""
Switch @error
    Case 1
        $sError = "The Cancel button was pushed."
    Case 2
        $sError = "The Timeout time was reached."
    Case 3
        $sError = "The InputBox failed to open. This is usually caused by bad arguments."
    Case 4
        $sError = "The InputBox cannot be displayed on any monitor."
    Case 5
        $sError = "Invalid parameters width without height or left without top."
EndSwitch
If $sSearch = "" Or $sError <> "" Then Exit MsgBox(48, "Error", "No Search Criteria")

Global $aTracking
_FileReadToArray($sFilePath, $aTracking)
Switch @error
    Case 1
        $sError = "Error opening specified file"
    Case 2
        $sError = "Unable to split the file"
    Case 3
        $sError = "File lines have different numbers of fields (only if $FRTA_INTARRAYS flag not set)"
    Case 4
        $sError = "No delimiters found (only if $FRTA_INTARRAYS flag not set)"
EndSwitch
If $sError <> "" Then Exit MsgBox(48, "Error", $sError)

Global $aSearch = _ArrayFindAll($aTracking, $sSearch, 0, 0, 0, 1)
Switch @error
    Case 1
        $sError = "$aArray is not an array"
    Case 2
        $sError = "$aArray is not a 1D or 2D array"
    Case 3
        $sError = "$aArray is empty"
    Case 4
        $sError = "$iStart is greater than $iEnd"
    Case 5
        $sError = "Array not 2D and $bRow set True"
    Case 6
        $sError = "$vValue was not found in array"
EndSwitch
If $sError <> "" Then Exit MsgBox(48, "Error", $sError)

_ArrayColInsert($aSearch, 1)
For $i = 0 To UBound($aSearch) - 1
    $aSearch[$i][1] = $aTracking[$aSearch[$i][0]]
Next

_ArrayDisplay($aSearch)
MsgBox(64, 'Done', 'Total lines found = ' & UBound($aSearch) - 1 & @CRLF & _
            'Searched Line(s)= ' & @CRLF & _ArrayToString($aSearch, @CRLF, -1, -1, @CRLF, 1, 1))

 

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