FileRead()

Auto Hotkey

FileRead

Reads a file's contents into a variable.

OutputVar := FileRead(Filename)
Function Example: file := FileRead(A_ScriptDir "\MyFile.txt")

Parameters

Filename

The name of the file to read, which is assumed to be in A_WorkingDir if an absolute path isn't specified.

Options

Zero or more of the following strings. Separate each option from the next with a single space or tab. For example: "`n m5000 UTF-8"

Encoding: Specify any of the encoding names accepted by FileEncoding (excluding the empty string) to use that encoding if the file lacks a UTF-8 or UTF-16 byte order mark. If omitted, it defaults to A_FileEncoding.

RAW: Specify the word RAW (case-insensitive) to read the file's content as raw binary data. This option overrides any previously specified encoding and vice versa.

m1024: If this option is omitted, the entire file is loaded unless there is insufficient memory, in which case an error message is shown and the thread exits (but Try can be used to avoid this). Otherwise, replace 1024 with a decimal or hexadecimal number of bytes. If the file is larger than this, only its leading part is loaded. Note: This might result in the last line ending in a naked carriage return (`r) rather than `r`n.

`n (a linefeed character): Replaces any/all occurrences of carriage return & linefeed (`r`n) with linefeed (`n). However, this translation reduces performance and is usually not necessary. For example, text containing `r`n is already in the right format to be added to a Gui Edit control. The following parsing loop will work correctly regardless of whether each line ends in `r`n or just `n: Loop Parse, MyFileContents, "`n", "`r".

ErrorLevel

ErrorLevel is set to 0 if the load was successful. It is set to 1 if a problem occurred such as: 1) file does not exist; 2) file is locked or inaccessible; 3) the system lacks sufficient memory to load the file.

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

Reading Binary Data

When the RAW option is used, the return value is a string containing the raw, unmodified contents of the file. As the native encoding is UTF-16, the string always contains an even number of bytes. To get the number bytes which were read (rounded up to an even number), multiply the return value of StrLen by 2.

This option is generally required for reading binary data because by default, any bytes read from file are interpreted as text and may be converted from the source file's encoding (as specified in the options or by A_FileEncoding) to the script's native encoding, UTF-16. If the data is not UTF-16 text, this conversion generally changes the data in undesired ways.

If the data contains UTF-16 text, note that many functions assume the data ends at the first binary zero (if any are present). For example, MsgBox will only show characters up to the first binary zero. However, the entire contents are still present and can be copied between variables, passed to or returned from functions, concatenated or compared with other binary strings, or be accessed by advanced methods such as NumGet().

For a demonstration of the RAW option, see Saving and Restoring the Clipboard.

Finally, FileOpen and File.RawRead or File.ReadNum may be used to read binary data without first reading the entire file into memory.

Remarks

When the goal is to load all or a large part of a file into memory, FileRead performs much better than using a file-reading loop.

A file greater than 4 GB in size will cause an exception to be thrown unless the *m option is present, in which case the leading part of the file is loaded. An exception will also be thrown if the program is unable to allocate enough memory to contain the requested amount of data.

If there is concern about using too much memory, check the file size beforehand with FileGetSize.

FileOpen provides more advanced functionality than FileRead, such as reading or writing data at a specific location in the file without reading the entire file into memory. See File Object for a list of functions.

Related

FileEncoding, FileOpen/File Object, file-reading loop, FileGetSize, FileAppend, IniRead, Sort, Download

Examples

; Example #1: Read text file into OutputVar.
MyText := FileRead("C:\My Documents\My File.txt")
; Example #2: Quickly sort the contents of a file.
Contents := FileRead("C:\Address List.txt")
if not ErrorLevel  ; Successfully loaded.
{
    Contents := Sort(Contents)
    FileDelete "C:\Address List (alphabetical).txt"
    FileAppend Contents, "C:\Address List (alphabetical).txt"
    Contents := "" ; Free the memory.
}