FileSelectFile

AutoHotkey

FileSelectFile

Displays a standard dialog that allows the user to open or save file(s).

FileSelectFile, OutputVar [, Options, RootDir\Filename, Prompt, Filter]

Parameters

OutputVar

The name of the variable in which to store the filename(s) selected by the user. This will be made blank if the user cancels the dialog (i.e. does not wish to select a file).

Options

If omitted, it will default to zero, which is the same as having none of the options below.

M: Multi-select. Specify the letter M to allow the user to select more than one file via shift-click, control-click, or other means. M may optionally be followed by a number as described below (for example, both M and M1 are valid). To extract the individual files, see the example at the bottom of this page.

S: Save button. Specify the letter S to cause the dialog to always contain a Save button instead of an Open button. S may optionally be followed by a number (or sum of numbers) as described below (for example, both S and S24 are valid).

Even if M and S are absent, the following numbers can be used. To put more than one of them into effect, add them up. For example, to use 8 and 16, specify the number 24.

1: File Must Exist
2: Path Must Exist
8: Prompt to Create New File
16: Prompt to OverWrite File
32 [v1.0.43.09+]: Shortcuts (.lnk files) are selected as-is rather than being resolved to their targets. This option also prevents navigation into a folder via a folder shortcut.

If the "Prompt to Overwrite" option is present without the "Prompt to Create" option, the dialog will contain a Save button rather than an Open button. This behavior is due to a quirk in Windows.

RootDir\Filename

If present, this parameter contains one or both of the following:

RootDir: The root (starting) directory, which is assumed to be a subfolder in %A_WorkingDir% if an absolute path is not specified. If omitted or blank, the starting directory will be a default that might depend on the OS version (it will likely be the directory most recently selected by the user during a prior use of FileSelectFile). [v1.0.43.10+]: On Windows XP/2003 and earlier, a CLSID such as ::{20d04fe0-3aea-1069-a2d8-08002b30309d} (i.e. My Computer) may also be specified, in which case any subdirectory present after the CLSID should end in a backslash (otherwise, the string after the last backslash will be interpreted as the default filename, below).

Filename: The default filename to initially show in the dialog's edit field. Only the naked filename (with no path) will be shown. To ensure that the dialog is properly shown, ensure that no illegal characters are present (such as /<|:").

Examples:

C:\My Pictures\Default Image Name.gif  ; Both RootDir and Filename are present.
C:\My Pictures  ; Only RootDir is present.
My Pictures  ; Only RootDir is present, and it's relative to the current working directory.
My File  ; Only Filename is present (but if "My File" exists as a folder, it is assumed to be RootDir).
Prompt

Text displayed in the window to instruct the user what to do. If omitted or blank, it will default to "Select File - %A_SCRIPTNAME%" (i.e. the name of the current script).

Filter

Indicates which types of files are shown by the dialog.

Example: Documents (*.txt)
Example: Audio (*.wav; *.mp2; *.mp3)

If omitted, the filter defaults to All Files (*.*). An option for Text Documents (*.txt) will also be available in the dialog's "files of type" menu.

Otherwise, the filter uses the indicated string but also provides an option for All Files (*.*) in the dialog's "files of type" drop-down list. To include more than one file extension in the filter, separate them with semicolons as illustrated in the example above.

ErrorLevel

[v1.1.04+]: This command is able to throw an exception on failure. For more information, see Runtime Errors.

ErrorLevel is set to 1 if the user dismissed the dialog without selecting a file (such as by pressing the Cancel button). It is also set to 1 if the system refused to show the dialog (rare). Otherwise, it is set to 0.

Remarks

If the user didn't select anything (e.g. pressed CANCEL), OutputVar is made blank.

If multi-select is not in effect, OutputVar is set to the full path and name of the single file chosen by the user.

If the M option (multi-select) is in effect, OutputVar is set to a list of items, each of which except the last is followed by a linefeed (`n) character. The first item in the list is the path that contains all the selected files (this path will end in a backslash only if it is a root folder such as C:\). The other items are the selected filenames (without path). For example:

C:\My Documents\New Folder [this is the path in which all the files below reside]
test1.txt [these are the naked filenames: no path info]
test2.txt
... etc.

(The example at the bottom of this page demonstrates how to extract the files one by one.)

When multi-select is in effect, the sum of the lengths of the selected filenames is limited to 64 KB. Although this is typically enough to hold several thousand files, OutputVar will be made blank if the limit is exceeded.

A GUI window may display a modal file-selection dialog by means of Gui +OwnDialogs. A modal dialog prevents the user from interacting with the GUI window until the dialog is dismissed.

Known limitation: A timer that launches during the display of a FileSelectFile dialog will postpone the effect of the user's clicks inside the dialog until after the timer finishes. To work around this, avoid using timers whose subroutines take a long time to finish, or disable all timers during the dialog:

Thread, NoTimers
FileSelectFile, OutputVar
Thread, NoTimers, false

[v1.0.25.06+]: The multi-select option "4" is obsolete. However, for compatibility with older scripts, it still functions as it did before. Specifically, if the user selects only one file, OutputVar will contain its full path and name followed by a linefeed (`n) character. If the user selects more than one file, the format is the same as that of the M option described above, except that the last item also ends in a linefeed (`n).

Related

FileSelectFolder, MsgBox, InputBox, ToolTip, GUI, CLSID List, parsing loop, SplitPath

Also, the operating system offers standard dialog boxes that prompt the user to pick a font, color, or icon. These dialogs can be displayed via DllCall() as demonstrated at www.autohotkey.com/forum/topic17230.html.

Examples

FileSelectFile, SelectedFile, 3, , Open a file, Text Documents (*.txt; *.doc)
if SelectedFile =
    MsgBox, The user didn't select anything.
else
    MsgBox, The user selected the following:`n%SelectedFile%


; CLSID Example (requires XP/2003 or earlier):
FileSelectFile, OutputVar,, ::{645ff040-5081-101b-9f08-00aa002f954e}  ; Recycle Bin.

; Multi-Select Example:
FileSelectFile, files, M3  ; M3 = Multiselect existing files.
if files =
{
    MsgBox, The user pressed cancel.
    return
}
Loop, parse, files, `n
{
    if a_index = 1
        MsgBox, The selected files are all contained in %A_LoopField%.
    else
    {
        MsgBox, 4, , The next file is %A_LoopField%.  Continue?
        IfMsgBox, No, break
    }
}
return