【发布时间】:2012-06-16 02:22:38
【问题描述】:
我想知道 CMD shell 使用哪个版本的可执行文件。在任何 unix shell 中,我都会使用 which 来找到它。
Windows shell 中是否有等效命令?
【问题讨论】:
标签: powershell cmd
我想知道 CMD shell 使用哪个版本的可执行文件。在任何 unix shell 中,我都会使用 which 来找到它。
Windows shell 中是否有等效命令?
【问题讨论】:
标签: powershell cmd
各种。
where 是直接等价的:
C:\Users\Joey>where cmd
C:\Windows\System32\cmd.exe
注意,在PowerShell中where本身就是Where-Object的别名,因此你需要在PowerShell中使用where.exe。
在cmd你也可以使用for:
C:\Users\Joey>for %x in (powershell.exe) do @echo %~$PATH:x
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
在 PowerShell 中,您有 Get-Command 及其别名 gcm,如果您传递一个参数(但也适用于 PowerShell 中的别名、cmdlet 和函数),它们的作用相同:
PS C:\Users\Joey> Get-Command where
CommandType Name Definition
----------- ---- ----------
Alias where Where-Object
Application where.exe C:\Windows\system32\where.exe
第一个返回的命令是要执行的。
【讨论】:
where 对我根本不起作用(什么也没打印并立即退出),但 gcm 效果很好。
Get-Command where 的示例输出中收集到,where 实际上是 Where-Object 的别名,它优先于 where.exe。要运行where.exe,您必须输入where.exe。至少在 PowerShell 中。
WHERE 命令与 unix which 不太一样,因为它列出了在当前目录或 PATH 中找到的所有匹配文件。正如乔伊所说,列出的第一个是执行的那个。创建一个只返回第一个找到的批处理脚本很简单。
@echo off
for /f "delims=" %%F in ('where %1') do (
echo %%F
exit /b
)
但是WHERE比较慢。
下面是一个速度更快、功能更多的 WHICH.BAT 脚本。它使用广泛的延迟扩展切换,因为:1) 如果存在未引用的特殊字符,则扩展 %PATH% 是不可靠的。 2) 在启用延迟扩展时扩展 FOR 变量会损坏包含 ! 的值。
::WHICH.BAT CommandName [ReturnVar]
::
:: Determines the full path of the file that would execute if
:: CommandName were executed.
::
:: The result is stored in variable ReturnVar, or else it is
:: echoed to stdout if ReturnVar is not specified.
::
:: If no file is found, then an error message is echoed to stderr.
::
:: The ERRORLEVEL is set to one of the following values
:: 0 - Success: A matching file was found
:: 1 - CommandName is an internal command
:: 2 - No file was found and CommandName is not an internal command
:: 3 - Improper syntax - no CommandName specified
::
@echo off
setlocal disableDelayedExpansion
set "file=%~1"
setlocal enableDelayedExpansion
if not defined file (
>&2 echo Syntax error: No CommandName specified
exit /b 3
)
:: test for internal command
echo(!file!|findstr /i "[^abcdefghijklmnopqrstuvwxyz]" >nul || (
set "empty=!temp!\emptyFolder"
md "!empty!" 2>nul
del /q "!empty!\*" 2>nul >nul
setlocal
pushd "!empty!"
set path=
(call )
!file! /? >nul 2>nul
if not errorlevel 9009 (
>&2 echo "!file!" is an internal command
popd
exit /b 1
)
popd
endlocal
)
:: test for external command
set "noExt="
if "%~x1" neq "" if "!PATHEXT:%~x1=!" neq "!PATHEXT!" set noExt="";
set "modpath=.\;!PATH!"
@for %%E in (%noExt%%PATHEXT%) do @for %%F in ("!file!%%~E") do (
setlocal disableDelayedExpansion
if not "%%~$modpath:F"=="" if not exist "%%~$modpath:F\" (
endlocal & endlocal & endlocal
if "%~2"=="" (echo %%~$modpath:F) else set "%~2=%%~$modpath:F"
exit /b 0
)
endlocal
)
endlocal
>&2 echo "%~1" is not a valid command
exit /b 2
更新
我不得不对上面的脚本进行重大修改,因为如果在 PATH 中的某处碰巧存在具有相同根名称的 exe 文件,它会错误地将内部命令列为外部命令。
【讨论】: