How to Determine File Type in a Batch Script
In a batch script, you often need to know the "type" of a file to understand how to interact with it. Is it a text file? An executable program? A Word document? Windows stores this information in the registry as a "File Type," which links a file extension (like .txt) to a descriptive name (like txtfile) and an action (like opening it with Notepad).
This guide will teach you how to use the two primary built-in commands, assoc and ftype, to determine a file's type and the command used to open it. This is the command-line equivalent of looking at the "Type of file" property in Windows Explorer.
The Two-Part System: assoc and ftype
Determining a file's type from the command line is a two-step process that mirrors how Windows itself handles file associations.
assoc(Associate): This command is the first lookup. You give it a file extension (e.g.,.txt), and it gives you back the internal "File Type" name associated with it (e.g.,txtfile).ftype(File Type): This is the second lookup. You give it the File Type name from the previous step (txtfile), and it tells you the exact command that is executed when you double-click a file of that type.
Step 1: Getting the File Type Name with assoc
The assoc command is used to view or modify the association between an extension and a File Type name.
Syntax:assoc .extension
Let's find the File Type for a .txt file.
C:\> assoc .txt
Output: the command returns a single line showing the association.
.txt=txtfile
This tells us that the internal name for a .txt file is txtfile.
Step 2: Getting the Open Command with ftype
Now that we have the File Type name (txtfile), we can use ftype to find out what command is associated with it.
Syntax: ftype FileTypeName
For example:
C:\> ftype txtfile
Output: this command shows the full command string that is executed.
txtfile=%SystemRoot%\system32\NOTEPAD.EXE "%1"
This tells us that files of type txtfile are opened with NOTEPAD.EXE. The "%1" is the crucial placeholder for the file that was clicked.
Putting It Together in a Script
To automate this two-step lookup in a script, you need to capture the output of the first command (assoc) and use it as the input for the second command (ftype). This requires a FOR /F loop.
@ECHO OFF
SET "Extension=.docx"
SET "FileType="
SET "OpenCommand="
ECHO --- Determining File Type for %Extension% ---
ECHO.
REM --- Step 1: Get the File Type from the extension ---
FOR /F "tokens=2 delims==" %%A IN ('assoc %Extension%') DO (
SET "FileType=%%A"
)
IF NOT DEFINED FileType (
ECHO No association found for %Extension%.
GOTO :End
)
ECHO The File Type is: %FileType%
REM --- Step 2: Get the Open Command from the File Type ---
FOR /F "tokens=2,*" %%B IN ('ftype %FileType%') DO (
SET "OpenCommand=%%C"
)
IF NOT DEFINED OpenCommand (
ECHO No open command found for %FileType%.
) ELSE (
ECHO The Open Command is: %OpenCommand%
)
:End
Common Pitfalls and How to Solve Them
-
No Association Exists: If a file extension is not registered on the system, the
assoccommand will return an error:File association not found for extension .ext. The script above handles this by checkingIF NOT DEFINED FileType. -
Parsing Complex
ftypeOutput: Theftypecommand string can be complex. Thetokens=2,* delims==pattern is generally robust, as it splits the line by the first=and assigns everything after it to the second variable. -
Administrator Rights: Viewing file associations with
assocandftypedoes not require administrator rights, as this information is generally readable by all users. However, changing them does require elevation.
Practical Example: A "What Is This File?" Script
This script takes a filename as a command-line argument, extracts its extension, and then performs the two-step lookup to report how Windows will open it.
@ECHO OFF
SETLOCAL
SET "FilePath=%~1"
IF "%FilePath%"=="" (ECHO Usage: %~n0 "filename" & GOTO :End)
IF NOT EXIST "%FilePath%" (ECHO File not found. & GOTO :End)
ECHO --- File Type Inspector ---
ECHO File: "%FilePath%"
ECHO.
REM --- Get the extension from the input file ---
SET "FileExt=%~x1"
IF "%FileExt%"=="" (ECHO File has no extension. & GOTO :End)
REM --- Step 1: Get the File Type ---
SET "FileType="
FOR /F "tokens=2 delims==" %%A IN ('assoc %FileExt%') DO SET "FileType=%%A"
IF NOT DEFINED FileType (ECHO No association found for %FileExt%. & GOTO :End)
ECHO Extension '%FileExt%' is of type: %FileType%
REM --- Step 2: Get the Open Command ---
SET "OpenCommand="
FOR /F "tokens=1,* delims==" %%B IN ('ftype %FileType%') DO SET "OpenCommand=%%C"
IF NOT DEFINED OpenCommand (
ECHO No open command is defined for this file type.
) ELSE (
ECHO It opens with the command: "%OpenCommand%"
)
:End
ENDLOCAL
Conclusion
The combination of assoc and ftype provides a complete, built-in method for determining how the Windows shell will handle a specific file type.
associs the first step, linking a file extension to a File Type name.ftypeis the second step, linking a File Type name to an executable command.
By using FOR /F loops to parse the output of these two commands, you can write scripts that can intelligently identify a file's type and the program used to open it.