| Topics | |||
|---|---|---|---|
| Forma.bat | Formatting floppies | Batch files for use with the Registry: | |
| Dback.bat | Copying Data Files to Backup | Reg.bat | 1. Backup, Compacting & Restoring |
| Dirp.bat | Listing Directory contents | Regbak.bat | 2. Backup & Restoring only |
| Bootlog.bat | Logging Bootups | ||
| Call | Using other files to repeat commands | Main topic Index | |
|
~ Topics listed in Green are essential, I believe. ~
~ Those in Blue are less so, but are still useful. ~ Please start at the beginning, to understand how Batch files work. |
|||
| What Are Batch Files? |
|---|
| Batch files ( *.bat ) are DOS files containing one or more commands to be run in DOS mode, rather like macros. Running a batch file simply directs its pre-written commands to be obeyed, just as if they had been typed individually at the DOS Prompt. Before Win9x they were used extensively to run 'batches' of commands, and could be very lengthy. Holding numerous commands (especially strings of switches) in one file saves time and typing errors! They still have a place within Win9x systems, depending upon what is required by the user. Some program upgrade patches are supplied with batch files to apply the fixes. NB: If you create and keep Batch files in C:\ or C:\BATS\ add them to the PATH statement in C:\AUTOEXEC.BAT. Edit the line as appropriate to read:PATH C:\WINDOWS;C:\WINDOWS\COMMAND;C: orPATH C:\WINDOWS;C:\WINDOWS\COMMAND;C:;C:\BATS (directories separated by semi-colons)Now let's create a some Batch files. Most are simple, two slightly more complex. In the examples the batch files are written in lower case except where display text is included. This is the conventional method, which you don't have to follow. It tends to be easier to read - and troubleshoot! - in longer ones. The explanations use capital letters for clarity. |
| top of Page Main topic Index |
| Forma.bat |
|---|
This simple batch file consists of just a command line. It formats a floppy and gives it a standard name, and is suitable for using DOS to clear a number of floppies. There is a useful purpose for it - formatting floppies under DOS reduces the risk of a virus infection.
|
| top of Page Main topic Index |
| Dirp.bat |
|---|
This file is a bit more decorous and will give you a Directory listing of all the files in the current directory, in name order, excluding sub-directories, a page at a time. For details of the command see Dir. As with "Forma.bat" above, the aim of the file is to save typing the rest of the switches each time. We'll call the file Dirp.bat and again save it in the root directory, so it will always be available.
|
| Top of Page Main Topic Index |
A Batch File for Backing Up Data
| Dback.bat |
|---|
This next file is a 'template' for you to adapt to your setup. It uses Xcopy in a DOS Box to perform a simple backup, copying all the files in three directories holding Data from one drive ( C: ) to another ( D: ). If you don't have a backup utility like WinZip or a Zip Drive you need something better than laboriously using Explorer to copy files to floppies. And if you don't have a D: drive (as a partition or a separate Hard Disk) either, then do something about it now! A partition is cheaper than a second Hard Disk or a Zip drive.
XCOPY lines and paste them at the very end, then put rem at the beginning of those lines. Then remove the /U from the original lines and save the file. After running it the first time, you can edit it again - the 'remmed-out' lines will be there to remind you of the switch. REM is useful for trying variations of a command.The general format and batch file commands used are as explained under the other example files. This one adds more text to explain what it is happening, but essentially it just runs the three XCOPY lines.This file is for use in a DOS Box, as the switches used are applicable to XCOPY32 which is automatically used under Windows. If you would prefer to use it in DOS Mode, amend the switches as appropriate.
|
| Top of Page Main Topic Index |
A Batch File for Logging Bootups
| Bootlog.bat |
|---|
This quite simple batch file causes a date and time entry to be added to the end of a file called C:\Boot.txt each time the machine is started or rebooted.
|
| Top of Page Main Topic Index |
Registry Backup, Restoring, Exporting & Compacting
| Reg.bat |
|---|
| This file is designed to work with the Guide to Compacting the Registry, to make the typing of commands at the DOS Prompt less time consuming and error-prone. In fact, with this file you don't type anything except the choice of action offered by the Menu system, which you will find very simple and easy to adapt to any other batch file you decide to use. Please read the Guide before using the Compact Registry option included. We're going to use Notepad to create the file. Please do not use Wordpad or any other word processor. Word processors add formatting and all we want is plain text, where the only non-character key is the carriage-return (ie Enter) to end one line and begin another. I suggest you copy and paste the text rather than try typing it line by line - hence Notepad. A DOS Box, which might seem a logical choice, does not work because the DOS Editor copies and pastes using its own 'buffer' rather than the Windows Clipboard. Look at the file in detail to see what is happening once it has been saved.
|
|
The commands included are explained in Essential Commands. With regard to the batch file itself: Explanation:
|
| Top of Page Main Topic Index |
| Regbak.bat |
|---|
This batch file is designed to Backup or Restore Registry, and save you the chore of typing the commands. If you compare it with Reg.bat above, you'll see it is a cut-down version. Like Reg.bat, it should only be run in DOS Mode - not in a DOS Box - and like Reg.bat it retains backups. The commands in both vary slightly therefore from those given in Patrick's Guide to Compacting the Registry, which is recommended reading before you use this.
|
| Top of Page Main Topic Index |
Using Several Batch Files for Repeated Commands
| Using Call |
|---|
| As you can see above, one file can contain numerous commands invoked as required via the 'Choice' menu creation facility. However, they can get very long and complicated. Where a batch file includes either one command you frequently use or several commands (which may either just run one after another or be selectable via a menu) there is no point repeating them again in another file. The second can simply 'call' the first, and carry on after it has finished. Suppose you regularly restart your computer in DOS-mode to back up your Registry after each session. You could semi-automate this by adding at the top of your C:\Windows\Dosstart.bat file, eg:
@ECHO OFF
CALL REG.BAT
C:\MOUSE\MOUSE.COM
C:\WINDOWS\COMMAND\MSCDEX.EXE /D:CDROM1 /L:F
The effect of this would be to run Reg.bat (as above). This would give you total control, as you could Quit without backing up if you wished to, or decide (if the Windows session you just ended seemed flaky) to restore the last Registry backup immediately and try Windows again. The fully automated alternative - putting the command lines to backup instead - would destroy the option by making backups of possibly corrupt files. Once Dosstart.bat has done its job of calling another file, it will continue to process any commands below the 'CALL' line, in this case loading the DOS Mouse driver and enabling access to the CD-ROM drive, then terminate itself as normal.A 'Call' can be placed anywhere within a batch file, the advantage being that it keeps the main file running. Say you have another batch file and want it to use the Registry facilities in Reg.bat and then do some other tasks. If you were to put in simply:
REG.BAT
it would launch the Reg batch file but stop running itself.Bootlog.bat:
|
| Top of Page Main Topic Index |
< Home >