AutoIt FileOpen Function

The FileOpen function is used in combination with FileRead and FileWrite to open files for reading, and writing.

3098 views

Edited: 2017-03-26 15:15

The FileOpen Function of AutoIt is used in conjunction with other functions, such as the FileRead or FileWrite functions.

This function opens a file for reading or writing.

The file handle must be closed with the FileClose Function.

Returns -1 on failure, and returns a file handle on success.

Parameters

Filename and PathThe path and filename for the file to be opened.
Mode OptionalThe mode can be a combination of the following:
  • 0 Read mode (default)
  • 1 Write mode (append to end of file)
  • 2 Write mode (erase previous contents. I.e. Overwrites a file)
  • 8 Creates a directory structure if it does not exist.
  • 16 Force binary mode
  • 32 Use Unicode UTF-16 Little-endian. Reading will not override existing byte order mark.
  • 64 Use Unicode UTF-16 Big-endian. Reading will not override existing byte order mark.
  • 128 Use Unicode UTF-8 (with BOM). Reading will not override existing byte order mark.
  • 256 Use Unicode UTF-8 (without BOM).
  • 16384 When opened for reading and no BOM is present, use full file UTF-8 detection.

AutoIt FileOpen Example

In this example a mode value of 10 is used, (2+8) – erases previous content and creates directory.

$file = FileOpen("test.txt", 10)

FileWrite($file, "Line1")
FileWrite($file, "Still Line1" & @CRLF) ; The CRLF at the end is a line break
FileWrite($file, "Line2")

FileClose($file)

Tell us what you think:

  1. How to declare and work with variables in AutoIt, as well as some background information.
  2. Everything you need to know about working with minimized windows.
  3. How to set the request headers when performing HTTP requests.
  4. This Tutorial will focus on post requests in AutoIt, using the Winhttprequest.5.1 object.
  5. How to use while, for, and do until loops, and how to loop through arrays and object in AutoIt.

More in: AutoIt Tutorials