码迷,mamicode.com
首页 > 其他好文 > 详细

获取可执行程序(.exe)完整路径

时间:2016-07-02 14:36:31      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:程序路径

:::::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


本文出自 “记不起从前杯酒” 博客,请务必保留此出处http://9429042.blog.51cto.com/9419042/1795123

获取可执行程序(.exe)完整路径

标签:程序路径

原文地址:http://9429042.blog.51cto.com/9419042/1795123

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!