1.

Solve : Batch File With NO LABLES?

Answer»

@ECHO OFF
SET ANS=%1
IF "%ANS%"=="" GOTO NOTHING
IF /I "%ANS%"=="Y" GOTO YES
IF /I "%ANS%"=="N" GOTO NO
ECHO.
ECHO INVALID ENTRY, PLEASE REENTER
GOTO QUIT
:NOTHING
ECHO.
Echo You Entered Nothing
Echo Please Reenter
GOTO QUIT
:YES
ECHO.
Echo You Entered Yes
Echo Whooppee - isn't this exciting!
GOTO QUIT
:NO
ECHO.
Echo You Entered NO
Echo How Negative!
:QUITUnfortunately there's no function support for batch files. I don't know why you wouldn't want labels. I guess you could write conditional statements that call other batch files. This way you would avoid using labels to BRANCH out but it also means creating multiple batch files to call.Quote from: PaulJones on April 05, 2011, 06:58:18 PM

May we have an example?

Well, I'm sure someone else could cook up a better example but here's one:

Batch 1:
Code: [Select]@echo off
SET /p ANS=Choice?
IF "%ANS%"=="Y" ( CALL bat2.cmd )
IF "%ANS%"=="N" ( CALL bat3.cmd )
Batch 2 (bat2.cmd)
Code: [Select]echo You called Yes!
pause >nul


Batch 3 (bat3.cmd)
Code: [Select]echo You called No!
pause >nul

By answering yes or no, you branch out by calling other batch files instead of jumping to labels. Again, I don't know why you would want to take this route as it is tedious and unreasonable.You can eliminate the calls with something LIKE this:

Code: [Select]@echo off
setlocal

set /p ans=choice?
if /i "%ans%"=="y" echo You Called Yes
if /i "%ans%"=="n" echo You Called No
pause > nul

Agree with previous POSTER, batch code is already obtuse and obscure so why make things more difficult for yourself?

Quote
The above code does nothing.

Of course not, it was in response to the previous poster as a alternative to using calls.

The OP apparently wants to eliminate :labels. The following snippet accomplishes that, but READABILITY plunges near zero.

Code: [Select]@echo off
setlocal

set /p ans=choice?
if /i "%ans%"=="y" (echo You Entered Yes && echo Whooppee - isn't this exciting!
) else if /i "%ans%"=="n" (echo You Called No && echo How Negative!
) else (echo Invalid Entry, Try Again && %0)

Just curious why OP doesn't want to use all the features available.







Discussion

No Comment Found