About us  |  Contact  |  Sitemap   

< ? php if (is_front_page()) { dynamic_content_gallery(); } ? >

Fundamentals of Batch File processing

September 29, 2010

in Latest Posts,Tutorials,Windows

Batch file processing

MS-DOS is the first operating system and it is the software origin of Computers. In early days of generation of Computers, MS-DOS was the most integrated part of it and it was used to perform all the possible tasks that a computer performs today thought it lacked interactivity like that of today’s Windows. However, it MS-DOS is still holding its respect even today. Most of the programs are written in DOS. It is not a difficult language to learn but it is very useful. I’ll show you the fundamental or basic concepts of a batch file.


A Batch file is an MS-DOS file extension which runs in the command prompt window and again lacks interactivity. A code is written in normal notepad file and is saved with .bat extension. It is however can be used to alter or play with Windows. By Play, I refer to certain viruses that are created in MS-DOS.

This was enough of brief explanation about DOS and Batch File.

Lets start with the Tutorial.

======================================
To Start Off There Is The @echo off/on
And There Is echo
And pause

So Here Is An Example

@echo off
echo Hello This is a test
pause

Type This In Notepad And Save As Test.bat
Launch It
It Will Look Like This

Hello This is a test
Press any key to continue…

===================================

Again Open Notepad And Write:

@echo on
echo Hello This is a test
pause

And Save It On The Desktop As Test2.bat
Launch It , It Will Look Like This

C:\Documents and Settings\User\Desktop>echo Hello This is a test
Hello This is a test
C:\Documents and Settings\User\Desktop>pause
Press any key to continue

So For The Explanation

@echo off/on:
If You Put @echo on It Will Show The Directory As You Just Saw
If You Put @echo off It Will Just Show The Words With No Directories
so its best to use @echo off echo:
If You Want To Type A Simple Phrase Just Type In echo And Something In This Case echo Hello This is a test
which will show Hello This is a test

pause
Pause Will Pause The Batch File,If You Press A Key The Batch Will Continue In This Case It Will Exit Because There Is No More Left
So Try This

@echo off
echo Hello This is a test
pause
echo I am testing pause
pause

This Will Show

Hello This is a test
Press Any Key To Continue…
(when you press a key it will show)
I am testing a pause
Press Any key to continue..



============================================

So That Convers The Pause Command

Now Some More Commands
There Is Msg *
And There Is cls

Open Notepad And Write:

@echo off
echo Hello This is a test
pause
cls
echo I am testing pause
msg * The End
pause

And Save As Test3.bat

This Will Show

Hello This is a test
Press Any Key To Continue…
(when you press a key it will show)
cls
(the page will clear)
I am testing a pause
(a pop-up will show saying The End)
Press Any key to continue..

=======================================

So .. cls Will Just Clear The Screen
And Msg * Will Bring Up A pop-up :D
Other Commands Are REM And goto And start
So Create A Folder, Name It Test
Put Any Picture Inside And Name It testpic.jpg
Open Notepad And Type:
@echo off
echo Testing start and rem
rem title Test
start testpic.jpg (or any other extension)
pause

And Save It Inside The New Folder As Test4.bat
Launch It , It Will Show:
Testing start and rem
Press Any Key To Continue
(and the picture will launch)
So start Will Launch Any File


And rem Is A Ramark.. Which Wont Show In The Bat File When Running

================================================

Now The goto And set And if

Type This Inside Notepad

@echo off
echo This is a test
echo If you want to do math type 1 and press enter
echo If you want to see a picture type 2 and press enter
set /p option=
if ‘%option%’==’1′ goto :math
if ‘%option%’==’2′ start testpic.jpg (or any extension)

:math
echo 2+2
pause

And Save It Inside The New Folder

This Will Show

This is a test
If you want to do math type 1 and press enter
if you want to see a picture type 2 and press enter
(here you can type 1 or 2 and press enter)
(if you press 2 and Enter the picture will load)
(if you press 1 and enter this will show up):
2+2
Press any key to continue…

=================================================

So goto Will Go To A Label Or A Part Of  The File
When You Put :Math
A New Label Will Be Named Math And When You Put goto :math
The Vatch Will Go To :mathset
The Set Option Will Set Something . Here Its Going To Set :choice
The Set Is , In Some Options Followed By if
So If I Type 1 In This Example
The “choice” Will Be 1 .. And It Will Go To :math Because We Put
if ‘%choice%’==’1′ goto :math
So If We Put 1 The Choice Will Be 1 Therefore It Will [reed]goto :math

