Read Command Line arguments (Parameters) or call /?
:
…
If Command Extensions are enabled CALL changes as follows:
…
In addition, expansion of batch script argument references (%0, %1,
etc.) have been changed as follows:
…
%~$PATH:1 - searches the directories listed in the PATH
environment variable and expands %1 to the fully
qualified name of the first one found. If the
environment variable name is not defined or the
file is not found by the search, then this
modifier expands to the empty string
…
The FOR
command creates parameter variables which are identified
with a letter rather than a number (e.g. %%G
). The Parameter
Expansions described above can also be applied to these.
Applying above to your case (note doubled percent sigh in %%g
for using in a .bat
or .cmd
script):
for %%g in ("bin\myapp.exe") do @set "_pathToMyApp=%%~$PATH:g"
rem ↑↑ NO leading backslash
rem next line calls `myapp.exe` using its fully qualified name
"%_pathToMyApp%"
A real example: (note single percent sigh in %g
for using from command prompt in an open cmd
window). My PATH
variable reads as …;C:\Utils\;…
with no further reference to C:\Utils
folder (base directory), myapp.exe
is a simple application which displays all supplied parameters:
d:\bat> myapp.exe
'myapp.exe' is not recognized as an internal or external command,
operable program or batch file.
d:\bat> bin\myapp.exe
The system cannot find the path specified.
d:\bat> set _
Environment variable _ not defined
d:\bat> for %g in ("bin\myapp.exe") do @set "_pathToMyApp=%~$PATH:g"
d:\bat> set _
_pathToMyApp=C:\Utils\bin\myapp.exe
d:\bat> "%_pathToMyApp%" display all parameters
param 0 = C:\Utils\bin\myapp.exe
param 1 = display
param 2 = all
param 3 = parameters
d:\bat>