The Ultimate Guide To Batch File Programming PART- 3



The PAUSE Command: Freezing Time

Say you create a batch file which shows the Directory Listing of a particular folder(DIR) before performing some other task. Or sometimes before deleting all files of a folder, you need to give the user time to react and change his mind. PAUSE, the name says it all, it is used to time out actions of a script.Consider the following scenario:

batch-file-programming3

Code:
REM This Batch program deletes *.doc files in the current folder.
REM But it gives the user to react and abort this process.
@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.

Important: Say you have saved a batch file in the c:\name directory. Now when you launch command.com the default directory is c:\windows and in order to execute the batch file program stored in the c:\name directory you need to change the directory and go to c:\name.This can be very irritating and time consuming. It is a good practice to store all your batch programs in the same folder. You can run a batch file stored in any folder(Say c:\name) from anywhere(even c:\windows\history) if you include the folder in which the batch file is stored (c:\name)in the AUTOEXEC.BAT file, so that DOS knows which folder to look for the batch program.
So simply open c:\autoexec.bat in Notepad and append the Path statement to the
following line[c:\name is the folder in which all your batch files are stored.]:

SET PATH=C:\WINDOWS;C:\WINDOWS\COMMAND;C:\name

Autoexec.bat runs each time at startup and DOS knows each time, in which directory to look for the batch files.

ABOUT THE AUTHOR
AbhiShek SinGh
Founder of 'TheHackingArticles'. Cyber Security Analyst, Cyber Security Researcher, and Software Engineer. Follow 'AbhiShek SinGh' on Facebook , Twitter or Google+ or via Email

Subscribe to stay up to date