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

Nif*_*fle 16 windows batch-file

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

澄清:

  1. 该应用程序不在PATH中
  2. 我只有它的名字在这种情况下"ISTool.exe",我想得到C:\ Program\ISTool\ISTool.exe

pax*_*blo 29

您可以在路径上找到可执行文件(如果需要,可以在其他类似路径的字符串中):

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
Run Code Online (Sandbox Code Playgroud)

可以在"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.
Run Code Online (Sandbox Code Playgroud)

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

%~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.
Run Code Online (Sandbox Code Playgroud)

如果您的可执行文件不在路径上(根据您的编辑),您最好的选择是使用裸/子目录格式dir为您执行此操作.从根目录:

dir /b /s ISTool.exe
Run Code Online (Sandbox Code Playgroud)

将使用该名称获取该驱动器上的所有文件.然后你只需要解析输出.我自己的偏好是使用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
Run Code Online (Sandbox Code Playgroud)

您可以在每晚(对于永远在线的服务器)或在引导(对于桌面)的预定任务中执行此操作.您甚至可以让脚本更加智能化,每隔几天就可以完成一次(我有一个自动备份脚本,在我支持的家庭机器上做类似的事情).这会在临时缓存文件中创建列表,然后覆盖原始缓存文件,以确保最小化文件不存在的时间.

然后你可以使用:

findstr \\ISTool.exe c:\files.cache
Run Code Online (Sandbox Code Playgroud)

找到所有文件.


小智 8

基于这里真正有用的答案,我把这两个批次打包了,我认为我在这里分享(我知道这个帖子现在已经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
    )
  )
)
Run Code Online (Sandbox Code Playgroud)

不完美,因为总共有两个测试,但它足够快,所以我没有进一步打扰; 确定它可以只用%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%
Run Code Online (Sandbox Code Playgroud)


Nif*_*fle 0

我从其他人那里得到的答案有效(但速度慢或使用了额外的文件)并且适用于任何 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
Run Code Online (Sandbox Code Playgroud)

这会导致"C:\Program\ISTool\ISTool.exe"(带引号)
注意:上面的 delims= 后面跟着一个制表符