检查是否存在标签CMD [英] Check if label exists cmd

查看:190
本文介绍了检查是否存在标签CMD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道有没有什么办法来检查,如果一个标签在批处理文件中存在吗?

 如果输入%(%)= ABC(
  如果标签存在ABC(
    转到ABC
  )

我怎样才能做到这一点?
任何帮助将AP preciated。


解决方案

  FINDSTR /我/ R / C:^ [] *:%输入%\\>中%〜F0> NUL 2  - ; NUL&放大器;&安培;转到%输入%

搜索标签在当前的批处理文件,如果没有错误级别,存在标签

编辑 - 我意识到有是在我正在处理标签的结束方式的错误,并要编辑答案(已反正编辑),我看到了dbenham aclarations。他看到了错误并纠正它。谢谢。尼斯答案一如既往,但是这是不是你已经暴露了什么更糟糕。

在这一刻我只有XP进行测试,但是这是我的作品。如果任何人都可以测试更高版本的Windows版本,请。

第一个问题:在标签的开始。像往常一样dbenham是正确的,并在集合中的任何字符 [=,<空><标签>为0xFF] 可以precede,单次或重复时,标签的结肠。但是,就因为它是上线的第一个字符,它不重复,几乎所有的角色可以precede标签的冒号(唯一的例外是其他冒号)。那么,下面就没有任何问题的工作。

 呼叫:测试
    转到:测试
    回声这将不会呼应X = =:测试
    您好回声

没有,这难道不是一个有效的线,如果解析器尝试执行标签行,未识别的命令会发生错误,但是一个有效的标签以打电话或转到

问题二:标签结束。作为dbenham确定,我们大多数的放置空间和参数列表,当标签被用来定义一个函数/过程。这是我意识到错误,什么已经我原来的答复得到纠正。但,一个空格(显然行的末尾)不是标签名称后的唯一允许的字符。因此,在previous样品,以下任何标签将工作

 :测试参数
:测试:参数
:试验>参数
:测试<参数
:测试&放大器;参数

是的,在这种情况下,他们解析器有效的命令和有效的标签

,当然,这两个问题可以在同一时间发生

 呼叫:测试
    转到:测试
    回声这将不会呼应< ;;:测试:;; >这个工程
    您好回声

POST编辑1 - 似乎这一切的工作是在dostips.com年前完成。感谢所有谁编译评论中引用的exaustive列表。下一次,我会首先搜索。

POST编辑2 - 我一直在试图处理FINDSTR的限制包括所有案件。那么,有没有办法。有太多的局限性,首先包括在常规的前pression的0xFF的性格是不可能的。

有关一个强大的和简单的解决方案,从dbenham答案是最好的选择。

有关更强劲,但仍不完整,没有防弹版本,比dbenham的回答更复杂的

 关闭@echo    对/ L %% i的(1110)做(
        拨打:testLabelExist测试%%我&放大器;&安培;回声标签[测试%% I] ||存在回声标签[测试%%我]不存在
    )
    退出/ B:TEST1
 :TEST2
    :TEST3
X:TEST4
:: TEST5
:TEST6:
:#TEST7
 :test8参数
    :test9参数
:test10:myData的
:testLabelExist
    FOR / Fdelims =%%的T(
        FORFILES / P%〜DP0。 / M%〜NX0/ CCMD / D / C @echo×09'
    )做(
        FINDSTR / I / M / R / C:^ [^:] * [%% T] *:%〜1 [%% T:; = +]/ C:^ [^:] * [% %T] *:%〜1 $%〜F0> NUL 2 - ; NUL
    )
    退出/ B%ERRORLEVEL%

和它仍然遗漏了引号标签名称,命名一次失败点。

I wonder is there any way to check that if a label exist in a batch file?

If %input%=ABC (  
  If Label ABC Exists (
    Goto ABC
  )
)

How can I do this? Any help will be appreciated.

解决方案

findstr /i /r /c:"^[ ]*:%input%\>" "%~f0" >nul 2>nul && goto %input%

Search the label in the current batch file and if no errorlevel, label exists

EDITED - I realized there was an error on the way i was handling the end of the label and was going to edit the answer (it has been edited anyway) and i see the dbenham aclarations. He saw the error and corrected it. Thank you. Nice answer as always, BUT this is worse than what you have exposed.

In this moment i have only a XP to test, but this is what works for me. If anyone can test on later windows versions, please.

First problem: the start of the label. As usual dbenham is correct, and any character in the set [;=,<space><tab>0xFF] can precede, single or repeated, the colon of the label. But, as far as it is the first character on the line, and it does not repeat, almost any character can precede the colon of the label (one exception is other colon). So, the following will work without problems

    call :test
    goto :test
    echo this will not be echoed

X=;=:test
    echo Hello

NO, this it not a valid line, if the parser try to execute the label line, a "command not recognized" error will happen, BUT is a valid label to call or goto.

Second problem: end of the label. As dbenham identified, most of us place a space and the list of arguments when the label is used to define a function/procedure. This was the error i realized and what has been corrected in my original answer. BUT, a space (and obviously the end of line) is not the only allowed characters after the label name. So, In the previous sample, any of the following labels will work

:test arguments
:test:arguments
:test>arguments
:test<arguments
:test&arguments

And yes,in this case they are valid commands to the parser and are valid labels

AND, of course, the two "problems" can happen at the same time

    call :test
    goto :test
    echo this will not be echoed

< ;;:test:;; > This WORKS 
    echo Hello

POST EDIT 1 - It seems all this work was done years ago at dostips.com. Thanks to all who compiled the exaustive list referenced in comments. Next time, i'll search first.

POST EDIT 2 - I've been trying to deal with the limitations of findstr to include all cases. Well, there is no way. There are too many limitations, starting with the impossibility of include the 0xff character in a regular expression.

For a robust and simple solution, the answer from dbenham is the best option.

For a more robust, but STILL INCOMPLETE, no bulletproof version, and more complex than dbenham's answer

@echo off

    for /l %%i in (1 1 10) do (
        call :testLabelExist "test%%i" && echo Label [test%%i] exist || echo Label [test%%i] does not exist
    )
    exit /b

:test1
 :test2
    :test3
x:test4
::test5
:test6:
:test7#
 :test8 parameters
    :test9 parameters
:test10:myData


:testLabelExist
    for /f "delims=" %%t in (
        'forfiles /p "%~dp0." /m "%~nx0" /c "cmd /d /c @echo 0x09"'
    ) do (
        findstr /i /m /r /c:"^[^:]*[ %%t]*:%~1[ %%t:;,=+]" /c:"^[^:]*[ %%t]*:%~1$" "%~f0" >nul 2>nul 
    )
    exit /b %errorlevel%

And it still leaves out quoted label names, just to name one failure point.

这篇关于检查是否存在标签CMD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