Help with Remove %~dp0" from the string variable %~f0

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Eximious
Posts: 2
Joined: 22 May 2013 08:43

Help with Remove %~dp0" from the string variable %~f0

#1 Post by Eximious » 22 May 2013 09:41

I am trying to isolate a batch file name (something.bat)
by removing %~dp0
C:\Users\Public\Batch Files\
from %~f0
C:\Users\Public\Batch Files\something.bat

Unsuccessful:
set str=%~f0 :: Yields C:\Users\Public\Batch Files\
set str=%str:%~dp0=% :: Yields ~dp0=
or
set str=%str:%%~dp0=% :: Yields C:\Users\Public\Batch Files\=
or
set str="%str:%%~dp0=%" :: Yields "C:\Users\Public\Batch Files\="
or
set "str"="%str:%%~dp0=%" :: Yields C:\Users\Public\Batch Files\something.bat

Can I use a variable with string replacement?

As an alternative, I tried
strLen for a variable i.e. %~dp0
Call :strLen "%~dp0" %len%
with the same lack of success.

My alternate was going to be
%test:~7% :: Skip 7 characters and then grab everything else
using something like
Call :strLen "%~dp0" %len% :: to replace the hardcoded 7 with %len%
although I never got that far. What am I not understanding?

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Help with Remove %~dp0" from the string variable %~f0

#2 Post by Aacini » 22 May 2013 13:14

If you want the name and extension of the running Batch file, just use %~nx0

And the way to solve your question, is:

Code: Select all

setlocal EnableDelayedExpansion
set str=%~f0
set str=!str:%~dp0=!

or

Code: Select all

set str=%~f0
call set str=%%str:%~dp0=%%


Antonio

Eximious
Posts: 2
Joined: 22 May 2013 08:43

Re: Help with Remove %~dp0" from the string variable %~f0

#3 Post by Eximious » 22 May 2013 15:18

Thank you, Antonio, for your reply.
:mrgreen: Brilliant ! :mrgreen:
I would never have constructed !str:%~dp0=! -- Perfect Solution, as well as %%str:%~dp0=%%

and ... %~nx0 ... well, I used %~n0 , %~x0 , and %~dp0 ... but %~nx0 ? :oops: Thanks!

Post Reply