2008-10-12 109 views
7

有没有人知道如何确定PATH环境变量指定的文件夹之一的文件位置,而不是从根文件夹中执行dir filename.exe/s?查找路径上的文件

我知道这是一个编程问题的范围,但这对于与部署相关的问题很有用,我还需要检查可执行文件的依赖关系。 :-)

回答

31

您可以在C:\Windows\System32目录中使用where.exe实用程序。

+0

谢谢 - 短暂而甜蜜的,我知道有一个简单的命令行工具,它...! – ljs 2008-10-12 15:20:00

+0

对于什么操作系统?我无法在Windows XP系统上的任何位置找到“where.exe”。 – 2008-10-13 12:18:26

+1

WHERE.EXE随Windows XP Server 2003一起提供,以及自Win2K以来的Windows资源工具包。它也包含在VS2005中,但不包括2008(C:\ Program Files \ Microsoft Visual Studio 8 \ Common7 \ Tools \ Bin \ Where.Exe)。 – raven 2008-10-13 18:42:12

1

在Windows上我说,所以我想象有些人可能会发现这个问题,寻找与他们心目中的POSIX OS(像我一样),同样使用%WINDIR%\system32\where.exe

您的问题标题不指定窗口。

这个PHP代码片段可能会帮助他们:

<?php 
function Find($file) 
{ 
    foreach(explode(':', $_ENV('PATH')) as $dir) 
    { 
     $command = sprintf('find -L %s -name "%s" -print', $dir, $file); 
     $output = array(); 
     $result = -1; 
     exec($command, $output, $result); 

     if (count($output) == 1) 
     { 
      return($output[ 0 ]); 
     } 
    } 
    return null; 
} 
?> 

这是稍微改变生产代码,我在几个服务器上运行。 (即取出OO上下文并留下了一些卫生和错误检查出的简洁。)

+0

标签声明窗口。 – jop 2008-10-12 15:43:58

-1

只是踢,这里是一个一行的PowerShell实现

function PSwhere($file) { $env:Path.Split(";") | ? { test-path $_\$file* } } 
0

除了“的”(MS Windows)和'where'(unix/linux)工具,我写了自己的工具,我称之为'findinpath'。除了找到要执行的可执行文件,如果交给命令行解释程序(CLI),它将查找所有匹配项,并返回path-search-order,以便查找路径顺序问题。此外,我的实用程序不仅返回可执行文件,而且还返回任何文件规范匹配,以便在所需文件实际不可执行时捕获那些时间。

我还添加了一个功能,它变成了非常漂亮; -s标志告诉它不仅搜索系统路径,而且还搜索系统磁盘上的所有内容,排除已知的用户目录。我发现这个功能是系统管理任务非常有用......

这里的“使用”输出:

usage: findinpath [ -p <path> | -path <path> ] | [ -s | -system ] <file> 
    or findinpath [ -h | -help ] 

where: <file> may be any file spec, including wild cards 

     -h or -help returns this text 

     -p or -path uses the specified path instead of the PATH environment variable. 

     -s or -system searches the system disk, skipping /d /l/ /nfs and /users 

编写这样一个实用程序并不难,我会离开它作为一个练习为读者。或者,如果在这里问,我会发布我的脚本 - 它在'bash'。

2

如果要在API级别找到文件,可以使用PathFindOnPath。它还有额外的好处,可以指定其他目录,以防您想在除了系统或当前用户路径之外的其他位置进行搜索。

4

对于基于WindowsNT的系统:

for %i in (file) do @echo %~dp$PATH:i 

替换file与您要查找的文件的名称。

1

在Windows上使用PowerShell ...

Function Get-ENVPathFolders { 
#.Synopsis Split $env:Path into an array 
#.Notes 
# - Handle 1) folders ending in a backslash 2) double-quoted folders 3) folders with semicolons 4) folders with spaces 5) double-semicolons i.e. blanks 
# - Example path: 'C:\WINDOWS\;"C:\Path with semicolon; in the middle";"E:\Path with semicolon at the end;";;C:\Program Files; 
# - 2018/01/30 by [email protected] - Created 
$NewPath = @() 
$env:Path.ToString().TrimEnd(';') -split '(?=["])' | ForEach-Object { #remove a trailing semicolon from the path then split it into an array using a double-quote as the delimeter keeping the delimeter 
    If ($_ -eq '";') { # throw away a blank line 
    } ElseIf ($_.ToString().StartsWith('";')) { # if line starts with "; remove the "; and any trailing backslash 
     $NewPath += ($_.ToString().TrimStart('";')).TrimEnd('\') 
    } ElseIf ($_.ToString().StartsWith('"')) { # if line starts with " remove the " and any trailing backslash 
     $NewPath += ($_.ToString().TrimStart('"')).TrimEnd('\') #$_ + '"' 
    } Else {         # split by semicolon and remove any trailing backslash 
     $_.ToString().Split(';') | ForEach-Object { If ($_.Length -gt 0) { $NewPath += $_.TrimEnd('\') } } 
    } 
} 
Return $NewPath 
} 

$myFile = 'desktop.ini' 
Get-ENVPathFolders | ForEach-Object { If (Test-Path -Path $_\$myFile) { Write-Output "Found [$_\$myFile]" } } 

我也用博客的一些细节回答了在http://blogs.catapultsystems.com/chsimmons/archive/2018/01/30/parse-envpath-with-powershell