views:

99

answers:

2

How to extract just the filename (no extension) from %1 ?

+4  A: 

This should work

echo %~n1

Andy Morris
Thanks Andy !
ldigas
+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
Why do you expect this to break on a quoted argument? The help explicitly states that all surrounding quotes will be removed.
Joey
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
Thanks j_random. The first answer solved my problem, but you pointed some useful points (help for ?!, for example :)
ldigas
all the single percent variables support the ~ syntax (command line arguments, for loop, call label)
Anders
Thanks Anders, cmd.exe appears saner by the minute! But what are call labels (in the context of "single percent variables")?
j_random_hacker
you can call a label, a subroutine basically. :echoandwaitecho %~1pausegoto :EOF...call :echoandwait "hello world"
Anders
@Anders: Thanks, I didn't know that!
j_random_hacker