[20251209]windows如何确定执行文件所在的目录.txt
--//windows如何确定执行文件所在的目录,一定是顺着环境变量PATH定义逐个寻找。
d:\>echo %PATH%| tr ; "\n"
E:\tools\database21c\bin
D:\tools\linux\usr\local\wbin
C:\Windows\system32
C:\Windows
C:\Windows\System32\Wbem
C:\Windows\System32\WindowsPowerShell\v1.0\
C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common
d:\tools\bat
C:\Program Files (x86)\Vagrant\bin
C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps
--//linux可以使用which或者type命令查找。而windows呢?看了一些文档,发现使用where命令可以实现类似功能。
C:\>where /?
WHERE [/R dir] [/Q] [/F] [/T] pattern...
描述:
显示符合搜索模式的文件位置。在默认情况下,搜索是在当前目录和 PATH
环境变量指定的路径中执行的。
参数列表:
/R 从指定目录开始,递归性搜索并显示符合指定模式的文件。
/Q 只返回退出代码,不显示匹配文件列表。(安静模式)
匹配文件。(安静模式)
/F 显示所有相配文件并用双引号括上。
/T 显示所有相配文件的文件的文件。
pattern 指定要匹配的文件的搜索模式。通配符 * 和 ? 可以用在模式中。
也可以指定 "$env:pattern" 和 "path:pattern" 格式; 其中
"env" 是环境变量,搜索是在 "env" 变量的指定的路径中执行的。
这些格式不应该跟 /R 一起使用。此搜索也可以用将 PATHEXT 变
量扩展名附加于此模式的方式完成。
/? 显示此帮助消息。
注意: 如果搜索成功,此工具返回错误级别 0; 如果不成功,返回 1; 如果失
败或发生错误,返回 2。
示例:
WHERE /?
WHERE myfilename1 myfile????.*
WHERE $windir:*.*
WHERE /R c:\windows *.exe *.dll *.bat
WHERE /Q ??.???
WHERE "c:\windows;c:\windows\system32:*.dll"
WHERE /F /T *.dll
--//发现windows支持模糊查询。
C:\>where zip
E:\tools\database21c\bin\zip.exe
D:\tools\linux\usr\local\wbin\zip.exe
--//安装UnxUtils里面包含which命令,也就是windows应该可以实现which查询。
C:\>which zip
which: no zip in (E:\tools\database21c\bin;D:\tools\linux\usr\local\wbin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program
m Files (x86)\Vagrant\bin;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;)
--//linux的命令的惯性思维,必须输入后缀。
C:\>which
Usage: which [options] [--] programname [...]
Options: --version, -[vV] Print version and exit successfully.
--skip-dot Skip directories in PATH that start with a dot.
--skip-tilde Skip directories in PATH that start with a tilde.
--show-dot Don't expand a dot to current directory in output.
--show-tilde Output a tilde for HOME directory for non-root.
--tty-only Stop processing options on the right if not on tty.
--all, -a Print all matches in PATH, not just the first
C:\>which zip.exe
E:\tools\database21c\bin\zip.exe
--//仅仅查询一个满足条件的就停止扫描。
C:\>which -a zip.exe
E:\tools\database21c\bin\zip.exe
D:\tools\linux\usr\local\wbin\zip.exe
--//不熟悉windows的一些命令做一个记录。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/267265/viewspace-3105151/ ,如需转载,请注明出处,否则将追究法律责任。