Batch file dos windows
What are batch files? Batch files are not programs, pre se, they are lists of command line instructions that are batched together in one file. For the most part, you could manually type in the lines of a batch file and get the same results, but batch files make this work easy. Batch files do not contain «compiled» code like C++ so they can be opened, copied and edited. They are usually used for simple routines and low-level machine instruction, but they can be very powerful. If you look in your C:\, C:\WINDOWS, or C:\WINNT folder you will see a multitude of .BAT, .SYS, .CFG, .INF and other types. These are all kinds of batch files. This may shock you, but while most applications are writen in Basic or C++ they sit on a mountain of batch files. Batch files are the backbone of the Windows operating system, delete them and you’ve effectively disabled the OS. There is a reason for this. The system batch files on each computer are unique the that computer and change each time a program is loaded. The operating system must have access to these files and be able to add and delete instructions from them.
Simple instructions
- Open a text editor like notepad(NOT word or wordpad)
- Type or copy this text:
@ECHO OFF
ECHO.
ECHO This is a batch file
ECHO.
PAUSE
CLS
EXIT - Save this as batchfile.bat , make sure there is no .txt extension after the .bat
- Double-click the file icon
This is a little batch file I wrote that I use every day. It deletes the cookies that get dumped to my hard drive every time I go online. I could set my browser preferences not to accept cookies, but sometimes cookies are useful. Some CGI pages are unusable with cookies, sometimes when you enter a password for a Website, the site uses a cookie to remember your password. I just do not need hundreds of cookie files taking up space after I close my browser. With this batch file, all I have to do is double-click it and it deletes my cookies. Feel free to cut and paste this code to your Notepad or Wordpad. Save it as cookiekill.bat on your Desktop.
cls REM ******************************************* REM **Cookie Kill Program Will not work in NT** REM ******************************************* deltree /y c:\windows\cookies\*.* 1\*.* |
What does the batch file do? The first line has the command cls . cls clears the screen window of any previous data. The next three lines start with REM for «remark.» Lines begining with REM do not contain commands, but instructions or messages that will be displayed for the user. The next two lines begin with the command deltree , deltree not only deletes files but directories and sub-directories. In this case the file is deleting the directory cookies and all the files inside. This directory is automatically rebuilt. The deltree has been passed the parameter /y , this informs the process to answer «YES» to any confirmation questions. Sometimes you type the DEL or one of its cousins, the system will ask «Are sure you want to do this?» setting /y answers these prompts without interupting the process. The pause command halts the process temporarily and shows the users a list of all the files being deleted. cls clears the screen again, another REM line tells the user that the files are deleted. The last line contains only :end and returns the process to the command prompt. This version was created to show the user everything that is taking place in the process. The version bellow does the same thing without showing the user any details.
deltree /y c:\windows\cookies\*.*
deltree /y c:\windows\tempor
Without REM lines there are no comments. The @echo off command keeps the process from being «echoed» in the DOS window, and without the pause and :end lines, the process runs and exits without prompting the user. In a process this small it is okay to have it be invisible to the user. With more a complex process, more visual feedback is needed. In computing there is fine line between too much and too little information. When in doubt give the user the oportunity to see what is going on.
This version is a little more thurough, deletes alot of junk
cls @ECHO OFF ECHO. *********************************** ECHO. ** Clean Cookies and Temp Files ** ECHO. ** Will not work in NT ** ECHO. ******************************* deltree /y c:\windows\cookies\*.* deltree /y c:\windows\tempor 1\*.* 1\Netscape\Users\default\Cache\*.jpg 1\Netscape\Users\default\Cache\*.gif 1\Netscape\Users\default\Cache\*.htm 1\Netscape\Users\default\archive\*.htm 1\Netscape\Users\default\archive\*.gif 1\Netscape\Users\default\archive\*.jpg |
«C:\windows\history\today» will rebuld itself if you delete it. It’s not a file, it’s a specially configured directory structure that DOS doesn’t see the same way that windows does. C:\windows\history\today doesn’t actually exist as DOS sees it. Go into the C:\windows\history directory and type DIR/A this will show you the hidden directories and how they are named.
WINNT Version
@ECHO OFF ECHO ************************************************** ECHO ** DEL replaces DELTREE, /Q replaces /Y ** ECHO ************************************************** 1\Cookies\*.* 1 in the above line to your userID |
del /q C:\Windows\Temp\Adware\*.* del /q C:\Windows\Temp\History\*.* del /q C:\Windows\Temp\Tempor 1\*.* |
One thing I do quite often is erase old floppy disks. I might have a stack of them and I don’t care what’s on them, but I want all the files gone including potential virii(everyone says «viruses» but «virii» is the proper term. Snob!). But I get tired of opening a DOS prompt and typing in the command to format the disk. So I wrote a one line batch file that does it for me. Save it as: «disk_wipe.bat»
format a: /u |
Batch File Utilities and Commands
Any valid DOS command may be placed in a batch file, these commands are for setting-up the structure and flow of a batch file.
CLS
Clears the screen
EXIT
Exits the command-line process when the batch file terminates
EXIT |
BREAK
When turned on, batch file will stop if the user presses — when turned off, the script will continue until done.
BREAK=ON |
BREAK=OFF |
CALL
Calls another batch file and then returns control to the first when done.
CALL C:\WINDOWS\NEW_BATCHFILE.BAT |
Call another program
CALL C:\calc.exe |
CHOICE
Allows user input. Default is Y or N.
You may make your own choice with the /C: switch. This batch file displays a menu of three options. Entering 1, 2 or 3 will display a different row of symbols. Take note that the IF ERRORLEVEL statements must be listed in the reverse order of the selection. CHOICE is not recognized in some versions of NT.
@ECHO OFF ECHO 1 — Stars ECHO 2 — Dollar Signs ECHO 3 — Crosses IF errorlevel 3 goto CRS FOR. IN. DO (set of items) is one item or multiple items seperated by commas that determine how many times the loop runs. command or command strcuture is the operation you want to perform for each item in the list. This code will run through the set (A, B, C) , when it gets to B it will print the message: «B is in the set!»
This line will print the contents of C:\windows\desktop
So, you may create your own list or use various objects like files to determine the loop run. Details. GOTO
Skip sections of a batch file
Looping with GOTO
IF, IF EXIST, IF NOT EXIST
Use with «errorlevel» The generic paramater errorlevel refers to the output another program or command and is also used with the CHOICE structure. If you try and run a command in a batch file and produces an error, you can use errorlevel to accept the returned code and take some action. For example, let’s say you have a batch file that deletes some file.
PAUSE This displays the familiar » Press any key to continue. » message. REM
ECHO
ECHO can also be used in batch file to send output to the screen:
To echo special characters, precede them with a caret: ECHO ^ The @ before ECHO OFF suppresses the display of the initial ECHO OFF command. Without the @ at the beginning of a batch file the results of the ECHO OFF command will be displayed. The @ can be placed before any DOS command to suppress the display. SET LASTDRIVE
MSCDEX The AUTOEXEC.BAT fileAUTOEXEC.BAT stands for automatic execution batch file , as in start-up automatically when the computer is turned on. Once a very important part of the operating system, it is being less used and is slowly disapearing from Windows. It is still powerful and useful. In NT versions it is called AUTOEXEC.NT, click here for more information. Before the graphical user interface(GUI, «gooey») of Windows, turning on a PC would display an enegmatic C:\> and not much else. Most computer users used the same programs over-and-over, or only one program at all. DOS had a batch file which set certain system environments on boot-up. Because this was a batch file, it was possible to edit it and add a line to start-up the user’s programs automatically. When the first version of Windows was released users would turn their PCs on, and then type: WIN or WINDOWS at the prompt invoking the Windows interface. The next version of Windows added a line to the AUTOEXEC to start Windows right away. Exiting from Windows, brought one to the DOS prompt. This automatic invocation of Windows made a lot of people mad. Anyone who knew how to edit batch files would remove that line from the AUTOEXEC to keep Windows from controling the Computer. Most users do not even know that DOS is there now and have never seen it as Windows hides the any scrolling DOS script with their fluffy-cloud screen. At work I will often have to troubleshoot a PC by openning a DOS shell, the user’s often panic, believing that I have broken their machine because the screen «turns black». Most current versions of Windows have a folder called «Start-up.» Any program or shortcut to a program placed in this folder will start automatically when the computer is turned on. This is much easier for most users to handle than editing batch files. Old versions of DOS had a AUTOEXEC that looked like this:
All this really did way set the DOS prompt to «>» Later versions looked like this:
An AUTOEXEC.BAT from a Windows 3.11 Machine
This version simply sets DOS to boot to Windows.
Whenever a program is installed on a computer, the setup program or wizard will often edit the AUTOEXEC. Many developer studios will have to «set a path» so programs can be compiled or run from any folder. This AUTOEXEC is an example of that:
This AUTOEXEC sets the path for COBOL and JAVA development BINs. This way, the computer knows where to look for associated files for COBOL and JAVA files if they are not located directly in a BIN folder. Sets all the devices and boots to Windows.
For loading Windows from a CD
Removing the «REM» tags uncomments the commands and runs them.
NT does not use AUTOEXEC.BAT, the file is called AUTOEXEC.NT and should be found in the C:\WINNT\system32 folder. Here is a sample AUTOEXEC.NT file:
*.NT and *.CMDIn Windows systems config.sys is used to set the initial values of the environment variables. To see your current settings, type SET on a command line. In early versions config.sys is a text file you can edit. In later versions it is a complied file that cannot be changed in a text editor. In newer NT versions it is not used at all. Try msconfig.exe instead.
INI , *.ini — Initalization file. These set the default variables for the system and programs. More CFG , *.cfg — Configuration files. SYS , *.sys — System files, can sometimes be edited, mostly compiled machine code in new versions. More. COM , *.com — Command files. These are the executable files for all the DOS commands. In early versions there was a seperate file for each command. Now, most are inside COMMAND.COM. NT , *.nt — Batch files used by NT operating systems. More. CDM , *.cmd — Batch files used in NT operating systems. More. You may put and use command-line parameters into your batch-files. Suppose you had a batchfile called «test.bat» and these were the contents:
dont tell me what to do because it is set up to handle 6 parameters and there are six words. You can tease someone by changing the order:
Making your own variables
You can make up your own parameter names and have many of them:
|