DEV Community

Cover image for How to use conditional commands to write Batch File

Posted on


How to use conditional commands to write Batch File

One of the hidden and attractive features of Windows is the existence of a batch file (Batch File), by using which you can easily do many repetitive and boring tasks automatically. But the basis of this technology is a complete familiarity with the condition statement or If-else. In the following, we are going to teach you the difference between using this command in Batch File and other programming languages.

If you are familiar with programming, you must know that the If-else statement is used to control logical situations. More precisely, by using the If-else control statement, you can define different conditions according to the different situations that may occur in a problem. If the conditional statement is true, the corresponding code will be executed, otherwise the code corresponding to the else block will be executed. For example, the following code written in Java programming language means that if the number X is equal to 15, an expression will be printed that shows the number 15, but otherwise, the expression of an unknown number will be printed.

if (x == 15) System.out.println ("The value of x is 15!"); else System.out.println("Unknown value.");

The concept of the if-else statement is the same in all programming languages, but the way it is written may have slight differences. For example, in a batch file, we should write the above command as follows:

if %x%==15 (echo "The value of x is 15") else (echo "Unknown value")

Now consider a different scenario. For example, suppose a code is to be written that if the number X is equal to 15, the expression of the number is completely correct, otherwise, if the number X is equal to 10, the expression of the number is acceptable, and otherwise the expression of the number is not correct, print to be The following commands in the Java programming language are as follows:

if (x == 15) System.out.println ("the number is corect!"); else-if (x == 10) System.out.println ("The number is acceptable"); else System.out.println("the number is wrong!");

But the point is that the if-else command in Batch File is a little different. There is no else-if statement in this language, instead you can use consecutive if statements to write consecutive conditions. For example, the scenario described above is written as:

if %x%==15 (echo "the number is corect!") if %x%==10 (echo "The number is acceptable") else (echo "the number is wrong!")

Although using scripting is a very useful method, it is still better to use the PowerShell tool and it is recommended to use this tool because this tool has all the necessary programming features using its own programming, which is of course very simple. The user puts and also the user can run his written batch files through PowerShell. In addition, PowerShell can be easily used on Linux operating system.

Have you used batch file writing to do repetitive tasks in Windows or do you prefer PowerShell? Share your experiences with us. Meanwhile, check programmed ppsspp games for your system and sports site we utilized this batch on.

Oldest comments (0)

An Animated Guide to Node.js Event Loop

Node.js doesn’t stop from running other operations because of Libuv, a C++ library responsible for the event loop and asynchronously handling tasks such as network requests, DNS resolution, file system operations, data encryption, etc.

What happens under the hood when Node.js works on tasks such as database queries? We will explore it by following this piece of code step by step.