LATEST UPDATES
ShareHolder Finder v0.9.3 is now released!!! Check Product Page for more details.
✉ info@automationdevelopers.com

ITS ALL ABOUT AUTOMATION

Friday, 26 February 2016

AutoIt - Open a file for read/ write operations


Author Post Date Friday, 26 February 2016

Syntax:

FileOpen ( "filename" [, mode = 0] )

FileOpen function takes file name and optional file open mode as arguments and returns a file handle for subsequent file operations. In case of failure, it returns -1.

filename: Filename of the file to open. If the file is located in the script directory, we can simply specify only the file name. Otherwise, the full path to the file needs to be specified.
mode [optional]: $FO_READ (0) = Read mode (default)
                             $FO_APPEND (1) = Write mode (append to end of file)
                             $FO_OVERWRITE (2) = Write mode (erase previous contents)
                             $FO_CREATEPATH (8) = Create directory structure if it doesn't exist
                             $FO_BINARY (16) = Force binary mode
                             $FO_UNICODE or $FO_UTF16_LE (32) = Use Unicode UTF16 Little Endian reading and writing mode.
                             $FO_UTF16_BE (64) = Use Unicode UTF16 Big Endian reading and writing mode.
                             $FO_UTF8 (128) = Use Unicode UTF8 (with BOM) reading and writing mode.
                             $FO_UTF8_NOBOM (256) = Use Unicode UTF8 (without BOM) reading and writing mode.
                             $FO_ANSI (512) = Use ANSI reading and writing mode.
                             $FO_UTF16_LE_NOBOM (1024) = Use Unicode UTF16 Little Endian (without BOM) reading and writing mode.
                             $FO_UTF16_BE_NOBOM (2048) = Use Unicode UTF16 Big Endian (without BOM) reading and writing mode.
                             $FO_FULLFILE_DETECT (16384) = When opening for reading and no BOM is present, use the entire file to determine if it is UTF8 or UTF16. If this is not used then only the initial part of the file (up to 64KB) is checked for performance reasons.
The file will be created when opening a file in write mode, if it does not exists. The folder path must already exist (except using $FO_CREATEPATH mode).
The mode constants are defined in FileConstants.au3 and it can be a combination of above options. Eg: $FO_OVERWRITE + $FO_CREATEPATH.

The file handle must be closed with the FileClose() function.

Example:


#include <FileConstants.au3>
#include <MsgBoxConstants.au3>

;Open the file temp.txt in overwrite mode. If the folder C:\AutomationDevelopers does not exist, it will be created.
Local $hFileOpen = FileOpen("C:\AutomationDevelopers\temp.txt", $FO_OVERWRITE + $FO_CREATEPATH)

;Display a message box in case of any errors.
If $hFileOpen = -1 Then
    MsgBox($MB_SYSTEMMODAL, "", "An error occurred when opening the file.")
EndIf

;Close the handle returned by FileOpen.
FileClose($hFileOpen)

Related Posts




Interesting? Share and Let Others Know.

Post a Comment