FileExist() / DirExist()

Auto Hotkey

FileExist() / DirExist()

Checks for the existence of a file or folder and returns its attributes.

AttributeString := FileExist(FilePattern)
AttributeString := DirExist(FilePattern)

Parameters

FilePattern

The path, filename, or file pattern to check. FilePattern is assumed to be in %A_WorkingDir% if an absolute path isn't specified.

Return Value

Both functions return an attribute string (a subset of "RASHNDOCT"):

R = READONLY
A = ARCHIVE
S = SYSTEM
H = HIDDEN
N = NORMAL
D = DIRECTORY
O = OFFLINE
C = COMPRESSED
T = TEMPORARY

FileExist returns the attribute string of the first matching file or folder, "X" if the file has no attributes (rare), or an empty string if no file or folder is found.

DirExist returns the attribute string of the first matching folder, or an empty string if no folder is found.

Remarks

FileExist searches for both files and folders, whereas DirExist searches only for folders. InStr(FileExist(P), "D") returns zero (false) if the first matching file is not a folder, even if a matching folder exists.

Unlike FileGetAttrib, FileExist supports wildcard patterns and always returns a non-empty value if a matching file exists.

Since an empty string is seen as "false", the function's return value can always be used as a quasi-boolean value. For example, the statement if FileExist("C:\My File.txt") would be true if the file exists and false otherwise.

Related

FileGetAttrib, File-loops

Examples

if FileExist("D:\")
    MsgBox, The drive exists.
if FileExist("D:\Docs\*.txt")
    MsgBox, At least one .txt file exists.
if !FileExist("C:\Temp\FlagFile.txt")
    MsgBox, The target file does not exist.
; The following example shows how to check the file for a particular attribute:
if InStr(FileExist("C:\My File.txt"), "H")
    MsgBox The file is hidden.