FileReadLine

AutoHotkey

FileReadLine

Reads the specified line from a file and stores the text in a variable.

FileReadLine, OutputVar, Filename, LineNum

Parameters

OutputVar

The name of the variable in which to store the retrieved text.

Filename

The name of the file to access, which is assumed to be in %A_WorkingDir% if an absolute path isn't specified. Windows and Unix formats are supported; that is, the file's lines may end in either carriage return and linefeed (`r`n) or just linefeed (`n).

LineNum

Which line to read (1 is the first, 2 the second, and so on). This can be an expression.

If the specified line number is greater than the number of lines in the file, ErrorLevel is set to 1 and OutputVar is not changed. This also happens when the specified line number is the last line in the file but that line is blank and does not end in a newline/CRLF.

ErrorLevel

[v1.1.04+]: This command is able to throw an exception on failure. For more information, see Runtime Errors.

ErrorLevel is set to 0 upon success. Otherwise it is set to 1 and the original contents of OutputVar are not changed.

A_LastError is set to the result of the operating system's GetLastError() function.

Remarks

It is strongly recommended to use this command only for small files, or in cases where only a single line of text is needed. To scan and process a large number of lines (one by one), use a file-reading loop for best performance. To read an entire file into a variable, use FileRead.

Although any leading and trailing tabs and spaces present in the line will be written to OutputVar, the linefeed character (`n) at the end of the line will not. Tabs and spaces can be trimmed from both ends of any variable by assigning it to itself while AutoTrim is on (the default). For example: MyLine = %MyLine%.

Lines up to 65,534 characters long can be read. If the length of a line exceeds this, the remaining characters cannot be retrieved by this command (use FileRead or a file-reading loop instead).

Related

FileOpen/File.ReadLine(), FileRead, FileAppend, File-reading loop, IniRead

Example

Loop
{
    FileReadLine, line, C:\My Documents\ContactList.txt, %A_Index%
    if ErrorLevel
        break
    MsgBox, 4, , Line #%A_Index% is "%line%".  Continue?
    IfMsgBox, No
        return
}
MsgBox, The end of the file has been reached or there was a problem.
return