Your Internet Explorer is too old :( Please upgrade to latest IE or switch to Firefox/Google Chrome for better view!

October 5, 2011

Beginning with Batch Files

SCG command prompt
This article is about the windows batch programming, inside this, my own views and knowledge about the batch programming. This is a wide topic, here I will just give you overview of this, for more and query or discussion you can contact me.

What actually batch files are:
batch files are scripts written in English and contains command that executed ‘line by line’ by command interpreter in Microsoft operating systems.
It just like that you write a command on command prompt (cmd.exe) and execute it. Same as you can write those commands in text files and save with batch files extensions.
What does batch file do:
batch files give automation to your tasks, Tasks, which you do frequently, without using GUI.
Like file handling, operation, maintenance.

Need of batch files:
some of the people think that it is old and outdated language so why do we learn it? When we have other OOPs languages and good demand of them in market so why. So I will say yes! The same tasks you can do in C or other languages, but remember it is scripting NOT a Language. It is like VBS scripts or Java script.
You don’t need to compile and build, each time you do a change in it, just save it, and it is ready. Self-interest in windows and craziness of peoples force them to learn batch files.
I use them to integrate in SETUP programs, other languages applications, and customization in windows functionality.

File Extensions:
there are two type of extensions used widely .CMD and .BAT (not case sensitive)
cmd is short form of command and Bat you know of batch.
.cmd extension used in windows NT family system and also work with windows latest versions.
.bat works on DOS and windows versions.
I recommend you to use .bat it’s common and have more compatibility than .CMD

What are commands:
when a batch file executed it’ s interpreted by command interpreter. And commands are like program function which take command line argument and give the output.
Some of the commands are inbuilt in CMD.exe and some are external files for instance “copy” command is built in cmd.exe and “xcopy” is an external file located in system32 directory with name “xcopy.exe”
it takes command line argument so you cannot use it directly.
You can create your own commands too create a program that do some specific task (windows console application), and save it to system32 or windows directory. Then call it from commands prompt or batch files.
Note: commands are not case sensitive not even arguments.

About “Path” variable:
when you give some command or files name on command prompt or form anywhere else, windows does not search for whole system not even system drive it just look for the specific folders which is define in system variable called “path”.
If you want that your folder to be indexed by system you can add you folder path, I hope you did that if you ever learned java. You can check out by typing “path” in cmd for list indexed paths.

Some common commands:
cmd : type cmd /? and read you will know more than this about command prompt.
echo : show messages (like printf() in C), & turns command-echoing on or off.
cd : current directory, now learn only CD .. to go previous folder
copy : copy files to other locations
move : for move files to other folders
Help : typing help command display common commands, typing command name the with /? Parameter display command function. E.g. copy /? Or md /?

I’ll advice you to learn and see all commands before creating batch files.

First batch file:
echo off
color 0a
title Geeks
echo this is me and my batch file

Now save it as anyname.bat and run
how it is working:
1st command: turned off the path display of command prompt
2nd command: changed the color of command prompt ‘0’ for black in bg ‘a’ for green in fg.
3rd command: changed the title of command prompt
4th command: clear the screen like clrscr in c
5th command: display message
6th command: to hold the screen like getch in C

Shortcut for batch programming:
to open : windowkey+r for run menu then type cmd and hit
admin mode : click on start menu, type cmd, hit enter with CTRL+ALT+Shift (windows vista and 7)
previous command : use up arrow key on cmd to see previous command
copy cmd screen text : right click then mark text then hit enter
pasting on screen : normal way
full screen : ALT+Enter
terminating batch command : CTRL+break
File/Folder name suggestions : TAB Key

Another useful example:
okay this file is so simple but quite useful, this will work like an application, any file you will open with this batch file, the file will be copied to your defined location. Here is the code…

echo off
copy %1 F:\My_Privte_Folder

in this you will see copy command , so format of copy command is “copy sourceFileName destinationAddress”.
Here we’ve given %1 for source file name because it’s a variable and will be replaced by filename, which you’ll launch with this batch file.
F:\My_Private_Folder is destination folder you can replace it with your path (absolute path).
Save this file with name.bat, at anywhere in computer.
now right click on any file (not an application file)
choose “open with” and choose “default Program”
then click browse, look for your batch file location (but remember to uncheck the check box of “ALWAYS USE THE SELECTED APPLICATION TO OPEN THIS KIND OF FILE” )then click open, now your file will be copied to My_private_Folder in F drive.
Next time you don’t need to do all this, for this kind of file, just right click and select "open with" you will see the first option is your batch file select and copied.

Gradually I will post on some advance topic about batch files.
you can use Windows XP Help & support center to learn batch files and commands.
Don't forget to drop your comment here!


Post a Comment