Batch File Programming- Basics In Detail

Image Credit:

Image Credit:

If you are a new windows user then you surely might have noticed the command prompt in your accessories. I have asked this question a million times, what is the use of Command Prompt. Actually you can do anything from it which you are able to do from other programs. It is same kind of program like the Terminal in Linux, No limits.

Microsoft windows has a clear image among the developers as the beginners operating system with the basic stuff and useless for professional web developers, programmers and hackers. I personally believe Windows is not such a bad operating system although i am a huge of the Linux kernel but i can’t resist using the dual boot environment for Windows 8 on my Computer.

The command prompt gives you an experience to work like your father must have worked in his early days. There were no graphics and working on computers was so boring because of the text interface or the DOS environment. The command prompt is an approach of Microsoft to give a small touch of the DOS in their successful GUI operating systems after all it was the DOS which gave Bill Gates the start he wished and made the initial contribution to the success of Microsoft.

The Command prompt is an interpreter of the Batch File programming Language which is a native programming environment offered by Microsoft in its operating systems. Batch programs are created using text editors like notepad, WordPad etc.

The MS Batch comprises of a sequence of built-in commands which are used to perform some often done tasks like deleting similar copies of a same file, creating system logs, clearing the crap on your hard disk and some inside evil uses.

A batch program is interpreted line by line by the CLI (Command line Interpreter) which is popularly known as the command prompt.

Now you might be thinking, what is the use of using batch when you have a number of tools available? The answer is quite simple, the simplicity and satisfaction of doing the job manually and the fun of playing because you can create any program to do anything by putting in just a few efforts, a small research and a lot of practice.

There are two different modes that are supported by batch programming or the DOS

1. Interactive Mode- In this mode the program on executing asks the user for his input and further processes the command. To make this line clear let’s take an example of the simple delete command (Del). I will be providing a whole list of some most important and popularly used commands later on in this post.

Suppose i am writing a program to delete the entire file named “xyz” in a drive. So now when i will execute the program it will ask me whether i am sure to delete or not in the classic Y/N format. “If you are sure press Y”, and the program will proceed further on the basis of my input.

C:\>del xyz
C:\a\*, Are you sure (Y/N)? Y

This program is a sample program whose output is explained above. The syntax might be difficult in understanding if you don’t know the whole format, all the commands and their functions. Don’t worry i will provide them all later in this post. For now let’s proceed with the second mode of batch programming which is known as….

2. The Batch Mode- It is just the opposite of the interactive mode here the program doesn’t care about the user’s input and performs all the functions on its own as all the jobs it has to are predefined while creating the program.

Here also we will take the example of deleting files named “xyz”. On executing the program unlike the previous case here we will not be asked whether sure or not it will just delete the files as it has been programmed to do so.

C:\>del /Q xyz

This sample program is for the batch mode where the user will not be asked anything. The job will be directly done (sounds like the old merciless kings, HANG HIM!)

In the above programs i have tried to delete the same files but by using the different switch although both will perform the same functions only the way it operates is different.

Ok that was all with a small introduction of the programming language; now let us see how to create a batch file which is not such a difficult task.

As i previously said a batch program is created using text editors so open any text editor of your choice, i prefer the simple notepad and the notepad++ for the simplicity.

Type the program, here is a small example of a “hello world” post which just displays the text on your screen

@echo off
Echo Hello World

Now save the file but this time save it with the format “.bat”, here i am saving it with “firstprogram.bat”.

When you save a batch file the icon changes, the icon will appear something like a bicycle gear in box.

Now all you need to do is just double click the batch program you just created and it will start its job.

As i promised you earlier with all the basic trivia i will also provide you some commands of the batch file programming.

There are two types of commands in batch.

1. Internal commands- These commands are built in commands that are shipped with the operating system some examples of these types of commands are

echo , it is the simplest and the most popularly used command which is used to print text and numbers on the screen.

cls– it also one the well-known commands which is used to clear the output screen

del– we have already discussed it, used for deleting files and folders

dir– one of the populars, it stands for directory

exit– The batch output and input screen or the command prompt can’t be closed with the Alt+F4 shortcut now wither you close it by taking your mouse to the red cross on the corner or type in “exit” and press enter.

2. External Commands- These types of commands are often created while installing a new application and these commands are mostly used for calling the application and its support files to run. These external commands are executed in the RUN dialog box which you can open by pressing the super key also known as the windows key and R or take the long way by going to the start and accessories.

As i said in the first few lines that the batch programming is comprised of a sequence of run line commands so knowing some run line commands is necessary if you want to study batch programming in detail.

access.cpl Accessibility Controls

accwiz Accessibility Wizard

appwiz.cpl Add/Remove Programs

ciadv.msc Indexing Service

control admintools Administrative Tools

cleanmgr Disk Cleanup Utility

control color Display Properties

