2008-10-12 111 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