Sunday, 23 June 2019

Batch: Working with labels


Labels are used to organize the code.

How to define a label?
Using :, you can define a label.

Example
:label1

Hello.bat
@ECHO OFF

ECHO Hello, Good Morning

:START
ECHO Starting execution

:IN_PROCESS
ECHO Program Execution is in process

:FINISHED
ECHO Program finished Execution

C:\Users\Public>Hello
Hello, Good Morning
Starting execution
Program Execution is in process
Program finished Execution

GOTO statement
GOTO is used to execute the statements from given label (I mean you can jump to any other label).

Syntax
GOTO label


Hello.bat
@ECHO OFF

ECHO Hello, Good Morning

:START
ECHO Starting execution
GOTO FINISHED

:IN_PROCESS
ECHO Program Execution is in process

:FINISHED
ECHO Program finished Execution

C:\Users\Public>Hello
Hello, Good Morning
Starting execution
Program finished Execution


As you see the output of Hello script, it skipped IN_PROCESS label.

Previous                                                 Next                                                 Home

No comments:

Post a Comment