Wednesday 18 May 2011

Batch Programing


In DOS, OS/2, and Microsoft Windows, a batch file is a text file containing a series of commands intended to be executed by the command interpreter. Similar to job control language and other systems on mainframe and minicomputer systems, batch files were added to ease the work required for certain regular tasks by allowing the user to set up a batch script to automate many commands. When a batch file is run, the shell program (usually COMMAND.COM or cmd.exe) reads the file and executes its commands, normally line-by-line. Batch files are useful for running a sequence of executables automatically and are often used to automate repetitive or tedious processes.[1] Unix-like operating systems (such as Linux) have a similar type of file called a shell script.

In simple word batch file is a sequence of command to be executed in ‘cmd.exe’.


Now that is enough for theory part,
Let`s try some practical example.

For example to create a batch file to delete all unnecessary *.tmp file from window/tmp
Step1:- first open any text editor (notepad in window)
Step2:- Write sequence of command in notepad as following
ECHO This Batch File deletes all unwanted Temporary files from your system
ECHO Now we go to the Windows\temp directory.
cd windows\temp
ECHO Deleting unwanted temporary files....
del *.tmp
ECHO Your System is Now Clean
Step3:- Save file as test.bat
Congratulation your first batch file is ready, now let`s see what will happen when you will execute it, to execute just double click the file. Output will be like this
C:\WINDOWS>test
C:\WINDOWS>ECHO This Batch File deletes all unwanted Temporary files from your system
This Batch File deletes all unwanted Temporary files from your system
C:\WINDOWS>ECHO Now we go to the Windows\temp directory.
Now we go to the Windows\temp directory.
C:\WINDOWS>cd windows\temp
Invalid directory
C:\WINDOWS>ECHO Deleting unwanted temporary files
Deleting unwanted temporary files...
C:\WINDOWS>del *.tmp
C:\WINDOWS>ECHO Your System is Now Clean
Your System is Now Clean

The above is a big mess! The problem is that DOS is displaying the executed command and also the statement within the ECHO command. To prevent DOS from displaying the command being executed, simply precede the batch file with the following command at the beginning of the file:
ECHO OFF
Once we add the above line to our Temporary files deleting Batch program , the
output becomes:
C:\WINDOWS>ECHO OFF
This Batch File deletes all unwanted Temporary files from your system
Now we go to the Windows\temp directory.
Invalid directory
Deleting unwanted temporary files...
Your System is Now Clean
Hey pretty good! But it still shows the initial ECHO OFF command. Well we can use @ECHO OFF to prevent that.
Now let`s see some funny batch file (Warning:- Don`t try on your computer)
Try this one
@ECHO OFF
ECHO WARNING: Going to delete all Microsoft Word Document
ECHO Press CTRL+C to abort or simply press a key to continue.
PAUSE
DEL *.doc
Now when you execute this batch program, we get the following output:
C:\WINDOWS>a.bat
WARNING: Going to delete all Microsoft Word Document
Press CTRL+C to abort or simply press a key to continue.
Press any key to continue . . .
The batch file program actually asks the user if he wishes to continue and gives the user the option to abort the process. Pressing CTRL+C cancels the batch file
program(CTRL+C and CTRL+Break bring about the same results)
^C
Terminate batch job (Y/N)?y
After this you will get the DOS prompt back.
Or try this one
                Shutdown –s
This will shut down your computer in 1min.

Try this one this is really useful


This is a Folder Locker, where xxxx is your Password you can change it as u want.


cls
@ECHO OFF
title Folder Locker
if EXIST "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}" goto UNLOCK
if NOT EXIST Locker goto MDLOCKER
:CONFIRM
echo Enter password to lock folder or for cancel press N
set/p "cho=>"
if %cho%==XXXX goto LOCK
if %cho%==n goto END
if %cho%==N goto END
echo Invalid choice.
goto CONFIRM
:LOCK
ren Locker "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
attrib +h +s "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
echo Folder locked
goto End
:UNLOCK
echo Enter password to Unlock folder
set/p "pass=>"
if NOT %pass%==XXXX goto FAIL
attrib -h -s "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
ren "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}" Locker
echo Folder Unlocked successfully
goto End
:FAIL
echo Invalid password
goto end
:MDLOCKER
md Locker
echo Locker created successfully
goto End
:End




You can convert .bat file to .exe file using 'Quick Batch File Compiler'(QBFC)
well this is it for batch programming if you like it then follow me if you have any quarry then feel free to ask......
For more information on batch programming you can post quarry

1 comment:

Anonymous said...

nice one...dude..!