我试图制作一个像这样的脚本which
。所以我想检查这个命令是内部命令还是外部命令或两者兼而有之。我尝试执行程序,但是当程序(例如.exe)不正确时,我的程序退出时出现错误,所以我尝试调用help
对于这个程序。但现在如果我尝试检查例如echo
我有echo.exe
在我当前的目录中,我发现它是外部命令,但不是内部命令。所以我的问题是如何正确地做到这一点。如何检查该程序是内部程序还是外部程序。谢谢。
这是我的代码:
@echo off
setlocal enabledelayedexpansion
if "%1"=="" goto help
:start
if not "%1"=="" (
shift
if "%1"=="/?" goto help
goto :start
)
set arg=%*
for %%a in ("%pathext:;=" "%") do (
echo %arg%|findstr /E /I %%a >nul
rem If file was given with extension
if not ERRORLEVEL 1 goto with_rashr
)
set ext=0
for %%a in ("%pathext:;=" "%") do (
if EXIST "%CD%\!arg!%%~a" (
echo This is an external command: %CD%\!arg!%%~a
set ext=1
goto :internal
)
)
for %%G in ("%path:;=" "%") do (
for %%a in ("%pathext:;=" "%") do (
if EXIST "%%~G\!arg!%%~a" (
echo This is an external command: %%~G\!arg!%%~a
set ext=1
goto :internal
)
)
)
goto :internal
:with_rashr
echo Command with extension was given
if EXIST "%CD%\!arg!" (
echo This is an external command: %CD%\!arg!
set ext=1
goto :internal
)
for %%G in ("%path:;=" "%") do (
if EXIST "%%~G\!arg!" (
echo This is an external command: %%~G\!arg!
set ext=1
goto :internal
)
)
:internal
set PATH=
rem set PATH=%PATH%;%CD%
help %arg% >nul 2>&1
rem set error_check=%ERRORLEVEL%
rem echo %error_check%
rem %arg% /?
rem ERRORLEVEL 9009 when a batch attempts to execute a program that is not found.
rem echo %ext%
if ERRORLEVEL 9009 (
echo We couldn't execute command
if "%ext%"=="0" (
echo This is not a command
)
endlocal
goto :EOF
)
if "%ext%"=="1" (
if ERRORLEVEL 0 (
echo This is also an internal command
)
endlocal
goto :EOF
)
echo This is internal command
endlocal
goto :EOF
:help
echo Like which
echo Shows if this command external or internal
好问题,而且解决起来出奇的困难。
我以为我有一个很好的工作版本http://ss64.org/viewtopic.php?pid=5752#p5752 http://ss64.org/viewtopic.php?pid=5752#p5752,其中我回应了另一个人试图做基本相同的事情。但是在读完你的问题后,我意识到我的旧“解决方案”也遇到了同样的问题 - 如果路径中的某处碰巧存在具有相同根名称的 exe,它会错误地将内部命令报告为外部命令。
我首先检查是否可以找到外部命令,如果没有,那么我假设 HELP 知道的任何命令都是内部命令。
我想我现在有一个工作版本。
我首先使用与您尝试过的类似技术检查该命令是否是内部命令。我添加了一些曲折。
- 我依赖于 %temp% 文件夹中的一个空文件夹(如果它不存在,我会创建它)
- 我担心命令注入,因此在进行内部测试之前我会验证命令是否只包含字母。
- The
(CALL )
命令是清除错误级别的快速方法
据我所知,如果给定参数,每个内部命令都会打印出帮助信息/?
。如果该命令不是内部命令,则它将无法执行并将 ERRORLEVEL 设置为 9009,因为 PATH 为空且当前目录为空。
::WHICH 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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)