The Bat Script Journey — Knowing The Essentials Command of Batch

Handhika Yanuar Pratama
5 min readFeb 25, 2024

--

Photo by NEOM on Unsplash

Welcome to the bat script journey, we will explore what inside batch script. As Windows user, we know about command line, but not with this programming language, how can it be?

Batch scripts rely on a variety of commands to perform tasks. Common commands include ECHO, SET, IF, and FOR, each with its unique role in shaping script behavior. Let’s explore that within this article.

Echo: Communicating with the Console

The ECHO command is fundamental for displaying messages on the console. Whether it's a simple notification or debugging information, ECHO is your go-to command.

@echo off
echo Hello, Handhika, Remember your appointment!

In this example, the @echo off statement is used to suppress the display of subsequent commands, and echo Hello, Batch Scripting! outputs the message to the console.

Set: Managing Variables

The SET command is essential for working with variables in batch scripts. It allows you to assign values to variables, creating dynamic and adaptable scripts.

@echo off
set MY_VARIABLE=42
echo The answer is %MY_VARIABLE%.

Here, the script sets the value of MY_VARIABLE to 42 and then echoes the message "The answer is 42." to the console, incorporating the variable into the output.

If: Conditional Execution

The IF statement enables conditional execution of commands based on the evaluation of a condition. It adds decision-making capabilities to your scripts.

@echo off
set NUM=10
if %NUM% gtr 5 (
echo The number is greater than 5.
) else (
echo The number is not greater than 5.
)

In this example, the script checks if the value of NUM is greater than 5 and displays the corresponding message.

For: Looping Through Commands

The FOR command facilitates looping through a series of commands, allowing you to perform repetitive tasks with ease.

@echo off
FOR /L %%G IN (1,1,5) DO (
echo This is iteration %%G.
)

Here, the script uses a FOR loop to echo a message five times, iterating through the specified range.

Note that some command probably new like /L and %%G

  • This is the beginning of a FOR /L loop. It's a loop specifically designed for iterating through a range of numbers.
  • %%G is a loop variable that takes on each value in the specified range during each iteration.
  • (1,1,5) denotes the range. It starts from 1, increments by 1, and goes up to 5. So, the loop will iterate through the values 1, 2, 3, 4, and 5.
  • DO ( signifies the beginning of the block of commands that will be executed during each iteration of the loop.

Rem: Comment

In batch scripting, the rem command is short for "remark" or "comment." It is used to insert comments or remarks within the script that are not executed as commands

@echo off

rem This is a comment explaining the purpose of the script
echo Hello, Batch Scripting!

The output above is just like this, since comment will not be displayed.

Call: Subroutines and Modularization

The CALL command is used to call other batch files or labels within the same script, promoting modularization and code reusability.

@echo off

rem Main script starts here
echo Main script is starting...

rem Call the subroutine with parameters
call :mySubroutine Parameter1 Parameter2

rem Continue with the main script
echo Back in the main script.
pause
goto :eof

rem Subroutine definition starts here
:mySubroutine
echo Inside the subroutine.
echo Parameter 1: %1
echo Parameter 2: %2
goto :eof@echo off
call :mySubroutine
echo Back in the main script.
goto :eof

:mySubroutine
echo Inside the subroutine.
goto :eof

In this example, the script calls the :mySubroutine label, executes the corresponding commands, and then returns to the main script.

Goto: Controlling Script Flow

The GOTO command allows you to jump to a specified label within the script, providing control over the script's flow.

@echo off
echo This is the start.
goto :myLabel
echo This is not executed.

:myLabel
echo This is after the GOTO command.

Here, the script skips the echo This is not executed. line and jumps directly to the :myLabel label.

How to Display it?

If you are follow along this tutorial, you will notice that our command line is gone after we run the batch script. So to make it shown in your command line, add pause in every last lines of your program. Did you see the differences?

Conclusion

In conclusion, these essential commands serve as the building blocks for crafting robust and efficient batch scripts. Stay tuned for more advanced techniques in our next installment as we continue to unravel the world of batch scripting. Happy scripting!

--

--

Handhika Yanuar Pratama
Handhika Yanuar Pratama

Written by Handhika Yanuar Pratama

Live the Way the Life ask for || A Stoic Engineer || Technical Writer || Runner x Dreamer