Skip to content

How to Get the Script's Own Filename with %0 in Windows Batch

Learn how to use %0 and tilde modifiers (%~f0, %~dp0, %~nx0) in Windows Batch scripts to reliably extract the script's full path, directory, filename, and extension for logging, file operations, and self-aware scripting.

In scripting, it's often useful for a script to know its own name. This can be used for logging purposes ("MyScript.bat started at..."), creating related files (like MyScript.log or MyScript.ini), or in help messages. In Windows Batch, this self-awareness is achieved through the special argument zero (%0) parameter.

This guide will teach you how to use the %0 parameter and the powerful tilde (~) modifiers that accompany it. You will learn how to extract not just the simple name, but also the full path, the drive letter, and other components, allowing your script to be fully aware of its own identity and location.

The Magic Parameter: %0

In a batch script, the parameters %1, %2, %3, etc., refer to the arguments passed to the script by the user. The special parameter %0 comes before these and always refers to the command used to invoke the script itself.

This often includes not just the filename but also the path used to call it.

Basic Example: Displaying the Script's Name

Let's create a simple script to see what %0 contains in different scenarios.

ShowMyName.batbatch
@ECHO OFF
ECHO The value of %%0 is: %0

Scenario 1: Run from the same directory

C:\Scripts> ShowMyName.bat
The value of %0 is: ShowMyName.bat

Here, %0 is just the simple filename.

Scenario 2: Run from a different directory using a full path

C:\> C:\Scripts\ShowMyName.bat
The value of %0 is: C:\Scripts\ShowMyName.bat

Here, %0 contains the full path because that's what was used to call it. Because of this variability, we need a more reliable way to get just the parts we want.

The Power of Tilde (~) Modifiers with %0

To solve the inconsistency of %0, batch scripting provides a set of "tilde modifiers" that can extract specific parts of the path string contained in %0. These are the same powerful modifiers that work on other arguments (%1) and FOR loop variables (%%F).

The syntax is %~[modifiers]0.

For example, %~n0 will always give you just the script's name without the extension, regardless of how it was called.

The %~0 Cheat Sheet

Let's assume your script is located at D:\My Tools\MyScript.bat.

Modifier Meaning Example Output
%0 The raw command used MyScript.bat or D:\My Tools\MyScript.bat (varies)
%~f0 full path D:\My Tools\MyScript.bat
%~d0 drive letter D:
%~p0 path (folder) \My Tools\
%~n0 filename only MyScript
%~x0 extension only .bat
%~s0 short (8.3) path D:\MYTOOL~1\MYSCRIPT.BAT
%~dp0 drive and path D:\My Tools\
%~nx0 name and extension MyScript.bat

Of these, %~dp0 (the script's directory) and %~nx0 (the script's name) are among the most useful variables in all of batch scripting.

Common Pitfalls and How to Solve Them

  • Relying on plain %0: The biggest mistake is assuming %0 will just be the filename. As shown, it can contain a full path. Solution: Always use the tilde modifiers to get the specific part of the path you need. Use %~nx0 if you reliably want just the filename and extension.

  • Quotes in the Path: The plain %0 might be quoted if the script was called from a path with spaces. For example, "%~f0" will correctly expand to the full path and then our own quotes will handle any spaces within it.

Practical Example: A Self-Aware Logging Script

This script uses its own name to automatically create a uniquely named log file in the same directory where the script is located. This makes the script completely self-contained and portable.

DataProcessor.batbatch
@ECHO OFF
SETLOCAL

REM --- Get the script's own name and directory ---
SET "ScriptName=%~n0"
SET "ScriptDir=%~dp0"
SET "LogFile=%ScriptDir%%ScriptName%.log"

ECHO --- %ScriptName% ---
ECHO.
ECHO This script will log its output to:
ECHO "%LogFile%"
ECHO.

REM --- Redirect all future output of a code block to the log file ---
(
    ECHO Log started at %DATE% %TIME%
    ECHO.
    ECHO Starting data processing...
    REM (Simulate some work)
    ECHO Found 100 records.
    ECHO Process finished successfully.
    ECHO.
    ECHO Log finished.
) > "%LogFile%"

ECHO [SUCCESS] Log file has been written.

ENDLOCAL

If you rename DataProcessor.bat to MonthlyReport.bat, the script will automatically start creating MonthlyReport.log without any code changes.

Conclusion

The %0 parameter and its tilde modifiers are essential tools for creating intelligent and portable batch scripts. They allow a script to be aware of its own identity and location, freeing you from hardcoded paths and names.

Key takeaways:

  • %0 refers to the command used to run the script, which can be inconsistent.
  • Always use the tilde modifiers to reliably extract the exact part of the path you need.
  • The most useful modifiers are:
    • %~f0 for the full, absolute path.
    • %~dp0 for the script's containing directory.
    • %~nx0 for the script's name and extension.

By leveraging these special parameters, you can write more dynamic, robust, and professional batch scripts.

Navigation