How to Get File Name from a Full Path in Batch Script
When working with file paths in a batch script, you often start with a full path (e.g., C:\Users\Admin\Documents\report.txt) but only need a specific part of it, such as just the filename (report.txt), the extension (.txt), or the drive letter (C:). Manually parsing these strings is complex and unreliable. Fortunately, batch scripting provides a powerful set of built-in parameter expansion modifiers to deconstruct a path instantly.
This guide will teach you how to use the special tilde (~) modifiers within a FOR loop or on a script argument to easily extract the filename, extension, and other components from a full path, a fundamental skill for any file-handling script.
The Core Method: Parameter Expansion Modifiers
The key to parsing paths is the set of modifiers that can be applied to a FOR loop variable (like %%F) or a script argument (like %1). These are signaled by a tilde (~) placed after the %.
- For a
FORloop variable:FOR %%I IN ("path") DO ECHO %%~nxI - For a script argument:
ECHO %~nx1
Each letter following the tilde has a specific meaning and extracts a different part of the path.
The Modifiers Cheat Sheet
Let's assume we have the following full path stored in a variable:
C:\Users\Admin\Documents\My Report.txt
| Modifier | Meaning | Example Output |
|---|---|---|
%%~fI | full path | C:\Users\Admin\Documents\My Report.txt |
%%~dI | drive letter | C: |
%%~pI | path (folder) | \Users\Admin\Documents\ |
%%~nI | filename only | My Report |
%%~xI | extension only | .txt |
%%~sI | short (8.3) path | C:\Users\Admin\DOCUME~1\MYREPO~1.TXT |
%%~zI | size of the file | 12345 (in bytes) |
%%~tI | timestamp | 10/27/2023 10:15 AM |
You can also combine these modifiers to get specific results. The most useful combinations are:
| Modifier | Meaning | Example Output |
|---|---|---|
%%~dpI | drive and path | C:\Users\Admin\Documents\ |
%%~nxI | name and extension | My Report.txt |
Basic Example: Deconstructing a Path with FOR
This is the standard method when you have a path stored in a variable. The FOR loop runs once, giving you access to the modifiers.
@ECHO OFF
SET "MyFilePath=C:\Users\Admin\Documents\My Report.txt"
ECHO --- Deconstructing Path ---
ECHO Original Path: "%MyFilePath%"
ECHO.
FOR %%F IN ("%MyFilePath%") DO (
ECHO Full Path (f): %%~fF
ECHO Drive (d): %%~dF
ECHO Path (p): %%~pF
ECHO Name (n): %%~nF
ECHO Extension (x): %%~xF
ECHO ---------------------------
ECHO Drive + Path (dp): %%~dpF
ECHO Name + Ext (nx): %%~nxF
)
This script cleanly breaks the path into all its constituent parts.
Working with Script Arguments (%~1)
The same modifiers can be applied directly to script arguments (%1, %2, etc.). This is extremely useful for creating tool scripts that operate on files dragged onto them or passed from the command line.
@ECHO OFF
IF "%~1"=="" (ECHO Please provide a file path as an argument. & GOTO :EOF)
ECHO --- Parsing Argument 1 ---
ECHO.
ECHO Full Path (f): %~f1
ECHO Drive + Path (dp): %~dp1
ECHO Name + Ext (nx): %~nx1
And an example of usage and generated output:
C:\> ParsePath.bat "C:\My Project\src\main.js"
--- Parsing Argument 1 ---
Full Path (f): C:\My Project\src\main.js
Drive + Path (dp): C:\My Project\src\
Name + Ext (nx): main.js
%~1 itself is a special modifier that removes quotes from the argument.
Common Pitfalls and How to Solve Them
-
Using Modifiers on Regular Variables: This is the most common mistake. The
~modifiers do not work on regular variables with percent signs (%MyVar%).REM This will NOT work. It will just print the literal string.
ECHO %~nxMyFilePath%Solution: You must use a
FORloop to process a regular variable. This is the standard, accepted pattern. -
Paths with Spaces: The examples above are all robust because the input paths are quoted (
"%MyFilePath%"). This ensures that theFORloop or the argument parser receives the entire path as a single entity.
Practical Example: A File Processing and Renaming Script
This script takes a file path as an argument, extracts its components, and renames the file by appending the current date to its name.
@ECHO OFF
SETLOCAL
SET "FullFilePath=%~f1"
IF NOT EXIST "%FullFilePath%" (ECHO File not found. & GOTO :End)
ECHO Original file: "%FullFilePath%"
REM --- Deconstruct the path ---
FOR %%F IN ("%FullFilePath%") DO (
SET "ParentFolder=%%~dpF"
SET "FileName=%%~nF"
SET "FileExt=%%~xF"
)
REM --- Create a date stamp ---
SET "DateStamp=%DATE:~10,4%-%DATE:~4,2%-%DATE:~7,2%"
REM --- Construct the new name ---
SET "NewFileName=%FileName%_%DateStamp%%FileExt%"
ECHO Renaming to: "%NewFileName%"
REN "%FullFilePath%" "%NewFileName%"
:End
ENDLOCAL
For usage just run AddDate.bat "C:\Reports\Sales.csv" and the file will be renamed to Sales_2023-10-27.csv.
Conclusion
The parameter expansion modifiers are a powerful, built-in feature of batch scripting that provide a complete solution for parsing file paths.
- To parse a path stored in a regular variable, you must use a
FORloop:FOR %%F IN ("%MyVar%") DO ECHO %%~nxF - To parse a path passed as a script argument, you can apply the modifiers directly:
ECHO %~nx1 - The most useful combinations are
~dp(drive and path) and~nx(name and extension).
By mastering these simple but powerful modifiers, you can write clean, reliable scripts that can confidently handle any file path.