Batch file and some Fun commands


 

What is batch scripts?

A batch script is a text file that contains certain commands that are executed in sequence. It is used to simplify certain repetitive tasks or routines in the Windows, DOS and OS/2 operating systems, and is also used in complex network and system administration.


Batch executions


There are 2 ways to execute a batch script.

  • Type the batch script in the command prompt.
  • Write the code of script in a file and execute it through the command prompt.

Typing commands again and again on the terminal can be a very tedious task to do if we have a very lengthy code. So option 2 is generally preferred to create batch files.


How to create batch file?


  1. Create a new text file with a ‘.txt‘ extension.
  2. Now rename this file with extension as ‘.bat‘ this creates a Batch file.
  3. Now open this .bat file in any text editor and start scripting.

To begin scripting we must be aware of the commands of the batch interface. The commands of Batch are sometimes similar to Linux Scripting commands.


Basic Batch commands 


Basic batch commands are all case insensitive and can be used to perform a specific set of instructions:-

  • DIR – The ‘dir’ command is used to get all the directories, sub-directories, and files present in the current working directory.
  • CD – The ‘cd’ command is used to change the current working directory.
  • VER – The ‘ver’ command tells the version of the user’s Windows.
  • CLS – The ‘cls’ command is used to clear the screen of the command prompt.
  • ECHO – The ‘echo’ command is by default ‘on’ but if we turn it off by ‘echo off’ it turns off prompt till the time ‘echo on’ is passed.
  • @ – The ‘@’ if used before any command hides which command is running.
  • @ECHO OFF – This commands serves as the start point to any basic batch script as it hides the prompt with ‘echo off’ and hides ‘echo off’ command with ‘@’.
  • HELP – This command tells us all about the commands available in the cmd. It runs only if the cmd is run as an administrator.


Data Types in Batch

  • Integers – Batch supports the whole set of positive and negative integers
  • Strings – Unlike most programming languages we rarely use (“”) double-quotes here but we use ‘echo‘ command to print strings

Note: Batch doesn’t support floating-point values i.e. values with precision.


Variables in batch scripts 


A variable is an entity that stores a specific value and allows the user to perform any set of instructions on it. To create variables we use the command “SET” command. A variable, unlike many programming languages, can be assigned simply without specifying any data type to it.

SET my_variable=Hello World

To print this variable we need to use the command ECHO but with a slight variation. Since echo  prints both strings and variables to print string we simply write the string after ECHO as

ECHO Hello World

But to print a variable we use ECHO in a different way bypassing the variable names inside two percent signs (%)  so that variable name doesn’t become a string-

ECHO %my_variable%

Working with Batch Scripts

Creating our own Batch Scripts

Example 1: To print “GeekForGeeks” on the command prompt with and without using a variable.

Without using a variable 

ECHO ssecgroup

With a variable

SET my_var=ssecgroup
ECHO %my_var%


Arithmetic Operators in a Batch Script

List of operators :

SET /A sum=1+1     ::addition operator
ECHO %sum%     
SET /A mul=7*9     ::multiplication operator
ECHO %mul%
SET /A div=9/3     ::Division operator
ECHO %div%
SET /A assign=10   ::Assignment operator
ECHO %assign%
SET /A assign+=15  ::Increment then assignment operator
ECHO %assign%
SET /A mod= 10%3   ::Modulus/Remainder operator
ECHO %mod%



Source: Wikipedia and GeekForGeek 🙏

My YouTube CHANNEL: https://youtube.com/channel/UCkD3Np_PL-sdBiA7lpqJSyg

Comments

Popular posts from this blog

7-Eleven breached customer privacy by collecting facial imagery without consent

Json webtoken or jwt Format and encode , decode

How to Generate Strong Passwords or Generate Random Password