Check if cmd label exists

I wonder if there is a way to check if a shortcut exists in the batch file?

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

How can i do this? Any help would be appreciated.

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

Find the label in the current batch file, and if there is no error level, there is a label

EDITED - I realized that there was a mistake in the way I handled the end of the shortcut and was going to edit the answer (it was edited anyway), and I see dbenham aquariums. He saw a mistake and corrected it. Thank. A good answer, as always, BUT this is worse than you showed.

At this point, I only have XP to test, but this is what works for me. If someone can test later versions of Windows, please.

: . , dbenham , [;=,<space><tab>0xFF] , , . , , , ( ). , .

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

X=;=:test
    echo Hello

, , , " ", - .

: . dbenham, , /. , . , (, , ) . ,

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

,

, , ""

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

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

POST EDIT 1 - , dostips.com. , exaustive, . .

POST EDIT 2 - findstr, . , . , 0xff .

dbenham.

, STILL INCOMPLETE, , dbenham

@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%

- , .

+9

, MC ND answer. ( , ).

, .

, . . : :label documentation - .

findstr /ri /c:"^ *:%input% " /c:"^ *:%input%$" "%~f0" >nul 2>nul && goto %input%

, , .

  • : , ; = <space> <tab> <0x255>. , . <space>. [class], <tab> <0x255> .

  • , <space> ( ).

  • .

  • FINDSTR $ <CR><LF> , , script Unix style <LF>.

. . , , FINDSTR. FINDSTR, /G:file - yuck.

+7

:

echo off
set "label=sub"
REM next line to reset errorlevel to zero:
(call )
call :%label% 2>nul || (echo %label% not found & exit /b 1) 
echo back from %label%
Exit /b 0

:sab
echo here we are
+2

, , " " ...

for %%l in ( :label1 :label2 :label3 :label4 :EOF ) do set x_labelexistconst_%%l=.

: label1: label2: label3 - , CMD , , , , findstr, . ,

for /F "tokens=1" %%l in ( 'findstr /i /r /c:"^[ ]*:[a-z0-9]*" "%~f0"' ) do set x_labelexistconst_%%l=.

if defined x_labelexistconst_%input% goto %input%
if defined x_labelexistconst_%input% call %input%

… , CALL, findstr "EACH AND EVERYTIME", " ", "ONCE" , // .

, , && || goto, ,

if defined x_labelexistconst_%input% (call %input% && echo call ok || echo call bad)

Ultimately, finding a file with findstr and CALL and GOTO (which re-read the original bat file or something like that) are very slow commands, so using them or limiting their use once or never is the best option for bat files - especially with slow network connections. where the bat file may be large. And that makes the whole script smaller and easier to understand.

I have not debugged it and tested it a lot, but it just seems like a better idea.

0
source

Source: https://habr.com/ru/post/1538301/


All Articles