BATCH files – What, Why and How?

Well you must be knowing or have heard about “Batch files” or “dot bat (.bat)” files if you have anything to do with computer stuffs. If you don’t know what it exactly is, then maybe this is a pretty good place to start with. If you are already an expert programmer an know all about it then you are most welcome to share your knowledge here.

What is a BATCH file?

DOS, OS/2 and even Microsoft Windows use batch files as script file. It has a reason it is called a batch file, because a batch file basically contains multiple DOS commands or a group of DOS commands to be executed sequentially by the command interpreter (Command prompt precisely). A batch file is run by COMMAND.com or cmd.exe which is similar to running shell scripts. Just so you know, COMMAND.com is not a website but a command interpreter in Microsoft Windows.

Why to use a BATCH file?

BATCH files are generally used to schedule or automate a task. For example, if you want to copy certain files from one directory to other every week, you can just create a text file with the DOS commands written in a sequence and save it with a .bat extension to make it a batch file. Execute it and your job is done. No need to do the task repeatedly.

So, basically YES, it is indeed useful sometimes.

How to use a BATCH file?

Batch files are pretty easy to create and execute. Just open any text editor (maybe notepad) and write the DOS commands in each line and save it as a .bat file (anyname.bat).

Here are few tips when you are creating a batch file of your own.

You may consider writing “@echo off” at the start or some point of the file where you don’t want the command prompt to display the processing steps of the commands.

example:

@echo off
time

Capturedf

Another keyword is “echo” which is used to print or display any text in the command prompt. This example batch file displays “Hello World!”, prompts and waits for the user to press a key, and then terminates.

@echo off
echo Hello World
pause

Capturedf

You can try few more DOS commands together in a single batch file. Here is a simple batch file to copy a folder’s content to another destination.

@echo off title Just Copying Few Stuffs to D Drive
echo Copying started..... 
xcopy /q /h /c /s "C:\My Folder" "D:\My New Folder"
echo Copying is done my friend 
msg * Copying is done. 
pause

Capturedf

To know more about DOS commands you can just type “help” in command prompt and to know details about any particular command write the command name followed by /? e.g. “xcopy /?”

To learn KEEP EXPERIMENTING.

Happy Coding ….

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s