:::::usage: getPathOfProgram.bat PROGRAM
:::::Result: output pathOfProgram
@echo off
setlocal enabledelayedexpansion
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::if havenot the first parameter,output the Usage and exit with the exitcode 1
if [%1] equ [] (
call :showHelp %0
exit /b 1
)
if [%1] equ [/?] (
call :showHelp %0
exit /b 1
)
set pathOfAllProgram=%temp%\pathOfAllProgram
:\\\flush cache file
if [%1] equ [/f] (
if exist !pathOfAllProgram! (
del !pathOfAllProgram! && echo flush cache successfully
) else (
echo No cache file,donnot need to flush.
)
exit /b 0
)
::get program.exe
set pro=%1.exe
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::find gived program‘s path from reg,find reg path: regPath=hklm\software\microsoft\windows\currentversion\app paths\
set regPath=hklm\software\microsoft\windows\currentversion\app paths\
::set random file to store outputfile
set tempfile=%temp%\%time::=_%
::find gived program path from reg
reg query "%regPath%%pro%" 2>nul | find /i "%pro%" >> %tempfile%
if %errorlevel% equ 0 (
for /f "skip=1 tokens=1,2*" %%a in (%tempfile%) do set pathOfProgram=%%c
echo !pathOfProgram!
del %tempfile%
exit /b 0
)
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::find gived program‘s path from all disk
::%temp%\pathOfAllProgram store all EXE file of this computer
::if havenot this file,it means havenot collect all EXE files in this computer,then excute underside code to collect all excute files,else find gived EXE file in %temp%\pathOfAllProgram
::collect EXE files in each drive
if not exist !pathOfAllProgram! (
for /f "tokens=* skip=1" %%a in (‘Wmic Logicaldisk Where "DriveType=3" Get Name‘) do (
call :listFiles %%a
)
)
for /f "skip=2 tokens=*" %%a in (‘find /i "\%1.exe" !pathOfAllProgram!‘) do (
if [%%a] neq [] set pathOfProgram=%%a
echo !pathOfProgram!
exit /b 0
)
goto :eof
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:listFiles
:::give this label a parameter: drive\
:::for example: call :listFiles c:\
:::the label will collect all EXE files in gived drive,and store all files to %temp%\pathOfAllProgram
if [%1] neq [] (
for /r %1\ %%a in (*.exe) do echo %%a >> !pathOfAllProgram!
)
goto :eof
:showHelp
echo 获取指定可运行程序的完整路径
echo.
echo Usage:
echo %1 PROGRAM
echo %1 /f 刷新缓存文件,重新查找所有可执行文件
echo.
goto :eof