- First, never overwrite the environment variable
path
, not even
temporarily. Append your folder instead: set "path=%path%;%myAppPath%"
or set "path=%myAppPath%;%path%"
;
- Note and use proper
"
quotation and no spacing around =
to avoid any leading and trailing spaces in all set
commands.
- There exists a particular
path
command so you could use path %path%;myAppPath%
.
- Use full path to a program if you know it, e.g.
"%myAppPath%\myApp"
.
But how-to get to know full path to a program on unknown machine? Let's use where
command, see examples pasted from my cmd
window:
==> where /F notepad.exe
"C:\Windows\System32\notepad.exe"
"C:\Windows\notepad.exe"
==> where /F pspad.exe
INFO: Could not find files for the given pattern(s).
==> where /F pspad.exe 2>NUL
==> where /R "%ProgramFiles%" /F pspad.exe 2>NUL
==> where /R "%ProgramFiles(x86)%" /F pspad.exe 2>NUL
"C:\Program Files (x86)\PSPad editor\PSPad.exe"
Store path to my application to a variable (last occurrence):
==> set "myAppPath="
==> for /F "delims=" %G in ('where /F notepad.exe 2^>NUL') do @set "myAppPath=%~dpG"
==> set myAppPath
myAppPath=C:\Windows\
(first occurrence):
==> set "myAppPath="
==> for /F "delims=" %G in ('where /F notepad.exe 2^>NUL') do @if not defined myAppPath set "myAppPath=%~dpG"
==> set myAppPath
myAppPath=C:\Windows\System32\
Put it all together in a batch script:
@ECHO OFF
SETLOCAL EnableExtensions
set "myApp=%~1"
if not defined myApp set "myApp=java.exe"
set "myAppPath="
rem search in %path% environment variable
for /F "delims=" %%G in ('
where /F %myApp% 2^>NUL
') do set "myAppPath=%%~dpG" & goto :no1
:no1
rem search in %ProgramFiles% folder recursively
if not defined myAppPath for /F "delims=" %%G in ('
where /R "%ProgramFiles%" /F %myApp% 2^>NUL
') do if not defined myAppPath set "myAppPath=%%~dpG" & goto :no2
:no2
rem search in %ProgramFiles(x86)% folder recursively
if not defined myAppPath (
if defined ProgramFiles^(x86^) for /F "delims=" %%G in ('
where /R "%ProgramFiles(x86)%" /F %myApp% 2^>NUL
') do set "myAppPath=%%~dpG" & goto :no3
)
:no3
rem search in %SystemDrive%\ disk recursively
if not defined myAppPath for /F "delims=" %%G in ('
where /R %SystemDrive%\ /F %myApp% 2^>NUL
') do set "myAppPath=%%~dpG" & goto :no4
:no4
rem populate results
if defined myAppPath (echo could run "%myAppPath%%myapp%") else (
echo '%myapp%' not found in %SystemDrive%\ disk
)
Output:
==> 34039382.bat notepad
could run "C:\Windows\System32\notepad"
==> 34039382.bat iexplore.exe
could run "C:\Program Files\Internet Explorer\iexplore.exe"
==> 34039382.bat pspad.exe
could run "C:\Program Files (x86)\PSPad editor\pspad.exe"
==> 34039382.bat psexec.exe
could run "C:\Utils\Sysinternals\psexec.exe"
==> 34039382.bat
'java.exe' not found in C:\ disk
This oracle article, Deploying the JRE on Windows, states that the Java installer will copy java.exe
into the system directory:
Two copies of the java.exe
executable are installed. One copy is in
the bin
directory of the JRE. The second copy is placed in either
C:\windows\system
or C:\winnt\system32
, depending on the system.
Because of it's location in the system directory, this second copy of
java.exe
can be launched from any directory location without
giving the full path to it.