Can a Windows batch file determine its own file name?

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 in myScript.bat that can define the string "myScript.bat"?

+62
windows cmd batch-file
Jan 10 2018-12-12T00:
source share
6 answers

Yes.

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

Write %~n0 to get only the file name without the extension.

Write %~n0%~x0 to get the file name and extension.

+81
Jan 10 2018-12-12T00:
source share

Using the following script based on the SLaks answer, I decided the correct answer was:

 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 

Interestingly, % nx0 will not work, since we know that the '~' char is usually used to cut / trim quotes from a variable.

+27
Nov 05 '12 at 15:47
source share

You can get the file name, but you can also get the full path, depending on what you put between "% ~" and "0". Make a choice from

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

For example, from within c: \ tmp \ foo.bat, %~nx0 gives you "foo.bat", and %~dpnx0 gives you "c: \ tmp \ foo.bat". Note that the parts are always assembled in canonical order, so if you get pretty and try %~xnpd0 , you will still get "c: \ tmp \ foo.bat"

+20
Mar 20 '15 at 11:32
source share

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

So, if the file is myfile.bat, you can name it in several ways as follows, each of which will give you a different result of using% 0 or% ~ 0:

Myfile

myfile.bat

MYDIR \ myfile.bat

C: \ MYDIR \ myfile.bat

"C: \ MYDIR \ myfile.bat"

All of the above is a legal challenge if you call it from the correct relative location in the directory in which it exists. % ~ 0 breaks the quotes from the last example, while% 0 does not.

Since they all give different results,% 0 and% ~ 0 are unlikely to be what you really want to use.

Here is the 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 
+6
Nov 11 '15 at 17:15
source share

%0 is what you are looking for. See this question for more details.

 echo %0 
+2
Jan 10 2018-12-12T00:
source share

Try the example below to feel how magic 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 relevant rules are as follows.

 %~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 
+1
May 08 '17 at 1:46 pm
source share



All Articles