Prerequisites
- Computer programming knowledge.
- Windows command line commands basics.
What is Windows Command Prompt | Command Line | Command Shell | cmd |
cmd.exe | Command Line Interpreter?
Command prompt is an executable windows application which is available on most
of the operating systems. It supports a list of windows commands. Command line
interface helps in performing windows operations like
creating/deleting/renaming/copying/moving folders or files, open files or
applications etc.
What is a batch file?
A
batch file or
a batch script or
batch job is a file
which contains the list
or collection of commands which will execute sequentially from top to bottom in command window. In windows operating system a
batch file stored with an extension *.bat.
Batch scripting advantages
- Run multiple processes at a time
- Perform repetitive tasks
- Back up files
- Easy to manage large processes
- Saves time by running collection of commands
- Can call one batch from another batch
- Supports the user inputs as an arguments
- Publishing applications
- Supports all the windows command line commands
- Can work with git commands (git installation required)
In this tutorial you will be introduced with the basic batch scripting
commands.
Batch file commands
ECHO
@ECHO [ON/OFF] - sets the command-line settings. ON - Displays the executing commands on command window, OFF - won't displays the commands on window.
ECHO [Text] - Print text to the screen)
ECHO. - Print an empty line to the screen
ECHO - Displays the current setting of the command-line
PAUSE
Stops the commands execution and wait for any key press.
REM
Adds comment in script file.
Example:
REM comment details will goes here
EXIT
Terminates execution and closes the command window.
Variables
Variable can be initialized with SET command.
Syntax: SET [/A] [variable-name]=value
/A - If the value needs to be numeric
String variable:
SET employeeName="Srinubabu Ravilla"
ECHO %employeeName%
The above script will print the "Srinubabu Ravilla" on screen. Variable
can be accessed by surrounding with the %% as shown above.
String concatination:
SET employeeFirstName=Srinubabu
SET employeeLastName=Ravilla
ECHO
%employeeFirstName%+%employeeLastName%
The above script will print the "Srinubabu Ravilla" on screen by
combining both first name and last name variables.
Numeric variables:
SET /A employeeFixedPay=500000
SET /A employeeVariablePay=100000
ECHO Employee total salary:
%employeeFixedPay% + %employeeVariablePay%
The above script prints the "Employee total salary:600000 " on screen.
Prompt user for input:
SET /p userInput=Enter employee name:
ECHO User input: "%userInput%"
The above script prints the user entered input on screen.
Escape sequence
Most special characters can be escaped using the caret (^) below are the
list of characters that can be escaped.
Example: ECHO Mobile number format
^<Country-Code^>^<10-digit-numeric-value^>
The above script will print "Mobile number format
<Country-Code><10-digit-numeric-value>"
Directories
Get the current batch script execution directory:
An environment variable CD stores the current command directory
ECHO
Current directory: %CD%
Windows commands can be utilize in the batch scripting. Below are the few
examples.
Change from one directory to another:
cd /d c:
Navigate to another directory by directory path:
SET folderPath=C:\Users - Manual path
CD %folderPath% - With path from variable
Create a new folder:
md "NewFolder1234" - Create folder in current directory
md "c:\Users\sravilla\test123" - Create new folder in by path
md %directoryVariable%
- Create directory from variable/user input
Open Folder:
%SystemRoot%\explorer.exe %FolderPath%
%SystemRoot%\explorer.exe "C:\Users"
Start
Start command can be used to start the program
START C:\Users - Opens the directory
start notepad.exe - Opens notepad
START https://dotnetcookie.blogspot.com/
- Opens the URL in default browser
START mailto:srinubabu.ravilla@gmail.com
- Creates a new email in mail application
Loops
Unlike other programming languages batch scripting supports only for loop.
The definition of a loop is, statements execution repeats until
specified condition is satisfied.
Basic Syntax:
FOR %%variable_name IN list DO commands
list
- List of any elements, separated by either spaces, comma's or
semicolons.
command
- Can be any internal or external command, batch file, list of
commands.
%%variable_name
- Variable declaration in loops is specified with %%variable_name
instead of %variable_name%.
Looping through numbers
FOR %%x IN (1 2 3 4 5 6 3 2 1) DO Echo %%x
The above script prints all the numbers from starting to ending.
Looping through range of values
Batch scripting supports numeric range looping.
Syntax: FOR /L %%variable_name IN (LowerLimit, Increment, UpperLimit)
Do some_code
/L
- Specifies the loop suppose to iterate through the range of
values.
LowerLimit - The minimum numeric start value
Increment
- Numeric increment which adds to LowerLimit in each iteration
UpperLimit - The maximum value in a range to limit with
FOR /L %%Number_Variable IN (0, 2, 15) DO ECHO %%Number_Variable
The above script prints the range of values between 0 and 15 with the
increment of 2 in each iteration.
Output: 0,2,4,6,8,10,12,14
Loop through files
Batch script supports looping through the files in current directory. File
lookup (*.*) specified in the below command can support the wild card
search like (*.jpg).
for /r %%file_path in (*.*) do echo %%file_path
Looping through directories/folders
Batch script supports looping through the directories (Includes sub
directories) in current directory. Directory lookup ("*") specified in
the below command can support the wild card search like (*.jpg).
for %f in (.\*) do @echo %f - Files in current directory
for /D %s in (.\*) do @echo %s - Subdirs in current directory
for /R %f in (.\*) do @echo %f
- Files in current and all subdirectories
for /R /D %s in (.\*) do @echo %s - subdirs in current and all subdirectories
So, this is all about the batch scripting. Please start with the basic commands and practice in you machine to get familiar understanding and effective learning.