windows 在路径上定位文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/195590/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Locating a file on the path
提问by ljs
Does anybody know how to determine the location of a file that's in one of the folders specified by the PATH environmental variable other than doing a dir filename.exe /s from the root folder?
除了从根文件夹执行 dir filename.exe /s 之外,有人知道如何确定 PATH 环境变量指定的文件夹之一中文件的位置吗?
I know this is stretching the bounds of a programming question but this is useful for deployment-related issues, also I need to examine the dependencies of an executable. :-)
我知道这超出了编程问题的范围,但这对于与部署相关的问题很有用,我还需要检查可执行文件的依赖关系。:-)
回答by MvdD
You can use the where.exe
utility in the C:\Windows\System32
directory.
您可以使用目录中的where.exe
实用程序C:\Windows\System32
。
回答by Patrick Cuff
For WindowsNT-based systems:
对于基于 WindowsNT 的系统:
for %i in (file) do @echo %~dp$PATH:i
Replace file
with the name of the file you're looking for.
替换file
为您要查找的文件的名称。
回答by Nick
If you want to locate the file at the API level, you can use PathFindOnPath. It has the added bonus of being able to specify additional directories, in case you want to search in additional locations apart from just the system or current user path.
如果要在 API 级别定位文件,可以使用PathFindOnPath。如果您想在除系统或当前用户路径之外的其他位置进行搜索,它具有能够指定其他目录的额外好处。
回答by ChadsTech
Using PowerShell on Windows...
在 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]" } }
I also blogged the answer with some details over at http://blogs.catapultsystems.com/chsimmons/archive/2018/01/30/parse-envpath-with-powershell
我还在http://blogs.catapultsystems.com/chsimmons/archive/2018/01/30/parse-envpath-with-powershell 上写了一些详细的答案
回答by Kris
On windows i'd say use %WINDIR%\system32\where.exe
在 Windows 上我会说使用 %WINDIR%\system32\where.exe
Your questions title doesn't specify windows so I imagine some folks might find this question looking for the same with a posix OS on their mind (like myself).
您的问题标题没有指定窗口,所以我想有些人可能会发现这个问题在他们的脑海中寻找与 posix 操作系统相同的问题(比如我自己)。
This php snippet might help them:
这个 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;
}
?>
This is slightly altered production code I'm running on several servers. (i.e. taken out of OO context and left some sanitation and error checking out for brevity.)
这是我在多台服务器上运行的略微更改的生产代码。(即从面向对象的上下文中取出并留下一些卫生和错误检查以简洁起见。)
回答by Richard T
In addition to the 'which' (MS Windows) and 'where' (unix/linux) utilities, I have written my own utility which I call 'findinpath'. In addition to finding theexecutable that would be executed, if handed to the command line interpreter (CLI), it will find all matches, returned path-search-order so you can find path-order problems. In addition, my utility returns not just executables, but any file-specification match, to catch those times when a desired file isn't actually executable.
除了“which”(MS Windows)和“where”(unix/linux)实用程序之外,我还编写了自己的实用程序,我称之为“findinpath”。除了查找将要执行的可执行文件之外,如果交给命令行解释器 (CLI),它将查找所有匹配项,返回路径搜索顺序,以便您可以找到路径顺序问题。此外,我的实用程序不仅返回可执行文件,还返回任何文件规范匹配,以捕获所需文件实际上不是可执行文件的时间。
I also added a feature that has turned out to be very nifty; the -s flag tells it to search not just the system path, but everything on the system disk, known user-directories excluded. I have found this feature to be incredibly useful in systems administration tasks...
我还添加了一个非常漂亮的功能;-s 标志告诉它不仅要搜索系统路径,还要搜索系统磁盘上的所有内容,已知用户目录除外。我发现这个功能在系统管理任务中非常有用......
Here's the 'usage' output:
这是“使用”输出:
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
Writing such a utility is not hard and I'll leave it as an exercise for the reader. Or, if asked here, I'll post my script - its in 'bash'.
编写这样的实用程序并不难,我将其作为练习留给读者。或者,如果在这里被问到,我会发布我的脚本 - 它在“bash”中。
回答by Scott Weinstein
just for kicks, here's a one-liner powershell implementation
只是为了踢球,这是一个单行的 powershell 实现
function PSwhere($file) { $env:Path.Split(";") | ? { test-path $_$file* } }