如何在批处理脚本中查找应用程序XYZ的完整路径(如果已安装
澄清:
发布于 2009-09-17 19:42:48
我从别人那里得到的答案有效(但速度慢或使用了额外的文件),并且适用于任何exe,但并不真正满足我的需求。
因为我想找到一个特定的exe,所以我转而使用REG QUERY在注册表中查找。我找到了一个包含我想要查找的数据的密钥,并将其提取出来。
结果是速度很快,只有几行代码,但不是很美观,也不是很可重用。
简短的示例:
@ECHO off
SETLOCAL
set found=
FOR /F "tokens=1-3 delims= " %%a IN ('REG QUERY "HKEY_CLASSES_ROOT\Applications\ISTool.exe\shell\OpenWithISTool\command"') DO (
set found=%%c
)
for /f "tokens=1-2" %%a in ("%found%") do (
set my_exe=%%a
)
echo %my_exe%
ENDLOCAL
这会产生"C:\Program\ISTool\ISTool.exe"
(带引号)
注意:上面的delims=后跟一个制表符-字符
发布于 2009-09-17 08:27:50
您可以在path (或其他类似路径的字符串,如有必要)上找到可执行文件:
c:\> for %i in (cmd.exe) do @echo. %~$PATH:i
C:\WINDOWS\system32\cmd.exe
c:\> for %i in (python.exe) do @echo. %~$PATH:i
C:\Python25\python.exe
详细信息可在"for"
命令"for /?"
的帮助文本末尾找到,但摘要如下:
%~i - expands %i removing any surrounding quotes.
%~fi - expands %i to a fully qualified path name.
%~di - expands %i to a drive letter only.
%~pi - expands %i to a path only.
%~ni - expands %i to a file name only.
%~xi - expands %i to a file extension only.
%~si - expanded path contains short names only.
%~ai - expands %i to file attributes of file.
%~ti - expands %i to date/time of file.
%~zi - expands %i to size of file.
%~$P:i - searches the directories listed in the P environment variable
and expands %i 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.
可以组合修饰符以获得复合结果:
%~dpi - expands %i to a drive letter and path only.
%~nxi - expands %i to a file name and extension only.
%~fsi - expands %i to a full path name with short names only.
%~dp$P:i - searches the directories listed in the P environment variable
for %i and expands to the drive letter and path of the first
one found.
%~ftzai - expands %i to a DIR like output line.
如果您的可执行文件不在path中(根据您的编辑),您最好的选择是使用dir
的裸/子目录格式,它将为您完成此操作。从根目录:
dir /b /s ISTool.exe
会帮你找到那个驱动器上的所有文件。然后,您只需解析输出。我自己的偏好是使用Cygwin的"find /cygdrive -name ISTool.exe"
,但那是因为我已经安装了它。你可能不想那样(或者甚至不想那样做)。
更新:
dir /b /s
命令需要一段时间,因为它基本上是在搜索整个磁盘。如果这是一个问题,您可能需要考虑定期为所有磁盘上的所有文件创建一个缓存记录,其中包含cmd文件,如:
@echo off
setlocal enableextensions enabledelayedexpansion
del c:\files.cache.tmp >nul: 2>nul:
for %%d in (c d e) do (
cd /d %%d:\
dir /b /s >>c:\files.cache.tmp
)
del c:\files.cache >nul: 2>nul:
move c:\files.cache.tmp c:\files.cache
endlocal
您可以在夜间(对于始终在线的服务器)或在引导时(对于台式机)对计划任务执行此操作。您甚至可以使脚本更智能,每隔几天执行一次(我有一个自动备份脚本,可以在我支持的家庭计算机上执行类似的操作)。这将在临时缓存文件中创建列表,然后覆盖原始列表,以确保文件不存在的时间最小化。
然后,您可以只使用:
findstr \\ISTool.exe c:\files.cache
找到你所有的文件。
发布于 2012-09-25 02:21:20
基于这里真正有帮助的答案,我黑了这两批我认为我在这里分享的(我知道这个帖子已经3年了,但在谷歌搜索时发现它是第一个匹配的……):
1) which.bat:
@echo off
REM emulate the Linux which command
if "%1" == "" (
echo Usage: %~nx0 ^<command[.ext]^>
exit /b
)
setlocal
for %%P in (%PATHEXT%) do (
for %%I in (%1 %1%%P) do (
if exist "%%~$PATH:I" (
echo %%~$PATH:I
exit /b
)
)
)
不完美,因为总是有两个测试,但它足够快,所以我没有更多的麻烦;当然可以第一次做一个单独的测试只有%1…
2) findfile.bat:
@echo off
REM emulate the Linux find command
if "%1" == "" (
echo Usage: %~nx0 ^<startdir^> ^<file^>
exit /b
)
setlocal
for /f "delims=" %%A in ('dir /b /s %1\%2') do set F=%%A
if exist "%F%" echo %F%
https://stackoverflow.com/questions/1437457
复制相似问题