Page tree

Welcome to FreeSoftwareServers Confluence Wiki

Skip to end of metadata
Go to start of metadata

https://stackoverflow.com/questions/22472253/how-to-find-microsoft-office-path-from-command-line/63256191#63256191

https://superuser.com/questions/49104/how-do-i-find-the-location-of-an-executable-in-windows/1575281#1575281

https://stackoverflow.com/questions/304319/is-there-an-equivalent-of-which-on-the-windows-command-line

https://stackoverflow.com/questions/43934167/how-to-get-only-the-user-path-variable

Combo!

app_path_func.cmd:

@ECHO OFF
CLS

FOR /F "skip=2 tokens=1,2*" %%N IN ('reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\%~1" /v "Path" 2^>nul') DO (
 IF /I "%%N" == "Path" (
  SET wherepath=%%P%~1
  GoTo Found
 )
)

FOR /F "tokens=* USEBACKQ" %%F IN (`where.exe %~1`) DO (
 SET wherepath=%%F
 GoTo Found
)

FOR /F "tokens=* USEBACKQ" %%F IN (`where.exe /R "%PROGRAMFILES%" %~1`) DO (
 SET wherepath=%%F
 GoTo Found
)

FOR /F "tokens=* USEBACKQ" %%F IN (`where.exe /R "%PROGRAMFILES(x86)%" %~1`) DO (
 SET wherepath=%%F
 GoTo Found
)

FOR /F "tokens=* USEBACKQ" %%F IN (`where.exe /R "%WINDIR%" %~1`) DO (
 SET wherepath=%%F
 GoTo Found
)

:Found
SET %2=%wherepath%
:End

Test:

@ECHO OFF
CLS

CALL "app_path_func.cmd" WINWORD.EXE PROGPATH
ECHO %PROGPATH%

PAUSE

Result:

C:\Program Files (x86)\Microsoft Office\Office15\WINWORD.EXE
Press any key to continue . . .

Find via RegQuery (Faster!)

reg_app_path.cmd:
@ECHO OFF
CLS

FOR /F "tokens=* USEBACKQ" %%F IN (`reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\%~1" ^| findstr /C:"Program Files"`) DO (
SET REGSTR=%%F
)
SET %1=%REGSTR:*REG_SZ    =%

Test:

@ECHO OFF
CLS

CALL "reg_app_path.cmd" WINWORD.EXE PROGPATH
::CALL "where_prog_file.cmd" WINWORD.EXE PROGPATH
ECHO %PROGPATH%

PAUSE

Result

C:\Program Files (x86)\Microsoft Office\Office15\
Press any key to continue . . .

Perhaps you prefer "where" for actually searching for the file:

where_prog_path.cmd:

@ECHO OFF
CLS

FOR /F "tokens=* USEBACKQ" %%F IN (`where.exe /R "%PROGRAMFILES%" %~1`) DO (
SET wherepath=%%F
)

if "%wherepath%"=="" (
FOR /F "tokens=* USEBACKQ" %%F IN (`where.exe /R "%PROGRAMFILES(x86)%" %~1`) DO (
SET wherepath=%%F
)
)

SET %2=%wherepath%

Test:

@ECHO OFF
CLS

::CALL "win_office_path.cmd" WINWORD.EXE PROGPATH
CALL "where_prog_file.cmd" WINWORD.EXE PROGPATH
ECHO %PROGPATH%

PAUSE

Result:

INFO: Could not find files for the given pattern(s).
C:\Program Files (x86)\Microsoft Office\root\Office16\WINWORD.EXE
Press any key to continue . . .
  • No labels