162

Can a Windows batch file determine its own file name?

For example, if I run the batch file C:\Temp\myScript.bat, is there a command within myScript.bat that can determine the string "myScript.bat"?

Ross Ridge
  • 38,414
  • 7
  • 81
  • 112
djangofan
  • 28,471
  • 61
  • 196
  • 289

6 Answers6

215

Yes.

Use the special %0 variable to get the path to the current file.

Write %~n0 to get just the filename without the extension.

Write %~n0%~x0 to get the filename and extension.

Also possible to write %~nx0 to get the filename and extension.

Tim M.
  • 105
  • 2
  • 8
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 2
    This is just inaccurate. Both %0 and %~n0 get just the filename part. @djangofan 's answer is correct. – JustJeff Mar 20 '15 at 11:06
  • 10
    %0 returns the way the file was called. If a full path was used, %0 will have that. If you `cd` into the dir first and execute the batch from there, %0 will usually not have the path info (but you could get that from %cd% in that case) – Gogowitsch Jul 09 '15 at 08:51
  • @gogowitsch: Are you sure? I don't think that's true. – SLaks Jul 09 '15 at 14:29
  • On Win2012, `%cd%` will return the Current Directory of the process that called the batch file. So, if you are at a prompt in `D:\dir1` and enter `Y:\foo\bar.bat`, if `bar.bat` has `@echo %cd%` then it will output `D:\dir1`. – Preacher Jun 28 '18 at 21:56
61

You can get the file name, but you can also get the full path, depending what you place between the '%~' and the '0'. Take your pick from

d -- drive
p -- path
n -- file name
x -- extension
f -- full path

E.g., from inside c:\tmp\foo.bat, %~nx0 gives you "foo.bat", whilst %~dpnx0 gives "c:\tmp\foo.bat". Note the pieces are always assembled in canonical order, so if you get cute and try %~xnpd0, you still get "c:\tmp\foo.bat"

robotik
  • 1,837
  • 1
  • 20
  • 26
JustJeff
  • 12,640
  • 5
  • 49
  • 63
37

Using the following script, based on SLaks answer, I determined that the correct answer is:

echo The name of this file is: %~n0%~x0
echo The name of this file is: %~nx0

And here is my test script:

@echo off
echo %0
echo %~0
echo %n0
echo %x0
echo %~n0
echo %dp0
echo %~dp0
pause

What I find interesting is that %nx0 won't work, given that we know the '~' char usually is used to strip/trim quotes off of a variable.

djangofan
  • 28,471
  • 61
  • 196
  • 289
18

Bear in mind that 0 is a special case of parameter numbers inside a batch file, where 0 means this file as given on the command line.

So if the file is myfile.bat, you could call it in several ways as follows, each of which would give you a different output from the %0 or %~0 usage:

myfile

myfile.bat

mydir\myfile.bat

c:\mydir\myfile.bat

"c:\mydir\myfile.bat"

All of the above are legal calls if you call it from the correct relative place to the directory in which it exists. %~0 strips the quotes from the last example, whereas %0 does not.

Because these all give different results, %0 and %~0 are very unlikely to be what you actually want to use.

Here's a batch file to illustrate:

@echo Full path and filename: %~f0
@echo Drive: %~d0
@echo Path: %~p0
@echo Drive and path: %~dp0
@echo Filename without extension: %~n0
@echo Filename with    extension: %~nx0
@echo Extension: %~x0
@echo Filename as given on command line: %0
@echo Filename as given on command line minus quotes: %~0
@REM Build from parts
@SETLOCAL
@SET drv=%~d0
@SET pth=%~p0
@SET fpath=%~dp0
@SET fname=%~n0
@SET ext=%~x0
@echo Simply Constructed name: %fpath%%fname%%ext%
@echo Fully  Constructed name: %drv%%pth%%fname%%ext%
@ENDLOCAL
pause
Grant Foster
  • 722
  • 2
  • 11
  • 21
Jool
  • 1,706
  • 16
  • 14
3

Try to run below example in order to feel how the magical variables work.

@echo off

SETLOCAL EnableDelayedExpansion

echo Full path and filename: %~f0
echo Drive: %~d0
echo Path: %~p0
echo Drive and path: %~dp0
echo Filename without extension: %~n0
echo Filename with    extension: %~nx0
echo Extension: %~x0

