Batch Scriptingf Tutoial
Batch Scriptingf Tutoial
Batch scripts are stored in simple text files containing lines with
commands that get executed in sequence, one after the other. These files
have the special extension BAT or CMD. Files of this type are recognized
and executed through an interface (sometimes called a shell) provided by
2
Creation:
Right click >go to new>text document>and give file name and save with .bat
extension.
Execution:
1. By double click.
2. By using CMD:
Some commands:
echo :
we can use echo for output the string.
Example: echo Hello world.
pause:
for holding the output.
exit /b:
when output has displayed, press any key
to exit from terminal.
Rem or (::) :
For comments.
dir:
4
@echo off:
By default, a batch file will display its
command as it runs. The purpose of this
first command is to turn off this display.
The
@echo off
echo. :
for line break or giving space between two
lines.
cls:
for clear screen.
%random% :
5
Variables:
a. Local variables:
Defined boundary in which they can
accessed.
@echo off
SETLOCAL
SET /A local_variable = 90
SET /A local_variable = %local_variable% + 10
echo LV = %local_variable%
ENDLOCAL
b. Global variables:
They do not have a defined boundary,
and they can be used anywhere in the
whole script.
@echo off
SET /A global_variable = 90
echo GV = %globle_variable%
Input:
A:\batch scripting
tutorial>variable.bat 10 20 30 40
Output:
10 20 30 40
output:
Name hariom singh rajput and total marks: 90
cd/
D:
del inverse_of_matrix.cpp
@echo off
:a
echo ________Calculator________
echo.
set /p expression= Enter expression to calculate:
set /a ans=%expression%
9
1 Create String
Example
@echo off
set message = Hello World
echo %message%
Output
The above command produces the following output.
Hello World
2 Empty String
An empty string can be created in DOS Scripting by assigning it no
value during it’s initialization as shown in the following example.
Set a=
To check for an existence of an empty string, you need to
encompass the variable name in square brackets and also
compare it against a value in square brackets as shown in the
following example.
[%a%]==[]
The following example shows how an empty string can be created
and how to check for the existence of an empty string.
Example
@echo off
SET a=
SET b=Hello
if [%a%]==[] echo "String A is empty"
if [%b%]==[] echo "String B is empty "
11
Example
@echo off
SET a = Hello
SET b = World
SET /A d = 50
SET c=%a% and %b% %d%
echo %c%
Output
The above command produces the following output.
Hello and World 50
4 String Concatenation
You can use the set operator to concatenate two strings or a string
and a character, or two characters. Following is a simple example
which shows how to use string concatenation.
Example
@echo off
SET a = Hello
SET b = World
SET c=%a% and %b%
echo %c%
12
5 String length
In DOS scripting, there is no length function defined for finding the
length of a string. There are custom-defined functions which can
be used for the same. Following is an example of a custom-defined
function for seeing the length of a string.
Example
@echo off
set str = Hello World
call :strLen str strlen
echo String is %strlen% characters long
exit /b
:strLen
setlocal enabledelayedexpansion
:strLen_Loop
if not "!%1:~%len%!"=="" set /A len+=1 & goto :strLen_Loop
(endlocal & set %2=%len%)
goto :eof
A few key things to keep in mind about the above program are −
The actual code which finds the length of string is defined in
the :strLen block.
The length of the string is maintained in the variable len.
Output
The above command produces the following output.
11
6 toInt
A variable which has been set as string using the set variable can
be converted to an integer using the /A switch which is using the
set variable. The following example shows how this can be
accomplished.
13
Output
The above command produces the following output.
13150
7 Align Right
This used to align text to the right, which is normally used to
improve readability of number columns.
Example
@echo off
set str=My name is Hariom Singh Rajput
echo %str%
set str = %str:~-19%
echo %str%
A few key things to note about the above program is −
Spaces are added to the variable of y, in this case we are
adding 9 spaces to the variable of y.
We are using the ~-4 option to say that we just want to show
the last 4 characters of the string y.
Output
My name is Hariom Singh Rajput
Hariom Singh Rajput
8 Left String
This is used to extract characters from the beginning of a string.
Example
set str=Helloworld
14
Output
The above command produces the following output.
Helloworld
Hello
9 Mid String
This is used to extract a substring via the position of the
characters in the string.
Example
@echo off
set str = Helloworld
echo %str%
Output
The above command produces the following output.
Helloworld
world
10 Remove
The string substitution feature can also be used to remove a
substring from another string.
Example
@echo off
set str = Batch scripts is easy. It is really easy.
15
Output
The above command produces the following output.
Batch scripts is easy. It is really easy.
Batch scripts easy. It really easy.
Example
@echo off
set str = Batch scripts is easy. It is really easy
echo %str%
Output
The above command produces the following output.
Batch scripts is easy. It is really easy
atch scripts is easy. It is really eas
Example
@echo off
set str = This string has a lot of spaces
16
set str=%str:=%
echo %str%
The key thing to note about the above program is, the : = operator
is used to remove all spaces from a string.
Output
The above command produces the following output.
This string has a lot of spaces
Thisstringhasalotofspaces
13 Replace a String
To replace a substring with another string use the string
substitution feature.
Example
@echo off
set str=This message needs changed.
echo %str%
set str=%str:needs=has%
echo %str%
The key thing to note about the above program is, the example
replaces the word ‘needs’ with the string ‘has’ via the statement
%str:needs = has%
Output
The above command produces the following output.
This message needs changed.
This message has changed.
14 Right String
This is used to extract characters from the end of a string.
Example
@echo off
set str = This message needs changed.
echo %str%
Output
The above command produces the following output.
This message needs changed.
changed.
Making Decisions:
In order to make decisions whether one or more
conditions must be executed or not we use these
statements:
a) If-statement
b) If-else statement
c) Nested-if statements
A) If statement:
The if the statement is one of the selection statements. It
uses to select statements depending on the value of a
controlling expression.
Example:
@echo off
set /A a = 50
set /A b = 100
19
set str1=hariom
set str2=rajput
B) If_else statements:
If condition returns true then the statements inside the
body of “if” are executed and the statements inside body
of “else” are skipped.
If condition returns false then the statements inside the
body of “if” are skipped and the statements in “else” are
executed.
Syntax: if(condition)
(do_something)
ELSE
(do_something_else)
Example:
@echo off
set str1=hariom
set str2=string2
if %str1%==hariom (
20
) else (
if %str2%==string3 (
) else (
C) Nested if statement:
Sometimes, there is a requirement to have multiple “if”
statement embedded inside each other. Following is the
general form of this statement:
Syntax: if(condition1) if(condition2) do_something
Thus, only if condition1 and condition2 are met, will the
code in the do_something block be executed
Example:
@echo off
set /A first = 50
Example:
@echo off
set /A a = 100
:labelone
exit /b 0
:labeltwo
Operators:
An operator is a symbol that tells the compiler to
perform specific mathematical or logical manipulations.
22
ARITHMETIC OPERATORS
Batch script language supports the normal Arithmetic
operators as any language. Following are the
Arithmetic operators available.
Example
Oper Description Example
ator
Example:
@echo off
set /A first = 50
OUTPUT:
Sum = 150
24
multiplication = 5000
division = 2
remainder = 3
RELATIONAL OPERATORS
Relational operators allow of the comparison of
objects. Below are the relational operators available.
Example
Oper Description Example
ator
@echo off
set /A first = 5
set /A second = 10
OUTPUT:
LOGICAL OPERATORS
Logical operators are used to evaluate Boolean
expressions. Following are the logical operators
available.
The batch language is equipped with a full set of
Boolean logic operators like AND, OR, XOR, but only for
binary numbers. Neither are there any values for TRUE
or FALSE. The only logical operator available for
conditions is the NOT operator.
26
@echo off
SET /A a = 5
SET /A b = 10
IF %a% GEQ 10 (
IF %b% LEQ 0 (
) ELSE (
) ELSE (
OUTPUT:
Example
Oper Description Exampl
ator e
Output
will be 2
Output
will be 15
Output
will be 2
Output
will be 2
@echo off
set /A number1 = 50
set /A number1+=50
echo %number1%
set /A number2-=50
echo %number2%
set /A number3*=50
echo %number3%
set /A number4/=50
echo %number4%
set /A number5%%=50
echo %number5%
29
100
50
10000
BITWISE OPERATORS
Bitwise operators are also possible in batch script.
Following are the operators available.
Example
Operator Description
@echo off
echo %Result%
echo %Result%
30
echo %Result%
OUTPUT:
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1
Return code:
Loops:
Since BASH is a command-line language, we get some pretty
feature-rich experience to leverage the programming skills to
perform tasks in the terminal. We can use loops and
conditional statements in BASH scripts to perform some
repetitive and tricky problems in a simple programmatic way.
Syntax
FOR /L %%variable IN (lowerlimit,Increment,Upperlimit) DO
do_something
Where
The /L switch is used to denote that the loop is used for
iterating through ranges.
Variable declaration – This step is executed only once for the
entire loop and used to declare any variables which will be
used within the loop. In Batch Script, the variable declaration is
done with the %% at the beginning of the variable name.
33
Example
@ECHO OFF
FOR /L %%X IN (0,1,5) DO ECHO %%X
Output
The above program produces the following output.
0
1
2
3
4
5
Example
@ECHO OFF
echo print reverse odd number (10-1):
for /l %%a in (10,-2,1) do ( echo %%a )
Output
34
ARRAYS:
Arrays are not specifically defined as a type in Batch Script but
can be implemented. The following things need to be noted
when arrays are implemented in Batch Script.
Creating an array:
An array is created by using the following set command.
set a[0]=1
Where 0 is the index of the array and 1 is the value assigned
to the first element of the array.
Another way to implement arrays is to define a list of values
and iterate through the list of values. The following example
show how this can be implemented.
EXAMPLE
@echo off
35
OUTPUT
The above command produces the following output.
1
2
3
4
Accessing Arrays
You can retrieve a value from the array by using subscript
syntax, passing the index of the value you want to retrieve
within square brackets immediately after the name of the
array.
@echo off
set a[0]=1
set a[1]=2
set a[2]=3
echo The first element of the array is %a[0]%
echo The second element of the array is %a[1]%
echo The third element of the array is %a[2]%
The above command produces the following output.
The first element of the array is 1
The second element of the array is 2
The third element of the array is 3
Modifying an Array
To add an element to the end of the array, you can use the set
element along with the last index of the array element.
EXAMPLE
@echo off
set a[0]=1
set a[1]=2
set a[2]=3
Rem Adding an element at the end of an array
Set a[3]=4
echo The last element of the array is %a[3]%
36
OUTPUT
The above command produces the following output.
Comments
37
Length of an Array
The length of an array is done by iterating over the list of
values in the array since there is no direct function to
determine the number of elements in an array.
@echo off
set Arr[0]=1
set Arr[1]=2
set Arr[2]=3
set Arr[3]=4
set "x = 0"
:SymLoop
if defined Arr[%x%] (
call echo %%Arr[%x%]%%
set /a "x+=1"
GOTO :SymLoop
)
echo "The length of the array is" %x%
OUTPUT
Output The above command produces the following output.
1
2
3
4
"The length of the array is" 4
EXAMPLE
@echo off
set obj[0].Name=Joe
set obj[0].ID=1
set obj[1].Name=Mark
38
OUTPUT
The above command produces the following output.
Name=Joe
Value=1
Name=Mark
Value=2
Name=Mohan
Value=3
Functions:
A function is a set of statements organized together to perform
a specific task. In batch scripts, a similar approach is adopted
to group logical statements together to form a function.
As like any other languages, functions in Batch Script follows
the same procedure −
Function Declaration − It tells the compiler about a function's
name, return type, and parameters.
Function Definition − It provides the actual body of the function.
Function Definition
39
EXAMPLE
:Display
SET /A index=2
echo The value of index is %index%
EXIT /B 0
Calling a Function
A function is called in Batch Script by using the call command.
Following is the syntax.
SYNTAX
call :function_name
Following example shows how a function can be called from the main
program.
EXAMPLE
@echo off
SETLOCAL
CALL :Display
EXIT /B %ERRORLEVEL%
:Display
40
OUTPUT
The above command produces the following output.
The value of index is 2
SYNTAX
Call :function_name parameter1, parameter2… parametern
The parameters can then be accessed from within the function
by using the tilde (~) character along with the positional
number of the parameter.
Following example shows how a function can be called with parameters.
EXAMPLE
@echo off
SETLOCAL
CALL :Display 5 , 10
EXIT /B %ERRORLEVEL%
:Display
echo The value of parameter 1 is %~1
echo The value of parameter 2 is %~2
EXIT /B 0
OUTPUT
The above command produces the following output.
The value of parameter 1 is 5
41
SYNTAX
Call :function_name value1, value2… valuen
The return values are set in the function using the set
command and the tilde(~) character along with the positional
number of the parameter.
Following example shows how a function can be called with return
values.
EXAMPLE
@echo off
SETLOCAL
CALL :SetValue value1,value2
echo %value1%
echo %value2%
EXIT /B %ERRORLEVEL%
:SetValue
set "%~1 = 5"
set "%~2 = 10"
EXIT /B 0
OUTPUT
The above command produces the following output.
5
10
EXAMPLE
@echo off
set str = Outer
echo %str%
CALL :SetValue str
echo %str%
EXIT /B %ERRORLEVEL%
:SetValue
SETLOCAL
set str = Inner
set "%~1 = %str%"
ENDLOCAL
EXIT /B 0
OUTPUT
In the above program, the variable ‘str’ is being localized in
the function SetValue. Thus even though the str value is being
returned back to the main function, the value of str in the main
function will not be replaced by the value being returned from
the function.
The above command produces the following output.
Outer
Outer
Recursive Functions
The ability to completely encapsulate the body of a function by
keeping variable changes local to the function and invisible to
the caller. We can now have the ability to call a function
recursively making sure each level of recursion works with its
own set of variables even though variable names are being
reused.
Following example shows how recursive functions can be used.
EXAMPLE
The example shows how to calculate a Fibonacci number recursively.
43
OUTPUT
The above command produces the following output.
The next Fibonacci number greater or equal 1000000000 is 1134903170.
A) Creating files
B) Reading files
C) Writing to files
D) Deleting files
E) Renaming files
Folder:
A folder is a directory where you keep your content.
44
Parameters
> Write to file.
2. Write a file:
@echo off
3. Delete a file:
45
4. Reading a file:
@echo off
for /f "tokens=*" %%x in (New_file.txt) do ( echo %%x)
5. Rename a file:
@echo off
rename New_file1.txt My_file1.txt
2. Rename a folder:
@echo off
ren Newfolder2 My_folder1
3. Move a folder:
@echo off
move Newfolder1 .\My_folder1
46
REM del delete folder but it will ask "are you sure to
delete folder" and if you press y than it delete folder and if you press n
than it will not delete your folder
rd Newfolder1
del My_folder1
del Newfolder2
rd Newfolder3
rd Newfolder4
Example:
@echo off
REM To get the list of all the running processes
TASKLIST
Process:
In Batch Script, the TASKLIST command can be used to get the
list of currently running processes within a system.
Syntax
TASKLIST [/S system [/U username [/P [password]]]] [/M [module] | /SVC | /V] [/FI
filter]
[/FO format] [/NH]
Example:
@echo off
REM To get the list of all the running processes
TASKLIST
Logging:
Logging in is possible in Batch Script by using the redirection
command.
Syntax
test.bat > testlog.txt 2> testerrors.txt
Example
@echo off
REM Logging in a file
TASKLIST > processes.txt
Output
Registry:
The Registry is one of the key elements on a windows system.
It contains a lot of information on various aspects of the
operating system. Almost all applications installed on a
windows system interact with the registry in some form or the
other.
The Registry contains two basic elements: keys and
values. Registry keys are container objects similar to
folders. Registry values are non-container objects similar to
files. Keys may contain values or further keys. Keys are
referenced with a syntax similar to Windows' path names,
using backslashes to indicate levels of hierarchy.
Reading from the registry is done via the REG QUERY command.
Example:
@echo off
windows\
50
Example:
@echo off
entry"
Deleting from the registry is done via the REG DEL command.
Example:
@echo off
Copying from the registry is done via the REG COPY command.
Example:
@echo off
Console\TestData
@echo off
Console\TestData
Debugging
Debugging a batch script becomes important when you are
working on a big complex batch script.
Following are the ways in which you can debug the batch file.
Using echo command
A very simple debug option is to make use of echo command
in your batch script wherever possible. It will display the
message in the command prompt and help you debug where
things have gone wrong.
Here is a simple example that displays even numbers based on
the input given. The echo command is used to display the
result and also if the input is not given. Similarly, the echo
command can be used in place when you think that the error
can happen. For example, if the input given is a negative
number, less than 2, etc.
Example
@echo off
if [%1] == [] (
echo input value not provided
goto stop
)
rem Display numbers
for /l %%n in (2,2,%1) do (
echo %%n
)
:stop
pause
52
Example
@echo off
if [%1] == [] (
echo input value not provided
goto stop
) else (
echo "Valid value"
)
:stop
pause
Output
C:\>test.bat
input value not provided
Press any key to continue..
1 VER
53
Output
Microsoft Windows [Version 6.3.9600]
2 ASSOC
Syntax
assoc – Displays all the file extensions
assoc | find “.ext” – Displays only those file extensions
which have the extension ext.
Example
@echo off
assoc > C:\lists.txt
assoc | find “.doc” > C:\listsdoc.txt
Output
.doc=Word.Document.8
.dochtml=wordhtmlfile
.docm=Word.DocumentMacroEnabled.12
.docmhtml=wordmhtmlfile
.docx=Word.Document.12
.docxml=wordxmlfile
3 CD
Syntax
cd
4 CLS
54
Example
@echo off
Cls
5 COPY
Example
The following example shows the different variants of
the copy command.
@echo off
cd
Rem Copies lists.txt to the present working directory.
If there is no destination identified , it defaults to the
present working directory.
copy c:\lists.txt
Rem The file lists.txt will be copied from C:\ to C:\tp
location
copy C:\lists.txt c:\tp
Rem Quotation marks are required if the file name
contains spaces
copy “C:\My File.txt”
Rem Copies all the files in F drive which have the txt file
extension to the
current working directory copy
F:\*.txt
Rem Copies all files from dirA to dirB. Note that
directories nested in dirA will not be copied
copy C:\dirA dirB
6 DEL
Example
The following example shows the different variants of
55
Example
@echo off
Rem All the directory listings from C:\ will be routed to
the file lists.txt
dir C:\>C:\lists.txt
Rem Lists all directories and subdirectories recursively
dir /s
Rem Lists the contents of the directory and all
subdirectories recursively, one
file per line, displaying complete path for each listed file
or directory.
dir /s /b
Rem Lists all files with .txt extension.
dir *.txt
Rem Includes hidden files and system files in the listing.
dir /a
Rem Lists hidden files only.
dir /ah
8 DATE
Example
@echo off
echo %DATE%
56
Example
Rem Turns the echo on so that each command will be
shown as executed
echo on
echo "Hello World"
Rem Turns the echo off so that each command will not
be shown when executed
@echo off
echo "Hello World"
Example
@echo off
echo "Hello World"
exit
11 MD
Syntax
md [new directory name]
12 MOVE
57
Syntax
move [source] [destination]
The files will be copied from source to destination
location.
Example
The following example shows the different variants of
the move command.
@echo off
Rem Moves the file list.txt to the directory c:\tp
move C:\lists.txt c:\tp
Rem Renames directory Dir1 to Dir2, assuming Dir1 is a
directory and Dir2 does not exist.
move Dir1 Dir2
Rem Moves the file lists.txt to the current directory.
move C:\lists.txt
13 PATH
Example
@echo off
Echo %PATH%
14 PAUSE
Syntax
Pause
15 PROMPT
Example
58
Example
The following example shows the different variants of
the rd command.
@echo off
Rem removes the directory called newdir
rd C:\newdir
Example
@echo off
ren C:\lists.txt C:\newlists.txt
18 REM
Output
None
19 START
Syntax
START “programname”
Example
@echo off
start notepad.exe
Output
When the batch file is executed, a new notepad
windows will start.
20 TIME
Example
@echo off
echo %TIME%
Output
The current system time will be displayed. For example,
22:06:52.87
21 TYPE
Example
@echo off
TYPE C:\tp\lists.txt
22 VOL
Output
The output will display the current volume label. For example,
Volume in drive C is Windows8_OS
Volume Serial Number is E41C-6F43
23 ATTRIB
Syntax
attrib
24 CHKDSK
Example
@echo off
chkdsk
25 CHOICE
Example
@echo off
61
Output
The above program produces the following output.
"What is the file size you what"
"A:10MB"
"B:20MB"
"C:30MB"
What is your option A , B or C [A,B,C]?
26 CMD
Example
@echo off
cmd
27 COMP
Example
@echo off
28 CONVERT
Syntax
CONVERT [drive]
Example
@echo off
CONVERT C:\
29 DRIVERQUERY
62
Example
@echo off
driverquery
Output
The above command will display the information of all the device
drivers installed on the current system. Following is an example
of a subset of the information displayed.
WacomPen Wacom Serial Pen HID D Kernel 8/22/2013 4:39:15
AM
Wanarp Remote Access IP ARP D Kernel 8/22/2013 4:35:45
AM
Wanarpv6 Remote Access IPv6 ARP Kernel 8/22/2013 4:35:45
AM
Wdf01000 Kernel Mode Driver Fra Kernel 8/22/2013 4:38:56
AM
WFPLWFS Microsoft Windows Filt Kernel 11/9/2014 6:57:28
PM
WIMMount File System 8/22/2013 4:39:34 AM
WinMad WinMad Service Kernel 5/9/2013 9:14:27 AM
WinNat Windows NAT Driver Kernel 1/22/2014 1:10:49 AM
WinUsb Driver Kernel 8/22/2013 4:37:55 AM
WinVerbs Service Kernel 5/9/2013 9:14:30 AM
WmiAcpi Microsoft Windows Mana Kernel 8/22/2013 4:40:04
AM
WpdUpFltr WPD Upper Class Filter Kernel 8/22/2013 4:38:45
AM
ws2ifsl Windows Socket 2.0 Non Kernel 8/22/2013 4:40:03
AM
wtlmdrv Microsoft iSCSI Target Kernel 8/22/2013 4:39:19 AM
WudfPf User Mode Driver Frame Kernel 8/22/2013 4:37:21
AM
WUDFWpdFs WUDFWpdFs Kernel 8/22/2013 4:36:50
AM
WUDFWpdMtp WUDFWpdMtp Kernel 8/22/2013 4:36:5
30 EXPAND
Example
63
31 FIND
Example
@echo off
FIND "Application" C:\tp\lists.txt
32 FORMAT
Syntax
format [drive]
Where drive is the drive which needs to be formatted.
Example
@echo off
format D:\
33 HELP
Example
@echo off
help
Output
The above command will display a list of all commands and their
description. Following is an example of a subset of the output.
SCHTASKS Schedules commands and programs to run on a
computer.
SHIFT Shifts the position of replaceable parameters in batch files.
SHUTDOWN Allows proper local or remote shutdown of machine.
SORT Sorts input.
START Starts a separate window to run a specified program or
command.
64
Example
@echo off
ipconfig
Output
The above command will display the Windows IP configuration on
the current machine. Following is an example of the output.
Windows IP Configuration
Example
@echo off
label
36 MORE
Example
@echo off
More C:\tp\lists.txt
Directory of C:\Program Files
Output
The above command will display the contents of the file lists.txt
one screen at a time. Following is an example of an output. Note
the -- More (12%) – at the end of the screen. In order to proceed
and display the remaining contents of the file, you need to enter
a key.
12/22/2015 02:31 AM <DIR> .
12/22/2015 02:31 AM <DIR> ..
12/15/2015 11:14 PM <DIR> Application Verifier
12/18/2015 05:06 PM <DIR> Bonjour
12/26/2015 08:01 PM <DIR> CCleaner
12/18/2015 05:05 PM <DIR> Common Files
12/17/2015 11:04 AM <DIR> Git
12/15/2015 11:09 PM <DIR> IIS
12/15/2015 11:10 PM <DIR> IIS Express
12/15/2015 10:16 PM <DIR> Intel
03/18/2014 02:24 AM <DIR> Internet Explorer
12/18/2015 05:06 PM <DIR> iPod
12/18/2015 05:06 PM <DIR> iTunes
12/15/2015 11:16 PM <DIR> Microsoft Identity Extensions
12/15/2015 11:46 PM <DIR> Microsoft Office
12/22/2015 02:31 AM <DIR> Microsoft Silverlight
12/15/2015 11:15 PM <DIR> Microsoft SQL Server
12/15/2015 11:15 PM <DIR> Microsoft SQL Server Compact
Edition
12/15/2015 10:59 PM <DIR> Microsoft Visual Studio 12.0
-- More (12%) --
37 NET
66
Syntax
NET [variant]
Where its variants can be one of the following −
net accounts
net computer
net config
net continue
net file
net group
net help
net helpmsg
net localgroup
net name
net pause
net print
net send
net session
net share
net start
net statistics
net stop
net time
net use
net user
net view
Example
@echo off
Net user
Output
The above command will display the current accounts defined on
the system. Following is an example of an output.
User accounts for \\WIN-50GP30FGO75
-------------------------------------------------------------------------------
Administrator atlbitbucket Guest
67
Syntax
PING [address]
Where address is the IP address or hostname of the destination
system.
Example
@echo off
Ping 127.0.0.1
Output
The above command will send ICMP/IP "echo" packets to the
destination address 192.168.0.1. Following is an example of the
output.
Pinging 127.0.0.1 with 32 bytes of data:
Reply from 127.0.0.1: bytes = 32 time<1ms TTL = 128
Reply from 127.0.0.1: bytes = 32 time<1ms TTL = 128
Reply from 127.0.0.1: bytes = 32 time<1ms TTL = 128
Reply from 127.0.0.1: bytes = 32 time<1ms TTL = 128
Example
@echo off
shutdown
Output
If the user executing the batch files has the relevant rights, the
computer will be shutdown.
40 SORT
68
Syntax
Sort [filename]
Where filename is the file whose contents need to be sorted.
Example
@echo off
Sort C:\tp\lists.txt
41 SUBST
Syntax
Subst [driveletter]
Example
@echo off
Subst p:
42 SYSTEMINFO
Example
@echo off
systeminfo
Output
The above command will show the system information on the
current system. Following is a subset of the output.
Host Name: WIN-50GP30FGO75
OS Name: Microsoft Windows Server 2012 R2 Standard
OS Version: 6.3.9600 N/A Build 9600
OS Manufacturer: Microsoft Corporation
OS Configuration: Standalone Server
OS Build Type: Multiprocessor Free
Registered Owner: Windows User
Registered Organization:
69
Syntax
Taskkill /im [taskname]
Example
@echo off
44 TASKLIST
Example
@echo off
Tasklist
45 XCOPY
Syntax
Xcopy [source][destination]
Example
Xcopy c:\lists.txt c:\tp\
46 TREE
Example
70
Output
The above command will display the tree structure of the current
directory. Following is an example of the output.
Folder PATH listing for volume Windows8_OS
Volume serial number is E41C-6F43
C:.
├───newdir
├───newdir1
└───newdir2
47 FC
Syntax
Fc [fileA] [fileB]
Where fileA and fileB are 2 files that need to be compared.
Example
@echo off
FC lists.txt listsA.txt
48 DISKPART
Example
@echo off
diskpart
Output
The above command shows the properties of disk partitions.
Following is an example of the output.
Microsoft DiskPart version 6.3.9600
Example
@echo off
Title “New Windows Title”
50 SET
Example
@echo off
set