Group, Label and Functions in Windows Batch


A group of commands in windows batch can be enclosed by brackets. The commands are treated as a single one and this is often used together with if, for where a group of commands need to be executed in blocks. For example, the following shows a basic condition branch and two groups of commands are placed in different branches.

@echo off

if defined var1 (
  echo "group1"
  echo "group1"
) else (
  echo "group2"
  echo "group2"
)

Similar to Linux-BASH shell, the group statements support the boolean-logic-like styles, where you can specify the success or failure commands by simply using && or ||.

@echo off
:: Command && SuccessCommand 
:: Command || FailCommand 
:: Command && SuccessCommand || FailCommand 
:: The && operator can be used to execute a command only when the previews command succeeded. 
:: The || operator can be used to execute a command only when the previews command failed. 
:: Be careful when combining the operators, i.e.: 
:: Command1 && (CommandBlock2) || (CommandBlock3)
:: If Command1 fails then CommandBlock2 will be skipped and CommandBlock3 will be executed.
:: If Command1 succeeds then CommandBlock2 will be executed.
:: If Command2 fails then CommandBlock3 will also be executed.
:: To correct this, i.e. using a simple REM as last block command: 
:: Command1 && (CommandBlock2 & REM) || (CommandBlock3) 
:: or: 
:: Command1 && (
::    CommandBlock2
::    REM force success
::) || (
::    CommandBlock3
::)

:: example
set /p p="input" && (
  echo The user entered a value, the variable v changed.
) || (
  echo The user just hit Enter, the variable v remains unchanged.
)

The labels are commonly-used in the Windows batch. They can be used via goto or call depending on purposes. The eof label is pre-defined and in fact does not actually act as a label. For example, if you have statements after label eof and use goto :eof to jump to this label, the script will actually interpret this as& exit /b which is ending the script.

The statements after label eof will be just ignored. Please note that there is a difference between goto eof and goto :eof (with colon). The previous one will look for the label eof and if not found, will print out a message saying the label eofis not found. The second one is just like exit /b but automatically sets the errorlevelvalue. exit /b can take a second parameter that is the errorlevel value.

testeof Group, Label and Functions in Windows Batch batch script beginner DOS implementation string tools / utilities tricks windows windows command shell

Windows Batch programming does not support definition of functions, but this can be done via the usage of labels. Instead of goto use call which will continue to the next statement after the branch of the label is finished. For example, the basic function (procedure that takes no parameters) in batch is used like this.

@echo off
:: invoke the procedure
call :hello
goto :eof

:: procedure hello()
:hello
echo Hello, World!

:eof

And, if you need to pass the parameters, you just simply reference the parameters like parsing the command-line parameters from %1 to %9, use shift if needed.

@echo off

call :hello justyy
goto :eof

:: procedure hello()
:hello
echo Hello, %1

:eof

The parameters passing in the function can be separated by spaces, or comma. Use double quote to enclose the parameters that contain spaces.

How does the function return values? Well, this is not supported, the functions defined using labels do not support a return-value. However, this can be solved by using out-like parameters (in Pascal, the var parameters, in C#, the out parameters and in C++, the reference/pointer variables). For example, the following defines a function that returns the value in its first parameter (out).

@echo off
set name=unknown
:: before=unknown
echo before=%name%
call :hello name
:: after=justyy
echo after=%name%
goto :eof

:: procedure hello()
:hello
set %1=justyy
:eof

Based on this, the main processing script can be separated and made clean from a couple of sub-tasks, that are defined as functions.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
760 words
Last Post: An Idea: Collection of Linux-Utils using Windows Batch
Next Post: Fibonacci Numbers, Windows Batch Programming Revisited

The Permanent URL is: Group, Label and Functions in Windows Batch

Leave a Reply