echo date time : %~t0
echo file size: %~z0

ENDLOCAL

The related rules are following.

%~I         - expands %I removing any surrounding quotes ("")
%~fI        - expands %I to a fully qualified path name
%~dI        - expands %I to a drive letter only
%~pI        - expands %I to a path only
%~nI        - expands %I to a file name only
%~xI        - expands %I to a file extension only
%~sI        - expanded path contains short names only
%~aI        - expands %I to file attributes of file
%~tI        - expands %I to date/time of file
%~zI        - expands %I to size of file
%~$PATH:I   - searches the directories listed in the PATH
               environment variable and expands %I to the
               fully qualified name of the first one found.
               If the environment variable name is not
               defined or the file is not found by the
               search, then this modifier expands to the
               empty string
Cary
  • 372
  • 1
  • 5
  • 14
0

Below is my initial code:

@echo off
Set z=%%
echo.
echo %z%0.......%0
echo %z%~0......%~0
echo %z%n0......%n0
echo %z%x0......%x0
echo %z%~n0.....%~n0
echo %z%dp0.....%dp0
echo %z%~dp0....%~dp0
echo.

I noticed that file name given by %~0 and %0 is the way it was entered in the command-shell and not how that file is actually named. So if you want the literal case used for the file name you should use %~n0. However, this will leave out the file extension. But if you know the file name you could add the following code.

set b=%~0
echo %~n0%b:~8,4%

I have learned that ":~8,4%" means start at the 9th character of the variable and then show show the next 4 characters. The range is 0 to the end of the variable string. So %Path% would be very long!

fIlEnAmE.bat
012345678901

However, this is not as sound as Jool's solution (%~x0) above.

My Evidence:

C:\bin>filename.bat

%0.......filename.bat
%~0......filename.bat
. . .

C:\bin>fIlEnAmE.bat

%0.......fIlEnAmE.bat
%~0......fIlEnAmE.bat
%n0......n0
%x0......x0
%~n0.....FileName
%dp0.....dp0
%~dp0....C:\bin\

%~n0%b:~8,4%...FileName.bat

Press any key to continue . . .

C:\bin>dir
 Volume in drive C has no label.
 Volume Serial Number is CE18-5BD0

 Directory of C:\bin
. . .
05/02/2018  11:22 PM               208 FileName.bat

Here is the final code

@echo off
Set z=%%
set b=%~0
echo.
echo %z%0.......%0
echo %z%~0......%~0
echo %z%n0......%n0
echo %z%x0......%x0
echo %z%~n0.....%~n0
echo %z%dp0.....%dp0
echo %z%~dp0....%~dp0
echo.
echo A complex solution:
echo ===================================
echo %z%~n0%z%b:~8,4%z%...%~n0%b:~8,4%
echo ===================================
echo.
echo The preferred solution:
echo ===================================
echo %z%~n0%z%~x0.......%~n0%~x0
echo ===================================
pause
  • I may be missing something but for me your final script example's complex solution doesn't work for me. At least not if the script is named "ScriptPathTest.cmd". It works if you do "set b=%~snx0" so that it gets set to "SCRIPT~1.cmd" and then you take the last four as normal, but at that point you might as well just do set b=%~x0 and use %b% OR ... just %~x0 like you do in the preferred one. =P – Brent Rittenhouse Feb 18 '21 at 05:08
  • It should also be noted that %0 doesn't work inside a called function so I like to start my scripts as: @( echo off & setlocal EnableDelayedExpansion & set _previous_errorlevel_=%ERRORLEVEL% & set _script_name_=%~0 & ( call :run %* || goto :exit_failure ) & goto :exit_success ) .. with the &s being newlines in practice. For those wondering, I store the previous errorlevel in case the user calls the script with /?, -h, --h. -help, or --help so I don't overwrite it unnecessarily for them. – Brent Rittenhouse Feb 18 '21 at 05:14
  • Huh... it seems like %0 or %~0 WITH NO LETTERS gives you a called subroutines name, but if you use %~n0, %~x0, %~nx0, %~f0, etc you'll get the equivilents for the script name instead. So either store it upfront or make sure you use letters always I guess. – Brent Rittenhouse Feb 18 '21 at 05:28