Tuesday, April 23, 2013

Echo executing .BAT file name

Full file name of executing batch is available in %0 variable, with quotes if needed ("C:\Users\vurdalakov\github\code_examples\parameter_extensions.bat")

Following modifier can be added:
  • %~f0 - fully qualified path name (C:\Users\vurdalakov\github\code_examples\parameter_extensions.bat)
  • %~d0 - drive letter (C:)
  • %~p0 - path without drive letter but with leading and trailing slashes (\Users\vurdalakov\github\code_examples\)
  • %~n0 - file name without extension (parameter_extensions)
  • %~x0 - file extension with leading dot (.bat)
Modifiers can be combined, e.g. %~nx0 to get file name with extension (parameter_extensions.bat).

Special s modifier can be used to get short (8.3) file and directory names, e.g. %~snx0 (PARAME~1.BAT)

@echo off

echo %0
echo %~f0

echo.

echo %~d0
echo %~p0
echo %~dp0

echo.

echo %~n0
echo %~x0
echo %~nx0

echo.

echo %~sf0
echo %~sdp0
echo %~snx0
Code above produces the following output:
"C:\Users\vurdalakov\github\code examples\parameter_extensions.bat"
C:\Users\vurdalakov\github\code examples\parameter_extensions.bat

C:
\Users\vurdalakov\github\code examples\
C:\Users\vurdalakov\github\code examples\

parameter_extensions
.bat
parameter_extensions.bat

C:\Users\VURDAL~1\github\CODEEX~1\PARAME~1.BAT
C:\Users\VURDAL~1\github\CODEEX~1\
PARAME~1.BAT

No comments:

Post a Comment