How to Open Windows Explorer at a Specific Folder in a Batch Script
A very common and user-friendly action for a batch script is to open a Windows Explorer window showing a specific folder. After your script creates a report, downloads a file, or creates a backup, you can automatically open the output location for the user. This provides immediate access and confirms that the script's action was completed successfully.
This guide will teach you how to use the built-in explorer.exe command to open any folder. You will learn the best practice for launching it, and the powerful /select switch that allows you to not only open a folder but also highlight a specific file within it.
The Core Command: explorer.exe
The executable for Windows Explorer is explorer.exe. You can call this command from a batch script and pass it a folder path as an argument to open that specific location.
Syntax: explorer "C:\Path\To\Folder"
The Recommended Method: Using the START Command
While you can call explorer.exe directly, it's a better practice to launch it using the START command. This ensures that the process is launched asynchronously, allowing your batch script to continue its execution or exit cleanly without waiting for the Explorer window to be closed.
Syntax:
START "" "C:\Path\To\Folder"
or
START "" explorer "C:\Path\To\Folder"
START: The command to launch a new process asynchronously."": A required dummy "title". TheSTARTcommand's first quoted string is always treated as the title for a new window.explorer(optional but good practice): Explicitly namingexplorer.exemakes your script's intent clearer."C:\Path\To\Folder": The directory you want to open.
Basic Example: Opening a Specific Folder
This script opens the user's Documents folder.
@ECHO OFF
ECHO --- Opening your Documents folder ---
ECHO.
REM Use the %USERPROFILE% variable to find the folder reliably.
START "" "%USERPROFILE%\Documents"
ECHO The folder has been opened in a new window.
When this script runs, a new Windows Explorer window will appear, displaying the contents of the user's Documents folder. The batch script will continue to run or exit immediately.
The "Select" Switch: Highlighting a Specific File
This is a powerful and highly useful feature. The /select switch tells Explorer to open the containing folder and automatically select (highlight) a specific file or subfolder.
Syntax: explorer /select,"C:\Path\To\File.txt"
/select,: The switch, followed immediately by a comma (with no space)."C:\Path\To\File.txt": The full path to the item you want to be highlighted.
For example, this script creates a new text file and then opens its containing folder with the new file already selected.
@ECHO OFF
SET "NewFile=%TEMP%\MyNewReport.txt"
ECHO This is the report content. > "%NewFile%"
ECHO File created. Now opening its location...
explorer /select,"%NewFile%"
This is extremely user-friendly, as it directs the user's attention to the exact file your script just created.
Common Pitfalls and How to Solve Them
-
Paths with Spaces: This is the most common reason for failure. If the path to your folder or file contains spaces, you must enclose it in double quotes.
- WRONG:
explorer C:\My Project\Files - RIGHT:
explorer "C:\My Project\Files"
- WRONG:
-
Forgetting the
STARTTitle: A common mistake is to runSTART "C:\My Folder". This will not open the folder. It will open a new, empty command prompt window with "C:\My Folder" as its title.- Solution: Remember the
STARTcommand's quirky syntax. The title comes first:START "My Folder" "C:\My Folder". Using empty quotes ("") for the title is the simplest and most common practice.
- Solution: Remember the
-
No Space After
/select,: The syntax for the select switch is very specific. There must not be a space between the comma and the path.- WRONG:
explorer /select, "C:\file.txt" - RIGHT:
explorer /select,"C:\file.txt"
- WRONG:
Practical Example: A "Create and Show Report" Script
This script generates a simple system report, saves it to the user's Documents folder with a unique name, and then opens Explorer with the new report highlighted for the user.
@ECHO OFF
SETLOCAL
ECHO --- System Report Generator ---
ECHO This script will create a report and show you where it is.
ECHO.
PAUSE
ECHO.
REM --- Find the Documents folder reliably ---
SET "DocsFolder="
FOR /F "tokens=1,2,*" %%A IN ('REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Personal') DO (
SET "DocsFolder=%%C"
)
IF NOT DEFINED DocsFolder SET "DocsFolder=%USERPROFILE%\Documents"
SET "ReportFile=%DocsFolder%\Report_%COMPUTERNAME%.txt"
ECHO Creating report at "%ReportFile%"...
(
ECHO System Report for %COMPUTERNAME%
ECHO Generated on %DATE% %TIME%
ECHO ===============================
ECHO OS Version:
ver
) > "%ReportFile%"
ECHO.
ECHO [SUCCESS] Report created. Opening the file location...
explorer /select,"%ReportFile%"
ENDLOCAL
Conclusion
Using explorer.exe is a simple and effective way to make your batch scripts more user-friendly by providing direct access to the files and folders they create or modify.
Key takeaways:
- The basic command is
explorer "path", but the recommended method is to useSTART "" "path"to ensure it runs in the background. - Always enclose your paths in double quotes to handle spaces.
- Use the powerful
/select,"path\to\file"switch to open a folder and automatically highlight a specific item.