Batch script
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.
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 a system file called the command
interpreter. On Windows systems, this interpreter is known as cmd.exe.
Functions
Ver functions
This batch command shows
the version of MS-DOS you are using
Syntax
Ver
Example
@echo off
Ver
Output
Microsoft windowso[6.3.9600]
Assoc functions
This is a batch command
that associates an extension with a file type (FTYPE), displays existing
associations, or deletes an association.
Syntax
Assoc – file path
Assoc | find “.doc” >
file path
Example
@echo off
Assoc > c:\lists.txt
Assoc | find”.doc” > c:\lists.txt
Output
.doc=word.docment8
.dochtml= word html file
cd functions
This batch command helps in making changes to a different directory, or
displays the current directory.
Syntax
Cd
Example
@echo off
Rem changing the program files
Cd\program files
Cd
Rem changing the path to program files
Output
C:\program files
Cls function
This batch command clears the screen.
Syntax
cls
Example
@echo off
Cls
The comment prompt screen
will be cleared
Copy function
This batch command is used for copying files from one location to the
other
Syntax
Copy[source][destination ]
Example
@echo off
Copy c:\list.txt c:\new folder
Output
All actions are performed
as per the remarks in the batch file.
Del functions
This batch command deletes files and not directories.
Syntax
Del[file name]
Example
@echo off
Del c:\lists.txt
Output
Delete the c directory
file
Dir function
This batch command lists the contents of a directory.
Syntax
Dir
Example
@echo off
Dir *.txt
Output
Give the .txt extension
file in dir
Date function
This batch command help to find the system date.
Syntax
Date
Example
@echo off
Date
Outputs
The current date will be
displayed in the command prompt.
Echo function
This batch command displays messages, or turns command
echoing on or off.
Syntax
Echo ”string”
Example
Echo “hello world”
Output
Hello world
Exit function
this batch command exits the DOS console.
Syntax
Exit
Example
@echo off
Echo “hello world”
Exit
Output
The batch file will
terminate and the command prompt window will close.
Md functions
This batch command creates a new directory in the current
location.
Syntax
Md[directory name]
Example
@echo off
Md newfolder
Cd new folder
Outputs
C:\new folder
Move function
This batch command moves files or directories between
directories.
Syntax
Move[source][destination]
Example
@echo off
move c:\list.txt c:\new folder
Output
All actions are performed
as per the remarks in the batch file.
Path function
This batch command displays or sets the path variable.
Syntax
Path
Example
@echo off
Echo%path%
Output
The value of the path
variable will be displayed in the command prompt.
Pause function
This batch command prompts the user and waits for a line of
input to be entered.
Syntax
Pause
Example
@echo off
Pause
Output
The command prompt will
show the message “Press any key to continue….” to the user and wait for the
user’s input.
Prompt function
This batch command can be used to change or reset the cmd.exe prompt.
Syntax
Prompt[newpromptname]
Example
@echo off
prompt myprompt$g
output
The prompt shown to the
user will now be myprompt>
Rd function
This batch command removes directories, but the directories need to be
empty before they can be removed.
Syntax
Rd [directory name]
Example
@echo off
Rd c:\newfolder
Output
All actions are performed
as per the remarks in the batch file.
Ren function
Renames files and directories
Syntax
Ren[old file name][new file name]
Example
@echo off
Ren c:\list.txt c:newlist.txt
Output
the file lists.txt will
be renamed to newlists.txt.
start function
This batch command starts a program in new window, or opens
a document
Syntax
Start”program name”
Example
@echo off
Start notepad.exe
Output
When the batch file is
executed, a new notepad windows will start.
Choice function
This batch command provides a list of options to the user.
Syntax
Choice /c [option] /m [message]
Example
@echo off
Echo “what is the file size”
Echo “a:10mb”
Echo “b:20mb”
Choice /c ab /m “what is your option a , b ”
Output
What is the file system
A:10mb
B:20mb
What is your option a, b
No comments:
Post a Comment