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?
Help with Remove %~dp0" from the string variable %~f0
Moderator: DosItHelp
Re: Help with Remove %~dp0" from the string variable %~f0
If you want the name and extension of the running Batch file, just use %~nx0
And the way to solve your question, is:
or
Antonio
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
Re: Help with Remove %~dp0" from the string variable %~f0
Thank you, Antonio, for your reply.
Brilliant !
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 ?
Thanks!


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 ?