compmgmt.msc Computer Management Console

control folders Folder Options

cliconfg SQL Client Configuration

certmgr.msc Certificate Manager

charmap Character Map

chkdsk Check Disk Utility

clipbrd Clipboard Viewer

calc Opens calculator

cmd Opens command prompt

devmgmt.msc Device Manager

dfrg.msc Disk Defragmenter

diskmgmt.msc Disk Management

dcomcnfg Component Services

ddeshare DDE Shares

diskpart Disk Partition Manager

desk.cpl Display Properties

drwtsn32 Dr. Watson

directx.cpl Direct X Control Panel

dxdiag Direct X Troubleshooter

eudcedit Private Character Editor

eventvwr.msc Event Viewer ( Maintaining System Logs )

explorer Opens My Documents

freecell FreeCell Game

fsquirt Bluetooth Transfer Wizard

fsmgmt.msc Shared Folders

gpedit.msc Group Policy Editor

hdwwiz.cpl Add Hardware Wizard

iexpress Iexpress Wizard (Package creator)

iexplore Internet Explorer

inetcpl.cpl Internet Explorer Properties

ipconfig Windows IP Configuration

intl.cpl Regional Settings

joy.cpl Game Controllers

lusrmgr.msc Local Users and Groups

logoff Logs out current user

magnify Open Magnifier

makecab Cabinet Maker, file compressor.

msconfig Open System Configuration Utility

mshearts Opens Hearts game

msinfo32 System Information

mspaint Opens Mspaint

msmsgs Windows Messenger

mstsc Remote Desktop

mmsys.cpl Sounds and Audio

mqbkup Message Queue Backup\Restore Utility

notepad Opens a New Notepad

ntmsmgr.msc Removable Storage

ntmsoprq.msc Removable Storage Operator Requests

ncpa.cpl Network Connections

netsetup.cpl Network Setup Wizard

openfiles Used to view Files Opened Remotely via local
share points

odbccp32.cpl ODBC Data Source Administrator

osk On Screen Keyboard

proxycfg Proxy configuration

packager Object Packager

perfmon.msc Performance Monitor

powercfg.cpl Power Options

pentnt Checks for Floating point error in Intel based

qappsrv Displays the available application terminal servers
on the network.

qprocess Displays information about processes

qwinsta Display information about Terminal Sessions

rcp Copies files to and from computer running the RCP

recover Recovers readable information from a bad or
defective disk.

relog Used for Logging.

replace Replaces files

rexec Runs commands on remote hosts running the
REXEC service

route Manipulates network routing tables

rsh Runs commands on remote hosts running the RSH

rsm Manages media resources using Removable

runas Allows a user to run specific tools and programs
with different permissions than the user’s current
logon provides.

regedit Opens Registry Editor

rsop.msc Resultant Set of Policy

rwinsta Reset the session

rasphone Remote Access Phonebook

services.msc Used for Managing all the services on the

sigverif File Signature Verification Tool

secpol.msc Local Security Settings

shutdown Shutdown Windows

syskey Windows System Security Tool

sc Communicates with the service controller and
installed services.

schtasks Replaced with at.

setver Sets the version number that MS-DOS reports to a

shadow Helps in remote connection & network used to
monitor another Terminal Services session

shrpubw Shared Folder Wizard

sndvol32 Volume Control

sysedit Windows.ini, system.ini, config.sys, autoexec.bat

sol Opens up Solitaire Game

timedate.cpl Date and Time Properties

telephon.cpl Phone and Modem Options

telnet Telnet Client

tftp Transfers files to and from a remote computer
running the TFTP service

tlntadmn Telnet Administration. Used to start, stop, and send
msg to a terminal session connected to via telnet.

tscon Attaches a user session to a terminal session.

tsdiscon Disconnects a session from a terminal server.

tskill Ends a process. Even can terminate a process
running on a remote session.

tourstart Windows XP Tour Wizard

tsshutdn shutdown in 60 sec

typeperf Very useful in login events. Used to monitor
Processor threads and writes into a specified log

userinit My Documents

verifier Driver Verifier Utility

winchat Microsoft Chat

winmine Minesweeper Game

wuaucpl.cpl Automatic Updates

wscui.cpl Security Center

wmplayer Windows Media Player

wmimgmt.msc Windows Management Infrastructure

w32tm Tool used to diagnose problems occurring with
Windows Time. register to run as a service and add
default configuration to the registry

winmsd System Information.

wupdmgr Windows Update Launches

winver Displays Windows Version

write Opens WordPad

The working of these commands might vary on the basis of the version of your operating system.

To know more about batch programming wait for the next article in the series where we will discuss some more things about it. And if you liked this article and my hard work then don’t hesitate to share it on your favorite social networking sites or gives us your feedback by commenting below or by using our contact form.

Leave an awesome and valuable reply