Doctor |
DOS |
¦ Batch
File Basics ¦ |
||
¦ Batch File Examples ¦ | ¦ Advanced Batch Files II ¦ | ¦ Batch File Tips ¦ |
¦ Obtain 500+ Batch Files ¦ |
|
An advisory to non-Canadians: Some characters
shown in some batch
files here may not be able to be reproduced on your
system unless
the Country Code is changed or you type them in as ASCII
characters.
Consult your text editor/word processor manual to see how to do
the latter.
|
:: CDS.BAT :: Changes Directory to One on the Same or Higher Adjacent Level :: Displays the Results :: @ECHO OFF IF "%1" == "1" GOTO LEVEL-1 IF "%1" == "2" GOTO LEVEL-2 IF "%1" == "3" GOTO LEVEL-3 IF NOT "%1" == "1" GOTO LEVEL-1A :LEVEL-1 CD ..\%2 GOTO END :LEVEL-2 CD ..\..\%2 GOTO END :LEVEL-3 CD ..\..\..\%2 GOTO END :LEVEL-1A CD ..\%1 :END C:\BATCH\DR
The file first determines if you used a number at the command line and what that number might be. The results branch to one of the LEVEL labels where the appropriate CD (Change Directory) command is issued. Each " ..\ " represents the next-level-up (parent) directory. Subsequent additions represent the grandparent, great grandparent, and so on, directories. Try it yourself at the command line from any sub-directory. "CD ..\" will take you up one level.
This file allows one to change to a directory adjacent to the current one. That is, if the directory one level up is PARENT1 and you are in CHILD1, by issuing "CDS CHILD2" or "CDS 1 CHILD2", you will be whisked sideways into that directory and a list of files will appear. Note that this batch file allows one to give no number or a number `1'. Either will take you to an adjacent sub-directory of the directory one level up.
You may also go to an adjacent directory two or three levels higher by including a number `2' or `3' on the command line. So, let's say you are in CHILD1 and the next level up is PARENT 1. Adjacent to it is PARENT2 and above both PARENT directories is GRANDMOM. To get to CHILD1 under PARENT2, issue "CDS 2 CHILD1". You may do the same thing with the directory above GRANDMOM by using the number `3'.
Don't forget the space on either side of any number you use at the command line with this batch file. If you have a good visual idea of your directory tree, you may add lines to the batch file for more levels if you wish.
For those of you thinking about having the batch file
displaying the directory structure, I tried that but found it to be confusing if
there were a lot of directories to display. If you wish to try it, add the
following lines after each "LEVEL" label:
TREE .. |
MORE
PAUSE
After "TREE", enter the same number of
dots and backslashes as are after the succeeding "CD" line. When the file runs,
a directory tree will be displayed for the number of levels you indicate on the
command line (1 -3). The batch file will then pause to allow you to view the
last screen of TREE. Pressing any key will then take you to the directory you
typed on the command line. If you typed the wrong one, simply issue "CONTROL-C"
to stop the batch file. Then run it again with the proper directory name.
I also found that having the tree display to be annoying everytime I wanted to use this batch file. A better method would be to have the batch file display the directories only when you needed, and then allow you to enter the directory to which you wish to go. Perhaps I'll write that version and add it here in the future.
________DATEFILE.bat
:: DATEFILE.bat :: Makes a Text File with the Current Date as the Name :: C:\BATCH\DOS\XSET CUR-DATE DATE YY-MM-DD See Text for REM > %CUR-DATE%.TXT XSET Information
This will make a zero-byte file which name will be in the format of "Year-Month-Day". That keeps similar files in chronological order. You may then load it into an editor and add text. Here's a version which does both:
:: DATEFILE.bat (Improved) :: Makes a Text File with the Current Date as the Name :: Loads it into DOS EDIT :: C:\BATCH\DOS\XSET CUR-DATE DATE YY-MM-DD C:\BATCH\DOS\EDIT %CUR-DATE%.TXT
This assumes you are using DOS EDIT and it is in the "DOS" directory. XSET is an update to the DOS "SET" command. It is assumed here that it too, is in the "DOS" directory. Get that program at the XSET website.
Want to do the same thing with a directory name?
:: DATE-DIR.bat :: Makes a Directory with the Current Date as the Name :: C:\BATCH\DOS\XSET CUR-DATE DATE YY-MM-DD MD %CUR-DATE% ________DBF.bat
This allows one to get a directory listing of a floppy drive and then prompts to format the disc or not. This is handy if you need a blank disc but want to check first to see if there are any files on it you might want.
It will be assumed that the `B' drive is to be used and that the floppy drive and blank disc size is 1.44 megabytes. Change these if yours differ.
:: DBF.bat :: Shows Directory of `B' Drive :: Prompts to Do a Full or Quick Format or Not :: If Quick Format is Chosen, Does Not Save Unformat Information :: @ECHO OFF CLS CALL C:\BATCH\DR B:\ ECHO. ECHO. ECHO Full Format B Drive? ECHO. ECHO Press `F' to Format ECHO. ECHO. ECHO Quick Format B Drive? ECHO. ECHO Press `Q' to Quick Format ECHO. ECHO. ECHO Press "Escape" to Cancel ECHO. C:\DOS\CHOICE /C:FQ^[ /N > NUL IF ERRORLEVEL 3 GOTO END IF ERRORLEVEL 2 FORMAT B: /Q /U IF ERRORLEVEL 1 FORMAT B: :END
(Note that `^[' represents the "Escape" character. To create it, press ^P (Control-P) in DOS Edit, then the "Escape" key. Other text editors allow one to create the "Escape" character by pressing ^V first.) |
________
After the header information, the batch file clears the screen and displays a directory listing of the `B' drive so you see what, if any, files are on the drive. You may substitute the DOS "DIR" command here with your preferences. I suggest an ordered list by name (alphabetical), shown one screen at a time, so the command would look like "DIR /ON /P".
Next, some choices are placed on the screen with blank lines in between for readability purposes. They are also indented from the left margin to make then stand out on the DOS screen. After that is the CHOICE command. The choices (F, Q, and Escape) are listed after the "/C" switch. The "/N" switch hides CHOICE's display of these letters and the "> NUL" directs any additional prompts to be hidden.
After this are ERRORLEVEL statements. These determine which key is pressed by the user and then it acts accordingly. I won't get into how these work because it is beyond the scope of this batch file example page. You can see that each corresponds to one of the possible keys and that each has a command after it. Thus, pressing "ESCAPE" tells the batch file to go to the end, which means the disc is not formatted. The other two ERROELEVEL statements direct the disc to be fully formatted or quick formatted.
________DELE.bat (Improved)
This is an improved version of the DELE.bat which was presented in the basic Batch File Examples. It allows multiple files of differing names to be specified for exclusion from deletion. All else in the current directory will be deleted except for files with certain attributes.
:: DELE.bat :: Deletes Current Directory Entries Except for Specified Files :: Wildcards (* and ?) may be Used in File Names :: (Hidden, System, and Read-Only Files are Not Affected) :: @ECHO OFF IF "%1" == "" GOTO END Stops the Batch File if no File Names are Given. MD SAVE Makes a Temporary "SAVE" Directory. :MOVE-FILES SHIFT Uses the DOS "SHIFT" command to Move IF "%0" == "" GOTO FILES-DELETE each File Name Specified at the XCOPY %0 SAVE > NUL Command Line to the "0" Parameter GOTO MOVE-FILES until none are left. Copies each Specified File in turn to the "SAVE" Directory. :FILES-DELETE ECHO Y | DEL . > NUL Deletes all Files in the Current Directory, showing no Prompts. MOVE SAVE\*.* . > NUL Returns Excepted File(s) to the Current Directory. RD SAVE Removes "SAVE" Directory and DR Displays the Results of the Operation. :END
You may improve the execution speed of this batch file by specifying that the SAVE directory be created on a RAM drive. See your DOS manual about setting up a RAM drive. Some information may be read at this site under DOS Tips.
________EAE.bat
:: EAE.bat :: Edits "Autoexec.bat" :: Prompts to run Autoexec or :: Return to the DOS Command Line :: @ECHO OFF C: EDIT C:\AUTOEXEC.bat Loads AE into DOS' "EDIT" ECHO. ECHO Run "Autoexec.Bat" ? Press: `R'. ECHO. Places choices on to ECHO To Cancel, Press: `Escape' the screen with spacing. ECHO. CHOICE /C:R^[ /N > NUL DOS 6's "Choice" command. IF ERRORLEVEL 2 GOTO END Determines which key has IF ERRORLEVEL 1 GOTO RUN been pressed and decides GOTO END the course of action. :RUN C:\AUTOEXEC.BAT :END
(Note that `^[' represents the "Escape" character. To create it, press ^P (Control-P) in DOS Edit, then the "Escape" key. Other text editors allow one to create the "Escape" character by pressing ^V first.) |
________ECS.bat
:: ECS.bat :: Edits the Config.Sys File :: Prompts to Reboot or Not :: @ECHO OFF EDIT C:\CONFIG.SYS Loads CS into DOS' "EDIT" ECHO. ECHO Reboot Computer? Press: `R'. ECHO. Places choices on to ECHO To Cancel, Press: `Escape' the screen with spacing. ECHO. CHOICE /C:R^[ /N > NUL DOS 6's "Choice" command. IF ERRORLEVEL 2 GOTO END Determines which key has IF ERRORLEVEL 1 GOTO REBOOT been pressed and decides GOTO END the course of action. :REBOOT C:\UTIL\WARMBOOT.COM See below. :END
(Note that `^[' represents the "Escape" character. To create it, press ^P (Control-P) in DOS Edit, then the "Escape" key. Other text editors allow one to create the "Escape" character by pressing ^V first.) |
________
This is a debug program which I keep in a UTIL (Utility) directory. You will have to change the path in the above batch file, if you decide to store it elsewhere. Download WarmBoot.com here.
Note, that the Reboot option should not be used if you are running any other program. Thus, be sure Windows, DOS SHELL, or any task switching program is shut down, and that you are not shelled out of any other application, before selecting the boot option of this batch file.
Make Note Files based on the
Current Date
or Under a Given File Name
Sometimes you wish to be able to remember something but don't want to have to fire up a word processor or text editor to type it up. This simple batch file allows one to quickly type some items right at the command line which will automatically be placed into a file with a name you specify; or for really fast note-taking, this batch file can automatically name the note file with today's date.
:: NOTE.bat :: Creates a Note File for the Current Date or Given Name :: Able to Append Text to Same :: @ECHO OFF IF "%1" == "" GOTO TODAY :NAME COPY CON TEMP.NOT TYPE TEMP.NOT >> C:\NOTES\%1.NOT GOTO END :TODAY COPY CON TEMP.NOT C:\DOS\XSET CUR-DATE DATE YY-MM-DD TYPE TEMP.NOT >> C:\NOTES\%CUR-DATE%.NOT :END DEL TEMP.NOT SET CUR-DATE=
The batch file first determines if you typed a file name at the command line. If you have not, it branches to the "TODAY" label. If you have, this "IF" statement will be false and the next section of the batch file will be executed.
That section starts off by using the DOS "COPY CON" feature. It will copy whatever you type at the console (keyboard) and save it to a file. In this case, that file is named "TEMP.not" for "Temporary Note". (The usage of "COPY CON" will be explained further on.)
Once you save the TEMP.not file, its information is sent to the permanent note file which will be "%1.not". "%1" is a replaceable parameter, so it will be substituted with what ever file name you typed at the command line. Thus, if you entered "NOTE PROJECT", the "%1" would become "PROJECT", so your final (permanent) note file would be called "PROJECT.not". This may be done from anywhere within DOS, and when finished, you will remain in the directory from which you issued NOTE.bat. Thus, you may quickly interrupt what you are doing and then return immediately to the current task.
Notice the use of the DOS append redirector ( >> ). This will create a file of the given name if one does not exist, but if it does, the information will be appended (added) to the file. So when you first type a note with a new file name, the file will be created, with the ".not" extension added for you by the batch file. However, if you type more notes at a later time under the same name, those new notes will be added to the old file. In either case, the TEMP.not file will be copied to the appropriate permanent note file and this second file will be placed into the NOTES directory. The batch file then goes to the end where upon the "TEMP.not" file is deleted.
Now, if you don't give a name at the command line, the opening "IF" statement will be true and the batch file will move to the "TODAY" section. Again, a file called "TEMP.not" will be created and will be copied or appended to a permanent note file in the NOTES directory. However, the file will automatically be named based on today's date.
This is done by using an update to the DOS "SET" command called "XSET". It places today's date into an environment variable called "CUR-DATE". This is then used to name the permanent note file, which is again placed into the NOTES directory. The format will be YEAR - MONTH - DAY so that files named with it will be in chronological order in the directory. As before, if this file does not exist, it is created; otherwise any additional notes you type on the given day are added to the existing file. If you type notes on the succeeding days, a new file is created each day with that day's date.
Again, the batch file then goes to the end where the "TEMP.not" file is deleted. Plus, the CUR-DATE variable is removed from memory by resetting it to nothing. An XSET link will be given further on.
Some of you may be wondering why the TEMP.not file is bothered with instead of just creating or appending the actual file name directly. That could be arranged, but using a temporary file is a safety in case one was in the NOTES directory and decided to type in a note. If an older version of DOS were being used, after exiting the COPY CON part of the batch file, any existing file would be overwritten and additional notes would *replace* the previous and not be added to them. Newer versions of COPY CON prompt for an overwrite, but that is just an extra step for the user. Also, if one wished to append to the file, that is not available from COPY CON, so that possibility would additionally have to be written into the batch file. The method shown here eliminates these COPY CON problems and keeps the batch file short.
To use the file, first create the "C:\NOTES" directory. Then whenever a "notes" urge strikes, simply type "NOTE" at the command line to place text in a file with the current date in the name. Alternatively, if you have a specific project in mind, then type a space and a name after the "NOTE" command. (Do not add a file extension - the batch file does that for you, and do not use the name "TEMP" because the batch file uses that name and then deletes it afterwards.) Any subsequent notes will be placed in one or the other file. The next day, of course, a new note file will be created if you enter one based on date.
A hint to improve the note file layout is to add an opening blank line to any appended notes. This will give you a separator when you peruse the file after several appends, making it easier to read. It also makes it easier to discern which notes were in one session and those which are in another. Unfortunately, you have to remember to do this, so it's something extra about which to think. (The improved version further on, will do this for you, plus more.)
When you type "NOTE" with or without a file name, the cursor will drop to a new line signalling that you are using COPY CON. Type in your text, pressing ENTER when you wish to start a new line. You may type long lines, but these lines are limited to 127 characters. If your screen setting is less than that, the line will automatically wrap to the next and beep when 127 character spaces are filled. Press ENTER to start a new line.
However, I recommend against lines which exceed the screen width because they might not wrap in some file viewers or text editors/word processors. This means the line will trail off the screen to the right and scrolling will be required. Pressing ENTER at the end of each line during composition of the original file will prevent this.
When you are finished, press "Control-Z", or on some systems, you may press "F6" to get a ^Z character. This character signals the end of the file. Press ENTER once more and the file will be saved and copied to the NOTES directory. If you have hopelessly messed up what you are typing at the command line or simply change your mind about the note file, then at any time before saving the COPY CON file, you may press "Control-C" to stop. That one command will terminate COPY CON and also display a "Terminate batch job?" prompt. Enter `Y' to stop immediately. No file will be saved. In fact, if you enter `N', the batch file will still terminate in this case. You may then try again, if you wish, by re-entering the batch file name with or without a file name.
Realise that using "Control-C" means a TEMP.not file will be created and not erased. However, after the next un-terminated NOTE.bat, it will be overwritten, and then deleted when the permanent note file is created.
Be aware that in COPY CON, you can only edit the line on which you are currently, and even at that, only by backspacing away the characters. Once you press ENTER and drop to the next line, the previous line becomes inaccessible. If you wish to edit any of these files later, you will have to load the file into a text editor.
The nice thing about this simple method is that since all notes are in a NOTES directory, you may peruse them all at a glance to see what projects or dates have notes attached. You may use the DOS "TYPE" command to look at the contents, or any file viewer may be used because these files are all in plain ASCII text.
Here is the link to get XSET.
NOTE.bat
(Improved)
So we now have a batch file which can quickly copy notes from the command line. However, the data in the file will be all crammed together and have no title. Therefore, this improved batch file will add a title and spacing. Also, if one were to append to these files regularly and keep them for any length of time, the question might arise as to exactly when the notes were taken. So, in addition, log dates & times for both the "NAME" and "TODAY" versions will be placed before each entry.
:: NOTE.bat (Improved) :: Creates a Note File for the Current Date or a Given Name :: Codes a Time into Current-Date Files :: Codes a Date & Time into Named Files :: @ECHO OFF IF "%1" == "" GOTO TODAY :: Name Section :: ############ COPY CON TEMP.NOT C:\DOS\XSET CUR-DATE DATE C:\DOS\XSET CUR-TIME TIME IF NOT EXIST C:\NOTES\%1.NOT GOTO TITLE IF EXIST C:\NOTES\%1.NOT GOTO BODY :TITLE ECHO %1 Note File > C:\NOTES\%1.NOT :BODY ECHO. >> C:\NOTES\%1.NOT ECHO DATE: %CUR-DATE% >> C:\NOTES\%1.NOT ECHO TIME: %CUR-TIME% >> C:\NOTES\%1.NOT ECHO. >> C:\NOTES\%1.NOT TYPE TEMP.NOT >> C:\NOTES\%1.NOT ECHO -------- >> C:\NOTES\%1.NOT ECHO. >> C:\NOTES\%1.NOT GOTO END :: Current Date Section :: ############ :TODAY COPY CON TEMP.NOT C:\DOS\XSET CUR-DATE DATE YY-MM-DD C:\DOS\XSET CUR-TIME TIME IF NOT EXIST C:\NOTES\%CUR-DATE%.NOT GOTO TITLE2 IF EXIST C:\NOTES\%CUR-DATE%.NOT GOTO BODY2 :TITLE2 ECHO %CUR-DATE% Note File > C:\NOTES\%CUR-DATE%.NOT :BODY2 ECHO. >> C:\NOTES\%CUR-DATE%.NOT ECHO DATE: %CUR-DATE% >> C:\NOTES\%CUR-DATE%.NOT ECHO TIME: %CUR-TIME% >> C:\NOTES\%CUR-DATE%.NOT ECHO. >> C:\NOTES\%CUR-DATE%.NOT TYPE TEMP.NOT >> C:\NOTES\%CUR-DATE%.NOT ECHO -------- >> C:\NOTES\%CUR-DATE%.NOT ECHO. >> C:\NOTES\%CUR-DATE%.NOT GOTO END :END DEL TEMP.NOT SET CUR-DATE= SET CUR-TIME=
This begins as before with the file branching to the appropriate section based on whether or not you have given a file name at the command line. For the "NAME" section in this improved version of NOTE.bat, COPY CON is issued and you type the notes to create "TEMP.not", just as in the previous version. After you save & exit, XSET is used to place the current date and time into separate variables.
Next, two "IF" statements decide whether the permanent note file has been created. If not, the batch file places a title at the start of the new file. As in the previous version of this batch file, the file name you type at the command line is substituted for "%1" and it becomes the name of the new file. The batch file adds a ".not" file-name extension. After this, the batch file continues with the body of the file. Earlier, if it was found the file already existed because you had previously typed notes, the batch file would have jumped directly to the BODY section.
In the BODY section, twin redirectors ( >> ) are used to send the next information to the permanent note file. (Remember that the double redirector appends, not overwrites.) First, a blank line is sent. This separates the title or previous entries from what follows. Next, the current date & time are added; another blank line is added, and then the temporary note file is copied to the new file. Finally, dashed & blank lines are placed into the permanent file to separate it from any notes which might be added later.
The batch file finishes by going to the end where the temporary note file is deleted and the environment variables are removed from memory by setting them to nothing.
Now, if no name is given at the command line, the file goes to the "TODAY" section. Here, a file name is made from the current date and is either created or appended to in the same manner as in the NAME section.
As with the first version of this batch file, first create the "C:\NOTES" directory. Then simply type "NOTE" at the command line to place text into a file with the current date in the name and with each note section prefaced by a time. Alternatively, typing a space and a name after the "NOTE" command will create the same type of file but under the name you give.
See REMIND.bat in Advanced Batch Files
II
for a way to place notes into the command prompt.
A NOTE.bat Addendum
We have a way to copy notes from the command line. It would now be nice to be able to review the notes and to delete ones no longer needed. Here is an extra batch file to do just that:
:: DDNOTE.bat :: Displays and Prompts for Deletion of Note Files :: @ECHO OFF IF "%1" == "TASKS" GOTO FUNCTIONS FOR %%F IN (C:\NOTES\*.NOT) DO CALL C:\BATCH\DDNOTE.BAT TASKS %%F GOTO END :FUNCTIONS CLS MORE < %2 DEL %2 /P :END
Ignore the opening "IF" statement for a moment and look at the "FOR-IN-DO" line. It translates to: "For each file in the NOTES directory, run the DDNOTE batch file with a "TASKS" parameter and then the .not file name". So for each .not file, DDNOTE.bat, is rerun. Now, that first "IF" statement will be true because the first parameter has become "TASKS". Thus, the batch file will now be directed to the "FUNCTIONS" section.
In the FUNCTIONS section, the screen is cleared and the MORE command is directed to take input from each file passed to it by FOR-IN-DO. It displays the file a screen at a time. Next, the DEL (DELETE) command shows the file name and prompts to delete or not. You may press `Y' to delete, or `N' to keep the file. After this, control returns to FOR-IN-DO, which passes the next file in the NOTES directory back to DDNOTE.bat with a "TASKS" parameter and the process repeats until all files in the directory have been displayed.
When you wish to review your notes, from anywhere in DOS issue "DDNOTE". Each note will appear on the screen. Afterwards, the file name will be given and a prompt to delete or not delete will show. After choosing `Y' or `N', another file will be displayed, and so on, until all files have been gone through. To cancel the batch file at any time, press "Control-C" and answer `Y' to the "Terminate batch job?" prompt. When finished, you will remain in the directory from which you issued the batch file so you may continue with any previous work.
So now, what if you have a zillion notes and just want to peruse one of them instead of cycling through the lot? Here's a version to do that:
:: DDNOTE.bat (Alternate Version) :: Displays and Prompts for Deletion of All or a Specified Note File :: @ECHO OFF IF "%1" == "TASK-ALL" GOTO ALL-FUNCTIONS IF "%1" == "" GOTO ALL IF NOT "%1" == "" GOTO SPECIFIC :ALL FOR %%F IN (C:\NOTES\*.NOT) DO CALL C:\BATCH\DDNOT.BAT TASK-ALL %%F GOTO END :ALL-FUNCTIONS CLS MORE < %2 DEL %2 /P :SPECIFIC IF NOT EXIST C:\NOTES\%1.NOT GOTO NO-FILE CLS MORE < C:\NOTES\%1.NOT DEL C:\NOTES\%1.NOT /P GOTO END :NO-FILE ECHO. ECHO File Not Found! ECHO. :END
Again, ignore the opening "IF" statement. The second and third "IF" statements determine if you entered a file name at the command line. If not, the file works as does the previous version. However,if you have given a file name, control is transfered to the"SPECIFIC" part. There, it's checked to see if the file name you gave exists. If not, a prompt is shown and the batch file ends. If the file exists, it is displayed a screen at a time and then you are prompted if you wish to delete it or not and the batch file ends.
When you wish to review your notes, from anywhere in DOS issue "DDNOTE" or "DDNOTE filename. The specified or all notes will appear on the screen one at a time. Afterwards, the file name will be given and a prompt to delete or not delete will show. After choosing `Y' or `N', if there are more files to show, you'll see them and be given a chance to delete each. Otherwise, the batch file ends. As before, when finished, you will remain in the directory from which you issued the batch file so you may continue with any previous work.
Directory Tag & Return
This requires one text file and two batch files to operate. The second batch file is created by the first, which is the one you'll run from the command line when you wish to return to the tagged directory. This combination allows one to tag a directory so that one may return to it at any time, including after a reboot. It may also be included in any batch file which starts a program which afterwards, you wish to return to the directory from which you started that program. An explanation follows "RT.bat". Afterwards, are two improved versions, including an extremely simplified one.
First, begin by creating or copying
the following,
beginning with the
double colon lines:
:: RT.txt :: Used with RT.BAT to Return to Tagged Directory :: @ECHO OFF CLS CD(Space)
Note that the "(Space)" above should not be typed. Simply be sure there is a space after the "CD". As well, do not press "ENTER" after this line. Save this as "RT.txt" in your BATCH directory.
Be aware that this file should be typed in a text editor which does not allow the cursor to drop below the last line. Otherwise, it will insert a carriage return and the batch file will not work. To test for this, when you type the last line, do not press "ENTER". See if the cursor can be made to drop below this line by pressing the "Down Arrow" key. If it drops, expect that you may have a problem.
An alternative is to use the "COPY CON" method right from the DOS prompt. Simply go to your BATCH directory and enter "COPY CON RT.TXT" at the command line. Then type the following, pressing "ENTER" after each line:
:: RT.txt :: Used with RT.BAT to Return to Tagged Directory :: @ECHO OFF CLS CD ^Z
Be sure to include the space after "CD" and press "Control-Z" immediately after. Hit "ENTER" to exit, and your file will be made. If you cannot get this to work by any method, e-mail me and I will send you a copy of the required text file.
Now make or copy the following:
RT.bat:: RT.bat :: Tags the Current Directory :: @ECHO OFF COPY C:\BATCH\RT.TXT C:\BATCH\RTT.BAT /Y > NUL CLS ECHO. ECHO Ready to Return to ECHO the Current Directory ECHO. CD >> C:\BATCH\RTT.BAT
To use this batch file, when you are in a directory to which you wish to return, press "RT, Enter". The screen will clear and the message "Ready to Return to the Current Directory" appears. Now do whatever you want: Start and work within a program, do DOS work, even reboot. When you wish to return to the tagged directory, press "RTT, Enter". You'll be whisked back!
"RT.bat" (Return-Tag.bat) works by creating the file "RTT.bat" (Return-To.bat), containing the main line of "CD ". It gets this information from "RT.txt" and places it into a new copy of "RTT.Bat", even if "RTT.bat already exists. This is where the "/Y" comes in. It answers "Yes" to the prompt regarding over-writing an existing file so you won't have to every time.
"RT.bat" continues by placing a message on the screen alerting the user that the directory has been tagged. Then it appends the current directory to the end of the "CD " line via the double ">". The line then reads "CD (tagged directory)". ("CD" by itself displays the current directory and path. Try it yourself at the command line.)
Since all this is placed into a batch file called "RTT.bat", when you enter "RTT", it changes to the tagged directory. Plus, because it is written to the hard drive, it survives a reboot.
Note this does not work if you tag a
directory on one
drive and attempt to
to return to it from another drive.
Simply switch to
the tagged directory's
drive first, then issue the "RTT" command.
This is a simple setup and is VERY powerful, but I crave
even MORE power. Therefore, I have assigned "RT" and "RTT" to function keys.
Now, I am one keystroke away from tagging a directory and only one away again
from returning to it.
It's very impressive to GUI users whom are
not used to DOS' blinding speed in situations where you have programmed events
into batch files *and* assigned the batch files to function keys. Also, to wow
them further, I have all my batch files loaded on to a RAM drive. Since they are
always in memory, they are zippy, zippy, zippy! See DOS Tips for more
such improvements.
I also have two other versions of this setup. They
are
more powerful, and the last is also much simplified.
Both allow one to
return to the tagged directory from
anywhere in DOS, regardless of the
drive.
Each is presented here:
RETURN.bat
(Improved)
The improved version requires two text files, two batch files and a utility to operate. As with the previous example, the second batch file is created by the first. This combination allows one to tag a directory so that it may be returned to at any time, still including after a reboot, but now from anywhere in DOS, even from another drive. Notes follow each section, with a full explanation given at the end, including the "DRIVE-IS" utility.
First, begin by creating or copying
the following,
beginning with the
double colon lines:
:: HOME.txt :: Used With Home.bat :: @ECHO OFF (Blank Line)
Note the blank line. Do not type those words; simply leave a blank line. |
:: SAVE-DIR.txt :: Used with Save-Dir.BAT to Return to Tagged Directory :: CLS CD(Space)
Notice that there is no "ECHO OFF". Also, the word "(Space)" should not be typed, nor should you press "ENTER" after this line. Simply be sure there is a space after the "CD" and that it is the last line of the file. Save both text files in your BATCH directory.
As discussed further back, this file should be typed in a text editor which does not allow the cursor to drop below the last line. Otherwise, it will insert a carriage return and the batch file will not work. Should this be a problem, see the previous version of the batch file for a test, and methods to overcome this.
Finally, make or copy the following:
SAVE-DIR.bat:: SAVE-DIR.bat (Improved) :: Remembers the Current Drive and Directory :: @ECHO OFF COPY C:\BATCH\HOME.TXT C:\BATCH\HOME.BAT /Y > NUL C:\UTIL\DRIVE-IS >> C:\BATCH\HOME.BAT TYPE C:\BATCH\SAVE-DIR.TXT >> C:\BATCH\HOME.BAT CD >> C:\BATCH\HOME.BAT CLS ECHO. ECHO Ready to Return to ECHO the Current Directory ECHO.
SAVE-DIR.bat works by first creating HOME.bat. It does that by copying the lines in HOME.txt to HOME.bat. It then uses DRIVE-IS to insert the current drive letter and a colon on the blank line at the temporary end of HOME.bat. It then appends the "CD " text in SAVE-DIR.txt to HOME.bat.
Note that there are other ways of getting the current drive into a batch file, but this is a fast and reliable method. It will also work in many versions of DOS, unlike some other methods. In the final version of this batch file, further on, is another method which can also work well under various DOS versions.
Finally, the batch file issues a "CD" command. This command normally displays the current path & directory at the command prompt. However, here it's redirected to HOME.bat where it is put after the letters "CD" and its succeeding space. These are already in place thanks to them being put there when SAVE-DIR.txt was appended to the file. All three operations are done by DOS' append redirector, ">>".
So what you end up with is a batch file which main lines
read:
(Drive):
CD (Path\Directory)
The parentheses will have your tagged drive and directory inserted each time you
issue the "Tag" (Save-Dir) command. When you run the HOME batch file, it changes
to the drive and directory you had previously tagged. Neat, huh?
Each time you use it, SAVE-DIR.bat makes a new HOME.bat by overwriting the old. To eliminate the prompt associated with overwriting a "/Y" is used. It tells DOS to overwrite without prompting. The "NUL" is used to keep the screen clean. Anything sent to NUL is not echoed (displayed) on the screen, so you don't see "One file(s) copied".
DRIVE-IS.com is a compiled debug script. Download Drive-Is.com here and place it in a C:\UTIL (Utilities) directory.
To use this batch file, when you are in a directory to which you wish to return, enter "Save-Dir". The screen will clear and the message "Ready to Return to Current Directory" appears. Now do whatever you want: Start and work within a program, do DOS work, switch drives, even reboot. When you wish to return to the tagged directory, enter "Home". You'll be whisked back! You may also shell out of a program and use this to go to the tagged directory. Then, typing "EXIT" takes you back into your application.
In use, "Save-Dir" and "Home" are too long to type, so either shorten the names, or better yet, assign the batch files to a function keys. One key will tag the drive & directory, the other will return you. I use F11 and F12.
RETURN.bat
(Simplified)
As I like to be "Mr. Milliseconds", I am always looking for ways to shorten batch files in order to have them run faster and also to simplify them. Here is a final version of RETURN.bat. It uses an update to the DOS "SET" command called "XSET". In only two steps, this places the current drive & directory into environment variables with no text files or debug utility required. An XSET link is at the end.
:: Save-DIR.bat (Simplified) :: Remembers the Current Drive and Directory :: @ECHO OFF ECHO :: Home.bat > C:\BATCH\HOME.BAT ECHO :: Returns to the Tagged Directory >> C:\BATCH\HOME.BAT ECHO :: >> C:\BATCH\HOME.BAT ECHO @ECHO OFF >> C:\BATCH\HOME.BAT ECHO. >> C:\BATCH\HOME.BAT C:\DOS\XSET CUR-DRIVE FDRIVE NUL ECHO %CUR-DRIVE% >> C:\BATCH\HOME.BAT C:\DOS\XSET CUR-DIR DIR ECHO CD\%CUR-DIR% >> C:\BATCH\HOME.BAT ECHO CLS >> C:\BATCH\HOME.BAT ECHO C:\DOS\DIR >> C:\BATCH\HOME.BAT CLS ECHO. ECHO Ready to Return to ECHO the Current Directory ECHO. :END SET CUR-DRIVE= SET CUR-DIR=
This works by first creating HOME.bat. If a previous version already exists, it is automatically overwritten. SAVE-DIR.bat begins that process by echoing some basic header text to HOME.bat. Next, the real work begins when XSET places the current drive letter into an environment variable called "CUR-DRIVE" and the current directory into one called "CUR-DIR". Each variable's value is then sent as a separate line to HOME.bat, with the directory value being preceded by a "CD\" command. To finish up HOME.bat, "Clear Screen" (CLS) and "Directory" (DIR) commands are sent to it.
Next, SAVE-DIR.bat clears the current DOS screen and displays a prompt stating that DOS is ready to return to the current directory. Finally, the environment values are reset, thus removing them from memory.
In operation, SAVE-DIR places the current drive letter on
one line of HOME.bat. Then, a "Change Directory" (CD) command is placed on the
next, followed by the current directory path & name. When Home.bat is run,
it issues these basic commands:
(tagged drive)
CD\(tagged
directory)
CLS
DIR
Consequently, you are
logged on to the tagged drive and taken to the tagged directory. HOME.bat
finishes up by clearing the screen and showing a files listing of the
returned-to directory. You may add the "DIR" switches you prefer to get say, an
alphabetical list which pauses with each full screen, or whatever options you
like. Here is the link to get XSET.
This is the same as the previous example. Simply issue "SAVE-DIR" in any directory. A prompt appears saying DOS is ready to return to that directory. At any time when in DOS, enter "HOME" and you'll be taken back to the tagged directory. Again, this is better if you assign "SAVE-DIR and "HOME" to function keys.
This last version is the one I actually use, although with some modifications and additions to suit my particular computer setup. I have SAVE-DIR programmed to F11, while F12 runs HOME.bat. I also use Loren Blaney's Color Directory as a replacement for the DOS "DIR" command. Thus, F12 takes me back to the tagged directory *and* displays that directory's contents in wide format with each file type in a different colour - all in one step.
SDEL.bat
(Improved)
Here is an improvement on the SDEL.bat from Batch File Examples. It allows one to specify as many files as the command line will hold.
:: SDEL.bat (Improved) :: Displays Files to Be Deleted :: Prompts to Delete Files or Abort Operation :: Allows as Many Files to be Specified as the Command Line can Hold :: @ECHO OFF CLS IF "%1" == "" GOTO NO-FILES ECHO. ECHO. ECHO These Files Will Be Deleted: ECHO. DIR | FIND "Directory" ECHO. ECHO %1 %2 %3 %4 %5 %6 %7 %9 %9 ECHO. ECHO. ECHO To Delete Listed Files, Press: D. ECHO. ECHO To Cancel, Press: Escape. ECHO. CHOICE /C:D /N > NUL IF ERRORLEVEL 2 GOTO END IF ERRORLEVEL 1 GOTO AGAIN GOTO END :AGAIN ECHO. ECHO Deleting %1 DEL %1 SHIFT IF NOT "%1" == "" GOTO AGAIN GOTO END :NO-FILES ECHO. ECHO NO FILES GIVEN! ECHO. :END C:\BATCH\DR.BAT
This begins by checking to see if one or more file names were typed at the command line. Next it displays the directory name and shows up to the first nine files contained therein which are to be deleted. It then prompts the user to choose to delete the files or to abort the operation. If `D' is pressed, all files specified at the command line will be deleted one at a time with an on-screen message confirming this. As a final look, the directory is displayed to show the files are gone.
This batch file should be run in the directory containing the files you wish to delete. It may be run from outside, but it means typing the path for each file. That tedium aside, the final line would give a directory listing of your current directory and not that in which the deletions have taken place.
TOP.bat
Have you ever wanted to change to another drive's root directory but had forgotten you had been working on that drive earlier? When you log on to it, you'll likely be in some sub directory. Here are two methods which will allow you to go immediately to the root directory of any drive on your system and see a files listing when you arrive. As with the last example, the first method uses XSET, an update to the DOS "SET" command. It will be assumed it is in the "C:\DOS" directory. Afterward, a much simpler method to do the same thing will be shown.
:: TOP.bat :: Moves to and Displays the Root Directory of the Selected Drive :: @ECHO OFF IF "%1" == "" GOTO CURRENT SET XSET=/UPPER C:\DOS\XSET DRIVE=%1 IF "%DRIVE%" == "A" GOTO A-DRIVE IF "%DRIVE%" == "B" GOTO B-DRIVE IF "%DRIVE%" == "C" GOTO C-DRIVE IF "%DRIVE%" == "D" GOTO D-DRIVE IF "%DRIVE%" == "E" GOTO E-DRIVE IF "%DRIVE%" == "F" GOTO F-DRIVE :CURRENT CD\ GOTO END :A-DRIVE A: CD\ GOTO END :B-DRIVE B: CD\ GOTO END :C-DRIVE C: CD\ GOTO END :D-DRIVE D: CD\ GOTO END :E-DRIVE E: CD\ GOTO END :F-DRIVE F: CD\ :END CLS CALL C:\BATCH\DR SET XSET= SET DRIVE=
To use this batch file, simply enter "TOP" or "TOP (Drive Letter)". Be sure to place a space between the batch file name and the drive letter should you specify one. Do not include the drive letter colon ( : ). You'll then zoom to the root directory of the current or selected drive, with a list of its files and sub-directories shown on a cleared screen.
The first thing which happens is that the batch file checks to see if you entered a drive letter or not. If not, it branches to the "CURRENT" label where the "CD" (Change Directory) command switches to the root directory of the drive on which you currently are working. The file then branches to the end where "Clear Screen" and "DR" commands are issued. As always, you may substitute the DOS "DIR" command here in place of the "DR" one.
Now, if you do specify a drive letter, including even one for the current drive, things get interesting. First the DOS "SET" command is used to set the XSET environment. In this instance, XSET is requested to make all variables be in upper case. So whether you issue a drive letter in lower or upper case on the command line, XSET will always make it be upper case. Thus, one of the succeeding "IF" statements will always be true (unless you type a drive letter which has not been included in the batch file's "IF" statements). One of the statements will be true because the batch file uses only upper-case letters with which to test.
Without XSET, two tests for each drive letter would be required. One would have to be for a lower case possibility, and the other for an upper case one. By using XSET's "/UPPER" switch, any drive letter you issue at the command line will be converted to upper case, so double "IF" statements are unnecessary.
Next, XSET is used again to make an environment variable called "DRIVE" from the first parameter you type on the command line after the batch file's name. As just explained, if you type "top d" or "TOP D", the variable will be upper-case `D' in either instance. After that, this variable is checked to see if it matches one of the drive letters in the list. I have included drive letters from `A' to `F' inclusive. You may add or eliminate those which do, or do not, suit your setup.
When the variable matches one of the drive letters, the
batch file branches to a specific label which coincides with that drive letter.
That letter is then issued in order to log you on to the appropriate drive, and
as earlier, the "CD" command changes to the root directory of that drive.
Afterwards, the batch file goes to the end. At that point, the root directory
contents are displayed and any variables are removed from memory by setting them
to nothing.
Here is the link to get XSET.
It occurred to me after writing this that I was being a goof. I realised that I could do the same thing with just a few lines of code. Here is that version:
:: TOP.bat (Simplified) :: Moves to and Displays the Root Directory of the Selected Drive :: @ECHO OFF IF "%1" == "" GOTO CURRENT %1: :CURRENT CD\ CLS C:\BATCH\DR
Yes... that's it! In this version, all that is required is to use "%1" in front of a colon. The drive letter will be filled in automatically. After that, the batch file issues "CD\" and displays a directory on a cleared screen. If no drive letter is given, the batch file immediately issues "CD\" and does the directory display.
More of these are in
Advanced Batch Files
II.
¦ Batch
File Basics ¦ |
||
¦ Batch File Examples ¦ | ¦ Advanced Batch Files II ¦ | ¦ Batch File Tips ¦ |
Return to the Main DOS Page |
Obtain 500+ Batch Files |
Go to the Richard Bonner's Home Page |