- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试制作一个像 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 ,其中我回应了另一个人尝试做基本上相同的事情。但是在读完你的问题后,我意识到我的旧“解决方案”也遇到了同样的问题 - 如果路径中的某处碰巧存在具有相同根名称的 exe,它会错误地将内部命令报告为外部命令。
我首先检查是否可以找到外部命令,如果没有,那么我假设 HELP 知道的任何命令都是内部命令。
我想我现在有了一个工作版本。
我首先使用与您尝试过的类似技术检查该命令是否是内部命令。我添加了一些曲折。
(CALL )
命令是清除 ERRORLEVEL 的快速方法据我所知,如果给出 /?
参数,每个内部命令都会打印出帮助信息。如果该命令不是内部命令,则它将无法执行并将 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
关于windows - 在批处理脚本中执行 'which'命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21142715/
我正在尝试制作一个基于文本的批处理游戏。但是我刚开始写我以前从未遇到过的问题时遇到了一个问题。 :menu :: the game menu - opens when the game starts
我正在构建一个社交媒体应用程序,用户需要发布一些内容,然后将发布的内容传播给他/她的 4 个圈子内的所有成员。这意味着查询进入循环。它就像一个家谱。逻辑工作得很好。但现在,当每个圈子中的成员数量不断增
1. DECLARE TYPE ref_cursor_type IS ref CURSOR; v_mycursor ref_cursor_type;
我想在这里做的是循环直到按下“x”。我知道 CHOICE 带有 /T 选项。 但是 CHOICE 对我要播放的动画的超时时间太长。这是一个例子: @echo off cls set frame=2 :
我已经寻找解决方案,但我仍然遇到问题。我有两个文件: File1.txt 1111 2222 3333 File2.txt 1111 2222 3333 4444 我想要一个只有差异的输
我正在做一个批处理脚本,必须检查计算机上是否安装了一些程序。为此,我执行 programName --version我将输出存储在一个变量中。问题是当我尝试与正则表达式进行比较时(只知道该程序是否存在
我知道如何从同一个批处理文件中的函数返回值,但我发现从不同的批处理文件返回值时存在一些问题。下面是一个例子: 文件 1.cmd SETLOCAL ENABLEEXTENSIONS SETLOCAL E
我相信这个问题的答案应该很简单。我从一个地方获取目录列表并将它们存储到文本文档中。然后我读取文本文档名称并将它们存储到一个数组中。在此过程结束时,我希望删除数组中的所有条目。 我想这样做的原因是因为我
我家有两个摄像头,几乎每天都在创建图像。他们将它们保存到我的FTP服务器(Fritz.Box\Nas驱动器)。 文件夹结构如下: +-2016-08-24 +-+Subfolder +----+Ano
在Windows Batch中执行此操作。我有一个名称列表,并要求用户输入其名称。 我想检查该名称是否已经存在于列表中,如果存在,则直接进入goto,否则它将名称添加到列表中。 @echo off s
我正在编写一个批处理文件,我想运行一个 for 循环,将它的第一个值设置为一个变量。我只需要命令的第一个值,但我找不到另一种方法来做到这一点。我设置它的方式是使用一个 for 循环,然后是一个 do
我需要创建一个批处理文件,使用tracert命令跟踪一些IP,并将跟踪写入txt文件。我希望它很快,所以我想为每个跟踪启动一个新命令,以使所有跟踪请求立即启动。 这是我的 ping.bat: @ech
我想在批处理文件中使用PowerShell命令发送电子邮件。为此,我实现了一个名为 sendMail 的函数。我这样称呼它: setlocal enabledelayedexpansion call:
想要使用 java 执行 selenium 脚本/批处理脚本。根据输入参数调用脚本/批处理脚本。 了解如何使用 java 代码运行脚本/批处理。 请帮帮我。 最佳答案 要运行 java 项目中文件中包
我正在练习 JDBC 批处理并遇到错误: 错误1:不支持的功能错误2:执行不能为空或为null Property files include: itemsdao.updateBookName = Up
我从 json 文件中得到了以下字符串: 39468856, 现在,我想用这些数字进行计算..因此,我必须删除末尾的 , 。 此时我使用以下代码: for /f "skip=24 tokens=2"
我有一堆 SQL 查询作为文件存储在磁盘上。 它们都是纯 SELECT 查询,换句话说,它们只做读操作。 我正在连接到 Oracle 11g 数据库,我想测量所有这些查询的大致执行时间。有没有办法以编
我正在使用 java 来存储属性文件的目录路径。 然后在 bat 文件中我使用属性作为变量。 问题出在 Java 中,文件路径存储为 SOME_VAR=D\:\\Madhan\\Program Fil
我想用“%”替换字符串中的“mod”:set string=%string:mod=x%我应该输入什么作为“x”? 最佳答案 您可以通过启用延迟扩展来做到这一点,以便您可以使用 !作为分隔符。然后,将
在我目前正在处理的批处理文件中,我遇到了一个小问题。我有一个名为 Dimensions(number from 1-5, defined in a for /l loop).txt 的文件,其中包含
我是一名优秀的程序员,十分优秀!