How to extract just the filename (no extension) from %1 ?
Thanks Andy !
ldigas
2009-09-16 16:09:41
+3
A:
If %1
will contain just a filename (no path):
echo %~n1
If it could contain a path:
echo %~dpn1
will give you the absolute pathname.
Beautiful isn't it! :-P [EDIT: These forms happily deal with quoted arguments, dequoting them in the process, as Johannes points out in a comment.]
For more info: help for
. That's right, the relevant help is under the FOR
command. Which indicates that this syntax only works with FOR
loop variables. But actually it seems to work fine with %1
, %2
, etc. Which might make you think that it would work with any environment variable -- but it doesn't.
I love CMD.EXE
.
j_random_hacker
2009-09-16 16:13:44
Why do you expect this to break on a quoted argument? The help explicitly states that all surrounding quotes will be removed.
Joey
2009-09-16 16:20:08
You're right Johannes. The help says that `%~I` will "expand %I removing any surrounding quotes", leading me to the (incorrect) conclusion that using the other forms involving `~` would *not* remove surrounding quotes.
j_random_hacker
2009-09-16 16:30:17
Thanks j_random. The first answer solved my problem, but you pointed some useful points (help for ?!, for example :)
ldigas
2009-09-16 17:44:31
all the single percent variables support the ~ syntax (command line arguments, for loop, call label)
Anders
2009-09-16 23:57:31
Thanks Anders, cmd.exe appears saner by the minute! But what are call labels (in the context of "single percent variables")?
j_random_hacker
2009-09-17 03:11:58
you can call a label, a subroutine basically. :echoandwaitecho %~1pausegoto :EOF...call :echoandwait "hello world"
Anders
2009-09-29 11:30:40