Function Reference


FileExists

Checks if a file or directory exists.

FileExists ( "path" )

Parameters

Path The directory or file to check.

Return Value

Success: 1.
Failure: 0 if path/file does not exist.

Remarks

FileExists() returns 0 if you specify a floppy drive which does not contain a disk.

Related

FileGetAttrib, DriveStatus

Example

#include <MsgBoxConstants.au3>

Example()

Func Example()
    ; Create a constant variable in Local scope of the filepath that will be read/written to.
    Local Const $sFilePath = @TempDir & "\FileExists.txt"

    ; Create a temporary file to determine if it exists.
    If Not FileCreate($sFilePath, "This is an example of using FileExists.") Then Return MsgBox($MB_SYSTEMMODAL, "", "An error occurred whilst writing the temporary file.")

    Local $iFileExists = FileExists($sFilePath)

    ; Display a message of whether the file exists or not.
    If $iFileExists Then
        MsgBox($MB_SYSTEMMODAL, "", "The file exists." & @CRLF & "FileExist returned: " & $iFileExists)
    Else
        MsgBox($MB_SYSTEMMODAL, "", "The file doesn't exist." & @CRLF & "FileExist returned: " & $iFileExists)
    EndIf

    ; Delete the temporary file.
    FileDelete($sFilePath)
EndFunc   ;==>Example

; Create a file.
Func FileCreate($sFilePath, $sString)
    Local $fReturn = True ; Create a variable to store a boolean value.
    If FileExists($sFilePath) = 0 Then $fReturn = FileWrite($sFilePath, $sString) = 1 ; If FileWrite returned 1 this will be True otherwise False.
    Return $fReturn ; Return the boolean value of either True of False, depending on the return value of FileWrite.
EndFunc   ;==>FileCreate