Programming Field

Ftype - DOS/Command Prompt Reference

[Windows 2000/XP or later] Assigns the execution program (‘open’ command) to the file type used in file associations.

* Changing the settings may require administrator privileges or equivalent permissions since it involves modifying system-wide configurations.

Syntax

-- Display file-type settings
ftype [progid]
-- Configure file-type settings
ftype progid=[open-command]
progid

Specifies the file type name, which actually means specifying the ProgID.

If you run ‘ftype’ without specifying the ‘progid’ independently, it will output all currently registered file types and their corresponding ‘open’ commands.

open-command

Specifies the command line, indicating the ‘open’ command, that includes the program to be executed (everything after the first ‘=’ character in ‘progid=open-command’ is considered the command line). The command line can include placeholders such as ‘%1’, which can be replaced with file names, in addition to the path to the program (see Placeholders available in the command line in Details).

* When using the ‘%’ character to include environment variables or placeholders, it's important to note that the handling of ‘%’ characters differs between executing commands in the prompt and writing them in a batch file. In a batch file, you must always specify ‘%%’ to represent a single ‘%’ character. On the other hand, in the prompt, you don't need to double the ‘%’ character, but in return, environment variables are automatically expanded, making it almost impossible to include the variable names themselves.

If you omit the open-command and stop at ‘ftype progid=’, using the ‘=’ sign, it will remove the command-line settings from the ProgID.

Details

Ftype deals with the file types used in file associations. When combined with the Assoc command, you can provide custom associations for your own extensions.

In Ftype, file types are registered under HKEY_LOCAL_MACHINE\Software\Classes. During registration, the open-command is written as the ‘(Default)’ value under progid\Shell\Open\Command with the REG_EXPAND_SZ type. The use of REG_EXPAND_SZ allows including environment variable names in the command line.

* Since Ftype registers a new file type without writing settings other than Shell\Open\Command, the file type name becomes empty. In Explorer, it will be displayed as something like ‘<extension> File’. Additionally, the icon is automatically searched and used from the program name in the open command. If you want to separately register these, you need to directly manipulate the registry using tools like the Reg command.

Placeholders available in the command line

While you can include placeholders like ‘%1’ in the command line, these placeholders become special and are specific to file association registration. They are somewhat different from the ‘%’ used in batch files. For example, ‘%*’ expands to all additional arguments given during file open, but it does not include the portion expanded to the file name by ‘%1’ (it's like ‘%2 %3 %4 ...’ when using ‘%*’).

The available placeholders are as follows.

%1Replaced with the file name (full path). Using %1 when the selected item does not exist, such as DesktopBackground or Directory\Background, will result in the failure of the action execution.
%2For the ‘printto’ case, it will be replaced with the printer name. If the command is executed with arguments in a script file or similar, it will be replaced with the the first argument following the file name.
%3For the ‘printto’ case, it will be replaced with the driver name. If the command is executed with arguments in a script file or similar, it will be replaced with the the second argument following the file name.
%4For the ‘printto’ case, it will be replaced with the port number. If the command is executed with arguments in a script file or similar, it will be replaced with the third argument following the file name (and subsequently, up to %9 can be used).
%*Replaced with spaces as separators for arguments starting from %2. Even if more than eight arguments (equivalent to %9) are specified, they will be included.
%DReplaced with the relative path from the desktop for the file (can be retrieved by calling IShellItem::GetDisplayName with SIGDN_DESKTOPABSOLUTEPARSING specified). If it's a virtual item, the path will contain a GUID string.
%HReplaced with ‘0’. (Details unclear; hexadecimal)
%IReplaced with the handle of the shared memory pointing to ITEMIDLIST (IDLIST_ABSOLUTE) data and the process ID that created the handle. The format will be ‘:<handle>:<pid>’ (both in decimal). If unavailable, ‘:0’ will be used.
%LReplaced with the file name (full path). Using %L when the selected item does not exist, such as DesktopBackground or Directory\Background, will result in the failure of the action execution.
%SReplaced with the window display state (values of SW_*). If ‘SW_SHOWNORMAL’ is specified at launch, ‘1’ will be inserted.
%U[Windows 7 or later]Replaced with the value of the OriginURL property contained in PKEY_ExpandoProperties, which is stored in IPropertyStore retrieved from the target IShellItem.
%VReplaced with the file name (full path). If %L is available, it will be the same value; otherwise, it will be replaced with the same value as %W.
%WReplaced with the working directory name (this value is not affected by ‘NoWorkingDirectory’). If the target item is not a file system, the presence of this specifier will result in the failure of the action execution.

* [Windows 7 or later] Using ‘%D’ and ‘%U’ for DesktopBackground or Directory\Background will result in a crash since IShellItem does not exist in these cases.
* The handle of the shared memory pointed to by "%I" is created by the private function ‘SHAllocShared’, and when accessing it from a different process, you need to duplicate it within the process using the private function ‘SHMapHandle’.
* [Windows 7 or later?] The <handle> pointed to by "%I" appears to be the address of the memory block in the calling Explorer. It seems that it cannot be duplicated from another process using SHMapHandle (it appears that SHAllocShared does not create a handle that can be duplicated in its internal implementation). Therefore, it seems that access is possible only through COM objects, such as DelegateExecute, which are accessed via the same process as the Explorer. (There are no issues in Vista.)

Samples

Sample 1

ftype | find /I "notepad.exe"

The file type that uses the ‘notepad.exe’ in the execution command (Open command) will be extracted and output. (Strictly speaking, it will also output those that include ‘notepad.exe’ in the ProgID.)

Sample 2 (Batch file)

@echo off
setlocal enableextensions
cd /D "%~f1"
copy /Y "%~dp0hoge.exe" . > NUL
ftype HogeFile="%CD%\hoge.exe" "%%1" > NUL
if not %ERRORLEVEL%==0 exit /b
assoc .hoge=HogeFile > NUL

Copies (installs) ‘hoge.exe’ to the directory specified as the first parameter of the batch file, and registers the file type as ‘HogeFile’ with the extension ‘.hoge’ so that the file can be opened with ‘hoge.exe’ in the destination path. Since Ftype returns an exit code other than 0 in case of an error, you can use the If command to check for success.