Page 1 of 1

Search a file for a specific line

Posted: 18 Jun 2021, 03:19
by Ecimeric

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:\"?

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

Posted: 18 Jun 2021, 04:13
by AHKStudent

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

Re: Search a file for a specific line

Posted: 18 Jun 2021, 04:48
by Smile_
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."
}

Re: Search a file for a specific line

Posted: 18 Jun 2021, 11:45
by Ecimeric
Thank you, both; @Smile_: I could not get your code to work, perhaps because "0x03 (SuperSpeed)" is not at the start of the line.