我有一个命令的名称,我需要检查这个命令是否是内部命令。在批处理脚本中该如何实现?
经过大量调整,多亏了 @Andriy M 的帮助,现在终于可以正常工作了。
@ECHO off
CALL :isInternalCommand dir dirInternal
ECHO is dir internal: %dirInternal%
CALL :isInternalCommand find findInternal
ECHO is find internal: %findInternal%
exit /b 0
:isInternalCommand
SETLOCAL
MKDIR %TEMP%\EMPTY_DIR_FOR_TEST > NUL 2>& 1
CD /D %TEMP%\EMPTY_DIR_FOR_TEST
SET PATH=
%~1 /? > NUL 2>&1
IF ERRORLEVEL 9009 (ENDLOCAL
SET "%~2=no"
) ELSE (ENDLOCAL
SET "%~2=yes"
)
GOTO :EOF
您可以使用 where
命令。如果失败,该命令可能是内部命令。如果成功,您将获得可执行路径,证明它不是内部命令。
C:\Users\user>where path
INFO: Could not find files for the given pattern(s).
C:\Users\user>where find
C:\Windows\System32\find.exe
编辑:正如评论所提到的,如果你只是需要研究而不是可移植性,这可能不是最好的解决方案。因此,这里有另一个可能的解决方案。
将%PATH%
设置为空,这样HELP
就无法找到任何东西,然后运行HELP
命令来检查你想要检查的命令。
C:\Users\user>set PATH=
C:\Users\user>path
PATH=(null)
C:\Users\user>%WINDIR%\System32\help del
Deletes one or more files.
DEL [/P] [/F] [/S] [/Q] [/A[[:]attributes]] names
[...]
C:\Users\user>%WINDIR%\System32\help find
'find' is not recognized as an internal or external command,
operable program or batch file.
如果命令没有帮助信息,这种方法可能仍然失败。
编辑2:不用管了,这个方法也不行。两种情况都会返回%ERRORLEVEL%=1
。
path
的程序,则 where path
报告错误。 - cHaoHELP FIND
->“'help'不是内部或外部命令,也不是可运行的程序或批处理文件。” (哎呀...) - Andriy M%WINDIR%\system32\HELP
而不是只运行HELP
的原因。 - kichikfind /?
返回了 9009。所以,是的,这是一种稍微不同的方法(没有使用 help
),但基本思路仍然有效(我想)。 - Andriy Mkichik提供了一个很好的答案。但是,如果当前目录中恰好有与提供的命令名称匹配的可执行文件或批处理脚本,则可能会出现误报。
我所能想到的避免这个问题的唯一方法是在%TEMP%
目录中创建一个已知为空的文件夹,然后从该文件夹运行测试。
这是kichik解决方案的修改版本,应该可以正常工作。
@echo off
setlocal
::Print the result to the screen
call :isInternal find
call :isInternal dir
::Save the result to a variable
call :isInternal find resultFind
call :isInternal dir resultDir
set result
exit /b
:isInternal command [rtnVar]
setlocal
set "empty=%temp%\empty%random%"
md "%empty%"
pushd "%empty%"
set path=
>nul 2>nul %1 /?
if errorlevel 9009 (set rtn=not internal) else (set rtn=internal)
popd
rd "%empty%"
(
endlocal
if "%~2" neq "" (set %~2=%rtn%) else echo %1 is %rtn%
)
exit /b 0
@echo off
setlocal enableDelayedExpansion
set "empty=%temp%\empty%random%"
md "%empty%"
pushd "%empty%"
for /f "delims= " %%A in ('help^|findstr /rc:"^[^ ][^ ]* "') do call :test %%A
popd
rd "%empty%"
exit /b
:test
setlocal
set path=
%1 /? >nul 2>nul
if not errorlevel 9009 echo %1
exit /b 0