Batch Scrpting - Begineer To Core Level
Batch Scrpting - Begineer To Core Level
INTRODUCTION -
Batch Scripts are stored in simple text files containing lines with
commands that get executed in sequence, one after the other.
Scripting is a way by which one can alleviate this necessity by
automating these command sequences in order to make one’s
life at the shell easier and more productive. This tutorial
discusses the basic functionalities of Batch Script along with
relevant examples for easy understanding.
1 VER
This batch command shows the version of MS-DOS you are using.
2 ASSOC
This is a batch command that associates an extension with a file type (FTYPE),
displays existing associations, or deletes an association.
3 CD
This batch command helps in making changes to a different directory, or
displays the current directory.
4 CLS
This batch command clears the screen.
5 COPY
This batch command is used for copying files from one location to the other.
6 DEL
This batch command deletes files and not directories.
7 DIR
This batch command lists the contents of a directory.
8 DATE
This batch command help to find the system date.
9 ECHO
This batch command displays messages, or turns command echoing on or off.
10 EXIT
This batch command exits the DOS console.
11 MD
This batch command creates a new directory in the current location.
12 MOVE
This batch command moves files or directories between directories.
13 PATH
This batch command displays or sets the path variable.
14 PAUSE
This batch command prompts the user and waits for a line of input to be
entered.
15 PROMPT
This batch command can be used to change or reset the cmd.exe prompt.
16 RD
This batch command removes directories, but the directories need to be empty
before they can be removed.
17 REN
Renames files and directories
18 REM
This batch command is used for remarks in batch files, preventing the content
of the remark from being executed.
19 START
This batch command starts a program in new window, or opens a document.
20 TIME
This batch command sets or displays the time.
21 TYPE
This batch command prints the content of a file or files to the output.
22 VOL
This batch command displays the volume labels.
23 ATTRIB
Displays or sets the attributes of the files in the curret directory
24 CHKDSK
This batch command checks the disk for any problems.
25 CHOICE
This batch command provides a list of options to the user.
26 CMD
This batch command invokes another instance of command prompt.
27 COMP
This batch command compares 2 files based on the file size.
28 CONVERT
This batch command converts a volume from FAT16 or FAT32 file system to
NTFS file system.
29 DRIVERQUERY
This batch command shows all installed device drivers and their properties.
30 EXPAND
This batch command extracts files from compressed .cab cabinet files.
31 FIND
This batch command searches for a string in files or input, outputting matching
lines.
32 FORMAT
This batch command formats a disk to use Windows-supported file system such
as FAT, FAT32 or NTFS, thereby overwriting the previous content of the disk.
33 HELP
This batch command shows the list of Windows-supplied commands.
34 IPCONFIG
This batch command displays Windows IP Configuration. Shows configuration
by connection and the name of that connection.
35 LABEL
This batch command adds, sets or removes a disk label.
36 MORE
This batch command displays the contents of a file or files, one screen at a time.
37 NET
Provides various network services, depending on the command used.
38 PING
This batch command sends ICMP/IP "echo" packets over the network to the
designated address.
39 SHUTDOWN
This batch command shuts down a computer, or logs off the current user.
40 SORT
This batch command takes the input from a source file and sorts its contents
alphabetically, from A to Z or Z to A. It prints the output on the console.
41 SUBST
This batch command assigns a drive letter to a local folder, displays current
assignments, or removes an assignment.
42 SYSTEMINFO
This batch command shows configuration of a computer and its operating
system.
43 TASKKILL
This batch command ends one or more tasks.
44 TASKLIST
This batch command lists tasks, including task name and process id (PID).
45 XCOPY
This batch command copies files and directories in a more advanced way.
46 TREE
This batch command displays a tree of all subdirectories of the current directory
to any level of recursion or depth.
47 FC
This batch command lists the actual differences between two files.
48 DISKPART
This batch command shows and configures the properties of disk partitions.
49 TITLE
This batch command sets the title displayed in the console window.
50 SET
Displays the list of environment variables on the current system.
Command ~
@ECHO OFF
DEL .
Deletes All files in the Current Directory With Prompts and Warnings.
Try to avoid spaces when naming batch files, it sometime creates issues
when they are called from other scripts.
Don’t name them after common batch files which are available in the system
such as ping.cmd.
Remember to put the .bat or .cmd at the end of the file name.
Choose the “Save as type” option as “All Files”.
Step 1 − Open the folder where you saved and created the file.
Step 3 – This Will Execute the Batch File But Will Not Show
What Happened.
Step 3 − Right-click the file and choose the “Edit” option from
the context menu. The file will open in Notepad for further
editing.
Batch Script – Syntax
Normally, the first line in a batch file often consists of the following command.
ECHO Command –
@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
command "echo off" turns off the display for the whole script, except for
the "echo off" command itself. The "at" sign "@" in front makes the
command apply to itself as well.
Rem - Very often batch files also contains lines that start with the "Rem"
command. This is a way to enter comments and documentation. The
computer ignores anything on a line following Rem. For batch files with
increasing amount of complexity, this is often a good idea to have
comments.
Let’s construct our simple first batch script program. Open notepad and enter the
following lines of code. Save the file as “List.cmd”.
@echo off
Rem This is for listing down all the files in the directory Program files
dir "C:\Program Files" > C:\Users\sid\Documents\lists.txt
echo "The program has completed"
If the above batch script is stored in a file called test.bat and we were to run
the batch as –
Test.bat 1 2 3
The above command produces the following output.
1
2
3
Set Command
The other way in which variables can be initialized is via the ‘set’ command.
Following is the syntax of the set command.
Syntax
set /A variable-name=value
where,
variable-name is the name of the variable you want to set.
value is the value which needs to be set against the variable.
/A – This switch is used if the value needs to be numeric in nature.
The following example shows a simple way the set command can be
used.
Example -
@echo off
set message=Hello World
echo %message%
In the above code snippet, a variable called message is defined and set with the value
of "Hello World".
To display the value of the variable, note that the variable needs to be enclosed in the
% sign.
Output -
The above command produces the following output.
Hello World
In batch script, it is also possible to define a variable to hold a numeric value. This
can be done by using the /A switch.
The following code shows a simple way in which numeric values can be set with the
/A switch.
@echo off
SET /A a = 5
SET /A b = 10
SET /A c = %a% + %b%
echo %c%
All of the arithmetic operators work in batch files. The following example shows
arithmetic operators can be used in batch files.
@echo off
SET /A a = 5
SET /A b = 10
SET /A c = %a% + %b%
echo %c%
SET /A c = %a% - %b%
echo %c%
SET /A c = %b% / %a%
echo %c%
SET /A c = %b% * %a%
echo %c%
Example
@echo off
set globalvar = 5
SETLOCAL
set var = 13145
set /A var = %var% + 5
echo %var%
echo %globalvar%
ENDLOCAL
Few key things to note about the above program.
The ‘globalvar’ is defined with a global scope and is available throughout the
entire script.
The ‘var‘ variable is defined in a local scope because it is enclosed between a
‘SETLOCAL’ and ‘ENDLOCAL’ block. Hence, this variable will be destroyed
as soon the ‘ENDLOCAL’ statement is executed.
Output
The above command produces the following output.
13150
5
@echo off
echo %JAVA_HOME%
The output would show the JAVA_HOME directory which would depend from system
to system. Following is an example of an output.
%JAVA_HOME%
Batch Script – Comments
ECHO OFF
IF NOT "%OS%"=="Windows_NT" GOTO Syntax
ECHO.%* | FIND "?" >NUL
IF NOT ERRORLEVEL 1 GOTO Syntax
IF NOT [%2]==[] GOTO Syntax
SETLOCAL
SET WSS=
IF NOT [%1]==[] FOR /F "tokens = 1 delims = \ " %%A IN ('ECHO.%~1') DO SET WSS = %%A
FOR /F "tokens = 1 delims = \ " %%a IN ('NET VIEW ^| FIND /I "\\%WSS%"') DO FOR /F
"tokens = 1 delims = " %%A IN ('NBTSTAT -a %%a ^| FIND /I /V "%%a" ^| FIND "<03>"')
DO ECHO.%%a %%A
ENDLOCAL
GOTO:EOF
ECHO Display logged on users and their workstations.
ECHO Usage: ACTUSR [ filter ]
IF "%OS%"=="Windows_NT" ECHO Where: filter is the first part
of the computer name^(s^) to be displayed
Syntax
Rem Remarks
@echo off
Rem This program just displays Hello World
set message=Hello World
echo %message%
Output
The above command produces the following output. You will notice that the line with
the Rem statement will not be executed.
Hello World
Syntax
:: Remarks
where ‘Remarks’ is the comment which needs to be added.
The following example shows the usage of the "::" command.
Example
@echo off
:: This program just displays Hello World
set message = Hello World
echo %message%
Output
The above command produces the following output. You will notice that
the line with the :: statement will not be executed.
Hello World
Note − If you have too many lines of Rem, it could slow down the code, because in
the end each line of code in the batch file still needs to be executed.
Let’s look at the example of the large script we saw at the beginning of this topic and
see how it looks when documentation is added to it.
::===============================================================
:: The below example is used to find computer and logged on users
::
::===============================================================
ECHO OFF
:: Windows version check
IF NOT "%OS%"=="Windows_NT" GOTO Syntax
ECHO.%* | FIND "?" >NUL
:: Command line parameter check
IF NOT ERRORLEVEL 1 GOTO Syntax
IF NOT [%2]==[] GOTO Syntax
:: Keep variable local
SETLOCAL
:: Initialize variable
SET WSS=
:: Parse command line parameter
IF NOT [%1]==[] FOR /F "tokens = 1 delims = \ " %%A IN ('ECHO.%~1') DO SET WSS = %%A
:: Use NET VIEW and NBTSTAT to find computers and logged on users
FOR /F "tokens = 1 delims = \ " %%a IN ('NET VIEW ^| FIND /I "\\%WSS%"') DO FOR /F
"tokens = 1 delims = " %%A IN ('NBTSTAT -a %%a ^| FIND /I /V "%%a" ^| FIND
"<03>"') DO ECHO.%%a %%A
:: Done
ENDLOCAL
GOTO:EOF
:Syntax
ECHO Display logged on users and their workstations.
ECHO Usage: ACTUSR [ filter ]
IF "%OS%"=="Windows_NT" ECHO Where: filter is the first part of the
computer name^(s^) to be displayed
You can now see that the code has become more understandable to
users who have not developed the code and hence is more maintainable.
Batch Script – Strings
1 Create String
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.
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.
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.
7 Align Right
This used to align text to the right, which is normally used to improve readability of
number columns.
8 Left String
This is used to extract characters from the beginning of a string.
9 Mid String
This is used to extract a substring via the position of the characters in the string.
10 Remove
The string substitution feature can also be used to remove a substring from another
string.
11 Remove Both Ends
This is used to remove the first and the last character of a string.
12 Remove All Spaces
This is used to remove all spaces in a string via substitution.
13 Replace a String
To replace a substring with another string use the string substitution feature.
14 Right String
This is used to extract characters from the end of a string.
Batch Script – 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.
Each element of the array needs to be defined with the set command.
The ‘for’ loop would be required to iterate through the values of the array.
Creating an 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
set list=1 2 3 4
(for %%a in (%list%) do (
echo %%a
))
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.
Example
@echo off
set a[0]=1
echo %a[0]%
In this example, the index starts from 0 which means the first element can be
accessed using index as 0, the second element can be accessed using index as 1
and so on. Let's check the following example to create, initialize and access arrays −
@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]%
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]%
You can modify an existing element of an Array by assigning a new value at a given
index as shown in the following example −
@echo off
set a[0]=1
set a[1]=2
set a[2]=3
Rem Setting the new value for the second element of the array
Set a[1]=5
echo The new value of the second element of the array is %a[1]%
Iterating over an array is achieved by using the ‘for’ loop and going through each
element of the array. The following example shows a simple way that an array can
be implemented.
@echo off
setlocal enabledelayedexpansion
set topic[0]=comments
set topic[1]=variables
set topic[2]=Arrays
set topic[3]=Decision making
set topic[4]=Time and date
set topic[5]=Operators
Output
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
Structures can also be implemented in batch files using a little bit of an extra coding
for implementation. The following example shows how this can be achieved.
Example
@echo off
set obj[0].Name=Joe
set obj[0].ID=1
set obj[1].Name=Mark
set obj[1].ID=2
set obj[2].Name=Mohan
set obj[2].ID=3
FOR /L %%i IN (0 1 2) DO (
call echo Name = %%obj[%%i].Name%%
call echo Value = %%obj[%%i].ID%%
)
The following key things need to be noted about the above code.
Each variable defined using the set command has 2 values associated with
each index of the array.
The variable i is set to 0 so that we can loop through the structure will the length
of the array which is 3.
We always check for the condition on whether the value of i is equal to the
value of len and if not, we loop through the code.
We are able to access each element of the structure using the obj[%i%]
notation.
Output
Arithmetic operators
Relational operators
Logical operators
Assignment operators
Bitwise operators
Arithmetic Operators.
Operator Description
Bitwise Operators
Bitwise operators are also possible in batch script. Following are the operators
available.
Operator Description
The date and time in DOS Scripting have the following two basic commands for
retrieving the date and time of the system.
DATE.
Syntax
DATE
Example
@echo off
echo %DATE%
Output
The current date will be displayed in the command prompt. For example,
Mon 12/28/2015
TIME
This command sets or displays the time.
Syntax
TIME
Example
@echo off
echo %TIME%
Output
The current system time will be displayed. For example,
22:06:52.87
Following are some implementations which can be used to get the date and time in
different formats.
Batch Script - Input / Output
There are three universal “files” for keyboard input, printing text on the screen and
printing errors on the screen. The “Standard In” file, known as stdin, contains the
input to the program/script. The “Standard Out” file, known as stdout, is used to write
output for display on the screen. Finally, the “Standard Err” file, known as stderr,
contains any error messages for display on the screen.
Each of these three standard files, otherwise known as the standard streams, are
referenced using the numbers 0, 1, and 2. Stdin is file 0, stdout is file 1, and stderr is
file 2.
In the above example, the stdout of the command Dir C:\ is redirected to the file
list.txt.
If you append the number 2 to the redirection filter, then it would redirect the stderr to
the file lists.txt.
One can even combine the stdout and stderr streams using the file number and the
‘&’ prefix. Following is an example.
Stdin
To work with the Stdin, you have to use a workaround to achieve this. This can be
done by redirecting the command prompt’s own stdin, called CON.
The following example shows how you can redirect the output to a file called lists.txt.
After you execute the below command, the command prompt will take all the input
entered by user till it gets an EOF character. Later, it sends all the input to the file
lists.txt.
2 The system cannot find the file specified. Indicates that the file cannot be
found in specified location.
3 The system cannot find the path specified. Indicates that the specified path
cannot be found.
Error Level
The environmental variable %ERRORLEVEL% contains the return code
of the last executed program or script.
By default, the way to check for the ERRORLEVEL is via the following
code.
Syntax
IF %ERRORLEVEL% NEQ 0 (
DO_Something
)
Output
In the above program, we can have the following scenarios as the output
If the file c:\lists.txt does not exist, then nothing will be displayed in
the console output.
If the variable userprofile does not exist, then nothing will be
displayed in the console output.
If both of the above condition passes then the string “Successful
completion” will be displayed in the command prompt.
Batch Script – Loops
Loops
@ECHO OFF
:Loop
Output
Let’s assume that our above code is stored in a file called Test.bat. The
above command will produce the following output if the batch file passes
the command line arguments of 1,2 and 3 as Test.bat 1 2 3.
1
2
3
Batch Script – Functions
Function Definition
In Batch Script, a function is defined by using the label statement. When
a function is newly defined, it may take one or several values as input
'parameters' to the function, process the functions in the main body, and
pass back the values to the functions as output 'return types'.
Every function has a function name, which describes the task that the
function performs. To use a function, you "call" that function with its name
and pass its input values (known as arguments) that matches the types
of the function's parameters.
:function_name
Do_something
EXIT /B 0
Example
:Display
SET /A index=2
echo The value of index is %index%
EXIT /B 0
1 Calling a Function
In Batch Script, the TASKLIST command can be used to get the list of
currently running processes within a system.
Syntax
1. /S system
Specifies the remote system to connect to
2. /U
[domain\]user
Specifies the user context under which the command should execute.
3. /P [password]
Specifies the password for the given user context. Prompts for input if omitted.
4. /M [module]
Lists all tasks currently using the given exe/dll name. If the module name is not
specified all loaded modules are displayed.
5. /SVC
Displays services hosted in each process.
6. /V
Displays verbose task information.
7. /FI filter
Displays a set of tasks that match a given criteria specified by the filter.
8. /FO format
Specifies the output format. Valid values: "TABLE", "LIST", "CSV".
9. /NH
Specifies that the "Column Header" should not show in the output. Valid only for
"TABLE" and "CSV" formats.
Examples
TASKLIST
The above command will get the list of all the processes running on your
local system. Following is a snapshot of the output which is rendered
when the above command is run as it is. As you can see from the
following output, not only do you get the various processes running on
your system, you also get the memory usage of each process.
The above command takes the output displayed by tasklist and saves it
to the process.txt file.
The above command will only fetch those processes whose memory is
greater than 40MB. Following is a sample output that can be rendered.
Syntax
1. /S system
Specifies the remote system to connect to
2. /U
[domain\]user
Specifies the user context under which the command should execute.
3. /P [password]
Specifies the password for the given user context. Prompts for input if omitted.
4. /FI
FilterName
Applies a filter to select a set of tasks. Allows "*" to be used. ex. imagename eq
acme* See below filters for additional information and examples.
5. /PID
processID
Specifies the PID of the process to be terminated. Use TaskList to get the PID.
6. /IM
ImageName
Specifies the image name of the process to be terminated. Wildcard '*' can be used
to specify all tasks or image names.
7. /T
Terminates the specified process and any child processes which were started by it.
8. /F
Specifies to forcefully terminate the process(es).
Examples
DOS scripting also has the availability to start a new process altogether.
This is achieved by using the START command.
Syntax
Wherein
1. /MIN
Start window Minimized
2. /MAX
Start window maximized.
3. /LOW
Use IDLE priority class.
4. /NORMAL
Use NORMAL priority class.
5. /ABOVENORMAL
Use ABOVENORMAL priority class.
6. /BELOWNORMAL
Use BELOWNORMAL priority class.
7. /HIGH
Use HIGH priority class.
8. /REALTIME
Use REALTIME priority class.
Examples
The above command will run the batch script test.bat in a new window. The windows
will start in the minimized mode and also have the title of “Test Batch Script”.
The above command will actually run Microsoft word in another process
and then open the file TESTA.txt in MS Word.
Batch Script – Aliases
Creating an Alias
Syntax
1. /REINSTALL
Installs a new copy of Doskey
2. /LISTSIZE = size
Sets size of command history buffer.
3. /MACROS
Displays all Doskey macros.
4. /MACROS:ALL
Displays all Doskey macros for all executables which have Doskey macros.
5. /MACROS:exename
Displays all Doskey macros for the given executable.
6.
/HISTORY
Displays all commands stored in memory.
7. /INSERT
Specifies that new text you type is inserted in old text.
8. /OVERSTRIKE
Specifies that new text overwrites old text.
9. /EXENAME = exename
Specifies the executable.
10.
/MACROFILE = filename
Specifies a file of macros to install.
11. macroname
Specifies a name for a macro you create.
12. text
Specifies commands you want to record.
Example
Create a new file called shortcut.bat and enter the following commands in the file.
The below commands creates two aliases, one if for the cd command, which
automatically goes to the directory called test. And the other is for the dir command.
@echo off
doskey cd = cd/test
doskey d = dir
Once you execute the command, you will able to run these aliases in
the command prompt.
Output
The following screenshot shows that after the above created batch file is
executed, you can freely enter the ‘d’ command and it will give you the
directory listing which means that your alias has been created.
Batch Script – Network
1 NET ACCOUNTS
View the current password & logon restrictions for the computer.
2 NET CONFIG
Displays your current server or workgroup settings.
3 NET COMPUTER
Adds or removes a computer attached to the windows domain controller.
4 NET USER
This command can be used for the following
View the details of a particular user account.
5 NET STOP/START
This command is used to stop and start a particular service.
6 NET STATISTICS
Display network statistics of the workstation or server.
7 NET USE
Connects or disconnects your computer from a shared resource or displays
information about your connections.
Batch Script – 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.
This chapter looks at various functions such as querying values, adding,
deleting and editing values from the registry.
Reading from the registry is done via the REG QUERY command.
2 Adding to the Registry
Adding to the registry is done via the REG ADD command.
3 Deleting from the Registry
Deleting from the registry is done via the REG DEL command.
4 Copying Registry Keys
Copying from the registry is done via the REG COPY command.
5 Comparing Registry Keys
Comparing registry keys is done via the REG COMPARE command.
Reading from the Registry
Syntax
Where RegKey is the key which needs to be searched for in the registry.
Example
@echo off
REG QUERY
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Windows\
The above command will query all the keys and their respective values
under the registry key
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Windows\
Output
The output will display all the keys and values under the registry key.
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\W
indows\
This location in the registry has some key information about the windows
system such as the System Directory location.
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Windo
ws
Directory REG_EXPAND_SZ %SystemRoot%
SystemDirectory REG_EXPAND_SZ %SystemRoot%\system32
NoInteractiveServices REG_DWORD 0x1
CSDBuildNumber REG_DWORD 0x4000
ShellErrorMode REG_DWORD 0x1
ComponentizedBuild REG_DWORD 0x1
CSDVersion REG_DWORD 0x0
ErrorMode REG_DWORD 0x0
CSDReleaseType REG_DWORD 0x0
ShutdownTime REG_BINARY 3AFEF5D05D46D101
Adding to the Registry
Syntax
The REG ADD command has the following variations. In the second
variation, no name is specified for the key and it will add the name of
“(Default)” for the key.
Where
ValueName − The value, under the selected RegKey, to edit.
/d Data − The actual data to store as a "String", integer, etc.
/f − Force an update without prompting "Value exists, overwrite
Y/N".
/S Separator − Character to use as the separator in
REG_MULTI_SZ values. The default is "\0".
/t DataType − These are the data types defined as per the registry
standards which can be −
o REG_SZ (default)
o REG_DWORD
o REG_EXPAND_SZ
o REG_MULTI_SZ
Example
@echo off
REG ADD HKEY_CURRENT_USER\Console /v Test /d "Test
Data"
REG QUERY HKEY_CURRENT_USER\Console /v Test
In the above example, the first part is to add a key into the registry under
the location HKEY_CURRENT_USER\Console. This key will have a
name of Test and the value assigned to the key will be Test Data which
will be of the default string type.
The second command just displays what was added to the registry by
using the REG QUERY command.
Output
Following will be the output of the above program. The first line of the
output shows that the ‘Add’ functionality was successful and the second
output shows the inserted value into the registry.
Syntax
The REG DELETE command has the following variations. In the second
variation, the default value will be removed and in the last variation all the
values under the specified key will be removed.
Where
ValueName − The value, under the selected RegKey, to edit.
/f − Force an update without prompting "Value exists, overwrite
Y/N".
Example
@echo off
REG DELETE HKEY_CURRENT_USER\Console /v Test /f
REG QUERY HKEY_CURRENT_USER\Console /v Test
In the above example, the first part is to delete a key into the registry
under the location HKEY_CURRENT_USER\Console. This key has the
name of Test. The second command just displays what was deleted to
the registry by using the REG QUERY command. From this command,
we should expect an error, just to ensure that our key was in fact deleted.
Output
Following will be the output of the above program. The first line of the
output shows that the ‘Delete’ functionality was successful and the
second output shows an error which was expected to confirm that indeed
our key was deleted from the registry.
Syntax
Example
@echo off
REG COPY HKEY_CURRENT_USER\Console
HKEY_CURRENT_USER\Console\Test
REG QUERY HKEY_CURRENT_USER\Console\Test
In the above example, the first part is to copy the contents from the
location HKEY_CURRENT_USER\Console into the location
HKEY_CURRENT_USER\Console\Test on the same machine. The
second command is used to query the new location to check if all the
values were copied properly.
Output
Printing can also be controlled from within Batch Script via the NET
PRINT command.
Syntax
Example
The above command will print the example.txt file to the parallel port lpt1.
As of Windows 2000, many, but not all, printer settings can be configured from
Windows's command line using PRINTUI.DLL and RUNDLL32.EXE
Syntax
Example
IF EXIST "%file%" (
ECHO %PrinterName% printer exists
) ELSE (
ECHO %PrinterName% printer does NOT exists
)
It will first set the printer name and set a file name which will hold
the settings of the printer.
The RUNDLL32.EXE PRINTUI.DLL commands will be used to
check if the printer actually exists by sending the configuration
settings of the file to the file Prt.txt
Useful Books on Batch Script
DIR MAKER
FILE DELETER
TEXT FILE PRINTER
AUTOMATIC PROGRAMS STARTUP
STORING BATCH RESULTS INTO TEXT FILE
SIMPLE MATRIX FUN PT.1
SIMPLE MATRIX FUN PT. 2
AUTO SHUTDOWN, RESTART TOOL
GETTING RUNNING TASK INTO TEXT FILE
GRAPHICAL REPRESENTATION OF DRIVE FILES
AUTOMATIC PING TOOL
SYSINFO CATCHER
SMALL VIRUS TO HANG SYSTEM
SIMPLE BATCH VIRUS TO CORRUPT THE SYSTEM
DDOS ATTACKER
GRABING SAM AND SYSTEM FILES
MAKE OWN TERMINAL WITH CUSTOM TITLE &
USER
AUTOMATIC FILE HIDDER AND UNHIDER WITH
DETAILS
AUTOMATIC SITE OPENER PROGRAM
AUTOMATIC SEARCH ENGINE
AUTOMATIC FILE COPIER, MOVER PROGRAM
AUTOMATIC DRIVE/DIRECTORY DETAILS SAVER
SIMPLE CLOCK PROGRAM
PIN GENERATOR
Specialized Command Prompt
https://www.geeksforgeeks.org/basics-of-batch-scripting/
http://www.trytoprogram.com/batch-file/
https://en.wikipedia.org/wiki/Batch_file
https://www.instructables.com/Some-Cool-Batch-
Applications/
https://www.tech2hack.com/create-dangerous-notepad-
virus/
~~~~ END OF THE COURSE ~~~~