首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在批处理脚本中找到应用程序的完整路径

如何在批处理脚本中找到应用程序的完整路径
EN

Stack Overflow用户
提问于 2009-09-17 08:25:03
回答 6查看 28.6K关注 0票数 19

如何在批处理脚本中查找应用程序XYZ的完整路径(如果已安装

澄清:

  1. 应用程序不在路径中,我只知道它的名称,本例中为"ISTool.exe“,我想获取C:\Program\ISTool\ISTool.exe
EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2009-09-17 19:42:48

我从别人那里得到的答案有效(但速度慢或使用了额外的文件),并且适用于任何exe,但并不真正满足我的需求。

因为我想找到一个特定的exe,所以我转而使用REG QUERY在注册表中查找。我找到了一个包含我想要查找的数据的密钥,并将其提取出来。

结果是速度很快,只有几行代码,但不是很美观,也不是很可重用。

简短的示例:

代码语言:javascript
复制
@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=后跟一个制表符-字符

票数 0
EN

Stack Overflow用户

发布于 2009-09-17 08:27:50

您可以在path (或其他类似路径的字符串,如有必要)上找到可执行文件:

代码语言:javascript
复制
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 /?"的帮助文本末尾找到,但摘要如下:

代码语言:javascript
复制
%~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.

可以组合修饰符以获得复合结果:

代码语言:javascript
复制
%~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的裸/子目录格式,它将为您完成此操作。从根目录:

代码语言:javascript
复制
dir /b /s ISTool.exe

会帮你找到那个驱动器上的所有文件。然后,您只需解析输出。我自己的偏好是使用Cygwin的"find /cygdrive -name ISTool.exe",但那是因为我已经安装了它。你可能不想那样(或者甚至不想那样做)。

更新:

dir /b /s命令需要一段时间,因为它基本上是在搜索整个磁盘。如果这是一个问题,您可能需要考虑定期为所有磁盘上的所有文件创建一个缓存记录,其中包含cmd文件,如:

代码语言:javascript
复制
@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

您可以在夜间(对于始终在线的服务器)或在引导时(对于台式机)对计划任务执行此操作。您甚至可以使脚本更智能,每隔几天执行一次(我有一个自动备份脚本,可以在我支持的家庭计算机上执行类似的操作)。这将在临时缓存文件中创建列表,然后覆盖原始列表,以确保文件不存在的时间最小化。

然后,您可以只使用:

代码语言:javascript
复制
findstr \\ISTool.exe c:\files.cache

找到你所有的文件。

票数 32
EN

Stack Overflow用户

发布于 2012-09-25 02:21:20

基于这里真正有帮助的答案,我黑了这两批我认为我在这里分享的(我知道这个帖子已经3年了,但在谷歌搜索时发现它是第一个匹配的……):

1) which.bat:

代码语言:javascript
复制
@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:

代码语言:javascript
复制
@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%
票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1437457

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档