Search a file for a specific line Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
Ecimeric
Posts: 102
Joined: 11 Jan 2017, 02:23

Search a file for a specific line

Post by Ecimeric » 18 Jun 2021, 03:19

Code: Select all

Run, UsbTreeView.exe /R=report.txt
Loop, Read, report.txt
    ; Search for the line that is 13 lines after the line containing mountpoint "X:\",
    ; then search for the Needle.
    If InStr(A_LoopReadLine, "0x03 (SuperSpeed)")
        MsgBox, 64, , "X:\ is not running slowly."
In order to determine whether a device is running slowly (and because I cannot find a way to do so programatically), how can I search report.txt for the Needle in the line that is 13 lines after the line containing mountpoint "X:\"?
AHKStudent
Posts: 1258
Joined: 05 May 2018, 12:23

Re: Search a file for a specific line  Topic is solved

Post by AHKStudent » 18 Jun 2021, 04:13

Code: Select all

fileData =
(
abcd
tyu X:\ uyt
efg
hij
klm
nop
qrst
uv
w
x
o
l
y
z
tyu 0x03 (SuperSpeed) yut
1
2
3
4
)
FileAppend, % filedata, testline13.txt
FileRead, test, testline13.txt
loop, parse, test, `n
{
	if instr(a_loopfield, "X:\") {
		FileReadLine, line, testline13.txt, (a_index + 13)
		If InStr(line, "0x03 (SuperSpeed)") {
			MsgBox, 64, , "X:\ is not running slowly."
			break
		}
	}
}
ExitApp
User avatar
Smile_
Posts: 521
Joined: 03 May 2020, 00:51

Re: Search a file for a specific line

Post by Smile_ » 18 Jun 2021, 04:48

Or

Code: Select all

Filename := "report.txt", FoundAt := 0
Loop 
{
    FileReadLine, Line, % Filename, % A_Index
    If (Line = "") || RegExMatch(Line, ".*Mountpoint\s+:\s+\QX:\\E.*") ; Or RegExMatch(Line, "\QX:\\E")
    {
        FoundAt := A_Index
        Break
    }
}
If (FoundAt)
{
    LineAfter := FoundAt + 13
    FileReadLine, Line, % Filename, % LineAfter
    If RegExMatch(Line, "\Q0x03 (SuperSpeed)\E")
        MsgBox, 64,, % "X:\ is not running slowly."
}
Ecimeric
Posts: 102
Joined: 11 Jan 2017, 02:23

Re: Search a file for a specific line

Post by Ecimeric » 18 Jun 2021, 11:45

Thank you, both; @Smile_: I could not get your code to work, perhaps because "0x03 (SuperSpeed)" is not at the start of the line.
Post Reply

Return to “Ask For Help”