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

时间:2009-09-17 08:25:03

标签: windows batch-file

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

澄清:

  1. 该应用程序不在PATH
  2. 我所拥有的就是它的名字“ISTool.exe”,我想得到 C:\ Program \ ISTool \ ISTool.exe

6 个答案:

答案 0 :(得分:28)

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

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.

如果您的可执行文件不在路径上(根据您的编辑),您最好的选择是使用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

找到所有文件。

答案 1 :(得分: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
    )
  )
)

并不完美,因为总共有两个测试,但它足够快,所以我没有进一步打扰;确定第1次仅使用%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%

答案 2 :(得分:1)

这是我最接近的。一个缺点是它每次执行仅适用于一个驱动器,但这可以更灵活。另一个是输出,它在路径和文件名之间始终包含//。但根据定义,这是一条有效的道路。

@ECHO OFF

SET filename=autoexec.bat

FOR /R C:\ %%a IN (\) DO (
   IF EXIST "%%a\%filename%" (

      SET fullpath=%%a%filename%
      GOTO break
   )
)
:break

ECHO %fullpath%

将发送:C:\\autoexec.bat

修改

为了便于说明,for循环遍历从给定路径(C:\)开始的所有目录,并检查该目录中是否存在文件名。如果是,则将两个变量连接起来并存储在%fullpath%中,并通过跳转终止循环。

答案 3 :(得分: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

这导致"C:\Program\ISTool\ISTool.exe"(带引号)
注意:delims = above后面是tab-char

答案 4 :(得分:0)

有时这个简单的解决方案有效,您可以检查输出是否符合您的预期。第一行运行命令并获取标准输出的最后一行。

FOR /F "tokens=*" %%i in (' "xcopy /? 2> nul" ') do SET xcopyoutput=%%i
if "%xcopyoutput%"=="" echo xcopy not in path.

答案 5 :(得分:0)

或者,EverythingUltraSearch(免费软件),SwiftSearch等程序可以在MFT(NTFS分区)中搜索文件(因此可以这样做非常很快),(但是维基百科声称这种事情可以通过查找它不应该发生的事情来破坏你的安全模型) - 其中一些看起来像是有一些命令行参数,我没有使用它们,但是也许它可能会有所帮助,如果你正在寻求完整的驱动器搜索。