-2

I want my batch file to echo its filename into a text document.

I tried:

echo %~f0>"%userprofile%\Desktop\test\test.txt"

But instead of saying only its filename, it also says the directory that it's in.

C:\Users\User\Desktop\test.bat
barteczek56
  • 95
  • 1
  • 1
  • 7
  • The title says something different than the question body, so please update accordingly! – aschipfl Mar 19 '17 at 11:21
  • More interesting, that someone who already found the `%~f` modifier (which is quite hard to find for a newbie) seems not to be able to find the other ones. – Stephan Mar 20 '17 at 06:57

2 Answers2

3

Use %~n0 or %~nx0 in place of %~f0 depending on what your interpretation of name is.

See

for /? |more

from the prompt for documentation

Magoo
  • 77,302
  • 8
  • 62
  • 84
1

To get the filename and extension, use %~nx0. For a complete list of variable references, see for /?


For example:

echo %~nx0 > "%userprofile%\Desktop\test\test.txt"

Would output:

test.bat

Community
  • 1
  • 1
Sam Denty
  • 3,693
  • 3
  • 30
  • 43