FileSelect()

Auto Hotkey

FileSelect

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

OutputVar := FileSelect(Options, RootDir\Filename, Prompt, Filter)
Function Example: Files := FileSelect("M3", A_ScriptDir)

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: 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 FileSelect). 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

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 the +OwnDialogs option. 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 FileSelect 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
FileSelect, OutputVar
Thread, NoTimers, false

Related

DirSelect, 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

FileSelect, 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):
FileSelect, OutputVar,, ::{645ff040-5081-101b-9f08-00aa002f954e}  ; Recycle Bin.

; Multi-Select Example:
FileSelect, 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
    {
        Result := MsgBox("The next file is %A_LoopField%.  Continue?",, 4)
        if Result = "No", break
    }
}
return