Now I Am Going To Talk About Call,Exit,And SHIFT?

====================================================

Go Into Notepad And Type This

@echo off
echo yay me i know the basics of batch
call call.bat
pause
exit

And Save In A New Folder

Then Open Notepad And Type This Again

@echo off
echo I Love You
pause

And Save In The Same Folder That You Saved The Last One And Name It Call.bat

Launch The First One
This Will Show

yay me i know the basics of batch
I Love You
Press Any Key To Continue….

(when you press a key the below will appear)
Press Any Key To Continue…
and when you press a key it will exit

Ok So Now You Know What exit Does It Exits The File And Call ,Calls In Another Batch File!!..
So Now For SHIFT Which Is A Little Hard So Try To Understand

=====================================================

SHIFT [/n]

If Command Extensions are enabled the SHIFT command supports the /n switch which tells the command to start shifting at the nth argument, where n can be between zero and eight.
just like this 1 below

SHIFT /2

would shift %3 to %2, %4 to %3, etc. and leave %0 and %1 . how can i say it

Examples

the below has got to be saved as .bat.. so just name it test

@ECHO OFF
ECHO – %1
SHIFT
ECHO – %1

After creating the above example test.bat file, if you were to type the below command at the MS-DOS prompt, it would print “- ONE” and then “- TWO”; this command is commonly used to work through each of the command extensions or remove command extensions.
(SHIFT , i mean the explanation… i got it from a website and edited it)

Ok I hope you understand !

Now we will learn how to launch windows applications:
Type in notepad:

@echo off
echo I am testing notepad!
start /MIN notepad
pause

This will show

i am testing notepad
(and notepad will start minimized.. if you put /MAX instead of /MIN it will launch maximized)
Press Any Key To Continue

=====================================================

Now lets learn how to put a timeout inside a batch..
goto notepad and type:

@echo off
echo i am trying timeout
set wait=0
:pause1
set /a wait=%wait%+1
if %wait% leq 1000 goto pause1
echo IT WORKED!\
pause
dont understand the command.. just copy paste it.. or memorize it .. if i tell you the explanation it will take alot of time!!..

So this will show :
i am trying timeout
(couple of seconds and.. )
Press Any Key To Continue…
Now ill teach you how to change the TIME
goto notepad and type:
@echo off
echo i wonder what time it is!!
TIME 5:50
echo Oh so its 5:50
pause
this will show:
i wonder what time its is!!
Oh so its 5:50

==================================================

The TIME will just change The TIME:

Now the Color

go into notepad and type this :

@echo off
echo trying the colors
pause
color 04
echo testing
pause
color f
echo trying
pause
color 4f
echo trying
pause

04=background black and font red
f the font will be white
4f = background red and f = white

Here are the list(that can be found by cmd>color help)
0=black
1=blue
2=green
3=aqua
4=red
5=purple
6=yellow
7=white
8=gray
9=light blue
a=light green
b=light aqua
c=light red
d=light purple
e=light yellow
f=bright white

So this will show it

===================================================

Now the title :

type this in notepad

@echo off
tile Test
echo trying the title
pause

this will show the title Test When you launch it!!

lets try Shutdown/Restart

type this in Notepad :

@echo off
START C:\Windows\RUNDLL.EXE user.exe,exitwindowsexec
exit

The above will restart

@echo off
Shut down the computer

C:\Windows\RUNDLL32.EXE user,exitwindows
exit

The Above code will Shutdown your PC.

““““““““““““““““““““““““

That is it !!! I hope you like it , then share it ! :-)

{ 2 comments… read them below or add one }

Phillip June 18, 2011 at 4:54 am

Is it possible to run a batch file from a batch file?
And, i tried your chat room code, and after i do everything (enter the IP address, and type the message), it talks about the correct syntax.

Could you answer these questions?

admin June 23, 2011 at 2:46 pm

YEs, It is possible to run a batch file from a batch file :)

goto control panel
click on administrative tools(switch to classic view)
then click on services
then find messenger service
open it
you’ll see startup type which would be disabled
so click on that and select manual
now press the start button and apply

now do the same on the remote computer
until this service is activated on that pc, you won’t be able to use this

now click on that file you made
type the ip
type the message
and enter

there you go!

Leave a Comment

Previous post:

Next post: