加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

windows – 检查命令是否在CMD内部

发布时间:2020-12-14 02:09:47 所属栏目:Windows 来源:网络整理
导读:我有一个命令的名字,我需要检查这个命令是否是内部的.如何在批处理脚本中执行此操作? 解决方法 经过大量的调整,并且感谢@Andriy M的帮助,它终于有效了. @ECHO offCALL :isInternalCommand dir dirInternalECHO is dir internal: %dirInternal%CALL :isInter
我有一个命令的名字,我需要检查这个命令是否是内部的.如何在批处理脚本中执行此操作?

解决方法

经过大量的调整,并且感谢@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

旧解决方案

你可以在哪里使用.如果失败,该命令可能是内部的.如果成功,您将获得证明它不是内部的可执行路径.

C:Usersuser>where path
INFO: Could not find files for the given pattern(s).

C:Usersuser>where find
C:WindowsSystem32find.exe

编辑:正如评论所示,如果您正在寻找便携性而不仅仅是研究,这可能不是最好的解决方案.所以这是另一种可能的解决方案.

将%PATH%设置为空,以便HELP找不到任何内容,然后对您要检查的命令运行HELP.

C:Usersuser>set PATH=

C:Usersuser>path
PATH=(null)

C:Usersuser>%WINDIR%System32help del
Deletes one or more files.

DEL [/P] [/F] [/S] [/Q] [/A[[:]attributes]] names
[...]

C:Usersuser>%WINDIR%System32help find
'find' is not recognized as an internal or external command,operable program or batch file.

如果命令没有帮助,这可能仍会失败.

编辑2:没关系,这也行不通.两种情况都返回%ERRORLEVEL%= 1.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读