How to make processes in Batch Script?

By lingy | Lingy | 16 Apr 2022


I've done some scripts in Batch in the past, so I decided to create this simple tutorial in an uncompromising way. Feel free to experiment, test and comment on the technology and its uses, and ask questions in the comments.

Why use old technology like this?

Batch scripts really are an old technology that was ignored for a long time by Microsoft, so much so that several features that exist today in BASH and PowerShell simply don't exist in Batch, such as loops and things like that (or even exist, but only in its most primitive form, with GOTO clusters). So maybe you're thinking, “Why should I study something as old as Batch scripts?

The answer is that these scripts are used to perform automated actions like cleaning folders, converting files, releasing or blocking network ports and the like, and all with minimal user interference, which also reduces human error. Also, regardless of the Windows system version, Batch is among the dependencies, so it's not that difficult to create a script compatible with older versions of Windows, or adapt it to more modern versions.

Batch scripts have other characteristics, allowing, for example, interaction with the user, which can be useful, for example, in scripts that allow executing more than one action, such as scripts to encrypt and decrypt data, or even simple games (yes, you can create rudimentary games in Batch, which will work like old terminal games in BASIC or such).

What is a Batch Script?

A Batch script is, in short, a stack of commands that are inside a file, and will be executed from start to finish, with their execution being occasionally altered by conditional or control loops. Its origins predate the MS-DOS operating system, and it has withstood the passage of time until Windows 10, despite constant threats to its role as Microsoft's primary automated scripting language with languages such as Powershell and VBScript. Its main advantage is its simplicity, and its main disadvantage is the fact that it is an archaic language.

DOS commands

Like other scripting languages, Batch Script is just a file with a bunch of terminal commands. Just that, there is no middle ground. Thus, we can understand that everything that is possible to do via CMD, such as creating folders and files, running programs and even crashing the operating system, is also possible with a Batch program, and everything that cannot be done via CMD is also not possible. do via Batch (with rare exceptions).

Batch files have other advantages, such as avoiding crashes due to user errors, speeding up everyday and repetitive tasks, and making it possible to view complex sequences of environment configuration commands for beginners.

Let's start by recapping some of the most basic commands in the system:

Command Result ECHO Write on screen ECHO OFF Hide code executed by the system ECHO ON Redisplays the code executed by the system ECHO. Skip a line CLS Clean console START Run another program PAUSE Pauses program execution until a key is typed REM Indicates the beginning of a comment that will extend to the end of the line

Hello World!

And now let's go to the classic “Hello World” that the vast majority of programming languages implement. Your program will look like this:

echo Hello World
pause

Save this in a file called Hello.bat and double click on it.

Let's start by noticing some aspects of language. First of all, for messages, you don't need to put single or double quotes, just throw what you want there. Second, Batch Script commands are not case sensitive (only in more specific messages or the like). And third, your CMD probably looked like this:

C:> echo Hello World
Hello World

It wasn't our goal, after all, the program should act by hiding these commands, and not just keep listing them on the screen. Edit your file and leave it like this:

@echo off
echo Hello World
pause

Now the commands executed by the batch will be hidden in the system. Simple, agreed?

Running notepad.exe program

Let's say you want to open Notepad on Windows. Since the notepad.exe program is already inside the system PATH, you don't need to do anything other than run it:

@echo off
rem initializes Windows notepad
start notepad.exe

And there? Did you like the class? Tell me in the comments 😊😄

How do you rate this article?


7

0


Lingy
Lingy

My personal blog about technology

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.