Dos Variables

  • November 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Dos Variables as PDF for free.

More details

  • Words: 29,339
  • Pages: 80
DDB: DOS Variables

Doctor DOS Betamax's ...

DOS VARIABLES A Short Discussion of DOS Replaceable Characters & Strings

As one's usage of DOS increases along with the knowledge that accompanies it, a desire to have DOS do more will emerge. One of the first power tools many DOS users implement is the batch file. A basic DOS Batch File Tutorial may be found at this website that will get you started, but this discussion is aimed at adding a batch file utensil to your DOS tool box. What follows is information on DOS variables, what they are, and their typical usage. As with other discussions at this website, deep detail will not be gone into. If you want to know more about this aspect of DOS, see your DOS manual's section on Variables, and/or the Command Reference, and/or the On-Screen Help. Be aware that not everything presented here may work for your version or manufacturer of DOS. As well, there are some variances among DOS versions and software. What is presented here will use the most common methods. Despite differences, the concepts still hold true, and learning to use variables will be one more brick in the structure of a strong DOS knowledge.

http://www.chebucto.ns.ca/~ak621/DOS/DOS-Var.html (1 of 10)10/26/2006 12:57:22 PM

DDB: DOS Variables

INFORMATION BELOW MAY NOT BE REPRODUCED WITHOUT PERMISSION FROM THE AUTHOR ©

What is a DOS Variable? A variable is a term to which a value or character string is assigned. It consists of the name, which can be one or more letters, numbers or ASCII characters. If letters are used, they are sometimes stored in uppercase by some versions of DOS, some of the time. ASCII characters may include spaces, but there are times when spaces can cause problems. Others that can cause havoc are the percent sign ( % ), redirection signs ( > < ), the pipe symbol ( | ), and equals ( = ). It is better if they are not used until one becomes proficient at using variables. There are two related types of variables: Environmental Variables and Replaceable Character Variables. Both replace one term with another and save the information to memory, but the former's information is kept in a memory area called the "DOS Environment" until it is removed or the computer is rebooted. The latter use single characters or numbers as the names and as soon as the operation is completed, the information is typically deleted. Because the value can change, or vary, these are known as "Variables". They are usually accompanied by percent signs, but there are DOS versions that deviate somewhat. As an example, the method used by 4DOS for its built-in environmental variables is to precede them with " %_" (percent underscore), whereas DR, MS, and PC DOS all use one percent before, and one after, an environment name. 4DOS recognises this and also makes usage of it. Other DOSes may differ slightly. Some may use a `$' on either side. Replaceable Character Variables use a single percent sign or a single dollar sign before the name in all the DOS versions I have come across, but that may not be universal.

ENVIRONMENTAL VARIABLES Each consists of the Variable Name to be assigned, and the second part of it is the contents of that variable. These are placed into memory by the DOS "SET" command. This command may be used to view, add, delete or modify environmental variables. The name given to each will usually be preceded and succeeded by a percent sign ( % ) after it is stored. This might look like: SET LEVEL=2. This sets a value of `2' for the name "LEVEL". This information is stored in the environment as " % LEVEL% ". So everywhere %LEVEL% is seen, it will be replaced with the value of `2'. Using such variables means that the user need not type the contents out every time. Instead, the shortcut of %LEVEL http://www.chebucto.ns.ca/~ak621/DOS/DOS-Var.html (2 of 10)10/26/2006 12:57:22 PM

DDB: DOS Variables

% can be employed. The above example may not seem like much a shortcut because it takes more to type "LEVEL" than `2', but as you read on, the advantage shall become apparent. A variable is not only a shortcut to fewer keystrokes. If one was to not assign information as a variable and then at some point wanted to change that information, every instance of this change would have to be retyped. If there are dozens of references to that information, it would mean a lot of retyping. Using a variable to represent that information means only one change needs to be made, and that is done in the place where the variable is defined. Once one assigns the variable its new contents, all the other locations where the variable is used will automatically reference that change of contents through the variable's name. Another usage is to assign DOS variables to do specific procedures. This places information into memory so that DOS may make use of it to perform a dedicated task - one typically assigned by the user. Barring a reboot, these variables may be kept in memory indefinitely and used by DOS or its programs as needed during any computer work session. If one wishes to use them in every session, they must be included in a start-up file which can be used to place them in memory during boot-up.

THE ENVIRONMENT All of this is held in the environment. The information in there is typically used by DOS and its software as a reference as to how it should act, or where to locate certain files, or how to configure itself - or a combination of these. DOS, or its software programs, consult this area to see if there are instructions or information in there for them. So essentially, the environment contains the conditions & surroundings in which DOS and its programs work. These conditions can be imposed by DOS itself, its software, or by a user.

DOS INTERNAL VARIABLES A few of the environmental variables that come with DOS are shown below. These are set by DOS itself, or they may be specified by the user, as necessary, either through the boot-up configuration file or during the course of any computer work session. The latter may be done at the comand line or from within a batch file. ● ● ● ● ● ● ● ●

BOOT - The Boot Drive COMSPEC - The command interpreter specifier CONFIG - Allows various start-up configurations to be referenced. COPYCMD - Sets the overwrite traits of COPY, MOVE, and XCOPY. CPU - The current processor in use. DAY - The current day of the month. DIRCMD - Sets the method by which DIR shows files. DATE - The current date.

http://www.chebucto.ns.ca/~ak621/DOS/DOS-Var.html (3 of 10)10/26/2006 12:57:22 PM

DDB: DOS Variables ● ● ● ● ● ●

OS - The current operating system's name and version. PATH - The directories DOS may search to locate files. PROMPT - That which appears before the cursor at the command line. TEMP or TMP - Where temporary information is stored or work done. TIME - The current time. VER - The DOS version in use.

Be aware that not all versions of DOS support all of the above, and that these are not the only ones available. The ones above can take different forms with different DOSes. Realise too, that any changes made to variables that had originally been set during boot up will return to their defaults after a reboot. If you want to keep any of them, you will have to place the SET commands into AUTOEXEC.bat or some other file which is accessed upon boot-up.

Why Might I Want to Use the Environment beyond What was Already Set? Well, first of all, as mentioned above, one may wish to change the DOS default settings. Many users prefer a custom prompt beyond " C:\> ". As well, users might wish to include other directories in the DOS path. To see what environment variables are already in memory, issue "SET" by itself. A list will appear. Each can be changed by specifying the variable name, an equals sign and a new value. Thus, resetting %PROMPT% by: SET PROMPT=$D $P will show the current date, a space, and then the current drive & directory at the command line.

An Aside:

Do not confuse the PROMPT command's usage of dollar signs with the usage of the dollar sign for variables as discussed in this article. PROMPT uses the dollar sign to designate what are called "meta strings" in order to set the prompt one sees at the command line. These are similar in execution to, but are not the same as environment variables. (See your DOS manual's PROMPT explanation.)

Another reason to want to reset some of the values differently from the DOS defaults is in regards to

http://www.chebucto.ns.ca/~ak621/DOS/DOS-Var.html (4 of 10)10/26/2006 12:57:22 PM

DDB: DOS Variables

the TEMP directory. This is where temporary work is performed and temporary information is stored - a scratch pad of sorts. One may wish to locate that directory on a faster RAM drive; that is done by creating the directory and then resetting the %TEMP% variable to point to that directory. Users may also want to use the environment to dictate how certain DOS commands will work, or to set up varying configurations. An example of the former would be the %COPYCMD%. This sets the overwrite characteristics of the COPY, MOVE, and XCOPY commands to make them ask the user before files are overwritten, or conversely, to allow automatic overwriting. The %CONFIG% variable references different startup configurations and permits additional choices to be made later on based on its contents. Beyond those types of assignments, one may want to set variables to automate work that DOS will do. That work might require a different value depending on circumstances, hence the need for a variable term. So this might be seen as: :LOOP (Commands) IF "%CYCLE%" == "20" GOTO END GOTO LOOP In operation, a batch file could be set up to run a procedure over & over until `n' cycles have passed. Lines could test for say, 20 cycles, and then determine what the file should do next. Above, this snippet of a batch file is testing to see if %CYCLE% has reached the value of `20'. If it hasn't, the batch file will run its procedure again. If it has, the batch operation will end. In operation, the value of %CYCLE% would vary (increase) by one for each time through the procedure as per instructions on other lines not seen here in this example. If you want to remove a variable entirely, issue "SET (name)=". So to delete the %CYCLE% variable in the above example, it would be "SET CYCLE=". Do not include any percent signs when issuing this command. The opposite side of this technique is if a value that must be kept constant might change in the course of operations. Placing the original value into the environment would preserve it for later usage: SET PARAMETER=%1 This takes the first item typed on the command line after the batch file name and assigns it the name of %PARAMETER%. If the contents of %1 were to change during the course of the procedure, but the first contents were those which are required, one could not use %1 anymore. However, since % PARAMETER% remembers those original contents, it may be referenced instead. This brings us to:

http://www.chebucto.ns.ca/~ak621/DOS/DOS-Var.html (5 of 10)10/26/2006 12:57:22 PM

DDB: DOS Variables

REPLACEABLE CHARACTER VARIABLES These are often used in conjunction with environment variables and can have changing values, too. They are always preceded by at least one percent sign ( % ) and some by two. Some, like DOSKEY use the dollar sign ( $ ) as a replaceable character identifier. All are just ways for the operating system or its programs to recognise variables. Here is an example of one used at the command line: Suppose you have a batch file called "CTF.bat". It is used to copy text files to a floppy in order to be taken home or elsewhere. You don't want it to copy all the files every time but only a certain one. So you set it up so that the batch file's main line reads: COPY %1.TXT A: The " %1 " is a replaceable character variable. The batch file will fill in whatever you type at the command line and issue the COPY command upon it. Thus, if you enter " CTF READ-ME " at the command line, CTF.bat will copy a file called READ-ME.txt from the current directory to your `A' drive floppy. The next time, you might issue " CTF DIRECTORY " so that time the file copied to the floppy would be DIRECTORY.txt. Note that this example uses only one percent sign. Generally, single ones are used for replaceable characters, while double percent signs are used when setting a string variable. More on that in a moment... Let's use a second variable in the above example. If you have two floppy drives and wish to copy a differing file to a differing drive, your batch file could read: COPY %1.TXT %2 The first variable would contain the file to be copied, while the second would be the drive to where it is to be copied. Thus, " CTF BACKUP B: " would copy BACKUP.txt to the `B' drive. %1 was replaced with " BACKUP ", while %2 was replaced with " B ". Then, DOS did the copy operation.

The FOR-IN-DO COMMAND Shown as the "FOR" command in documentation, this is often spoken of as "FOR-IN-0D". This command allows an operation to be carried out on a number of items which could be drives, files, or other commands. An example in a batch file might be: FOR %%F IN (TEST-1.txt TEST-2.txt) DO TYPE %%F

http://www.chebucto.ns.ca/~ak621/DOS/DOS-Var.html (6 of 10)10/26/2006 12:57:22 PM

DDB: DOS Variables

This means that FOR each file IN the brackets DO type (show) its contents on the screen. The variable " %%F " will represent each of the two text files in turn and display its contents. Note the two percent signs before the variable character of `F'. This is how this command is used in a batch file in most DOS versions. At the command line, this is reduced to one percent sign. Also realise that the `F' character could be any letter. I chose `F' because it represents the Files shown in the brackets. If drive letters were inside the brackets, I would use " %%D ", and so on. Alternatively, some DOS versions allow full words to be used as the variable: FOR %%FILES IN (TEST-1.txt TEST-2.txt) DO TYPE %%FILES

Where May I Use Variables? User variables may be employed at the command line and within programs. They are most commonly used with, or within, batch files.

Can You Provide More Examples? One would be using the current date. Perhaps you want to have a backup file made every day which name incorporates that day's date. You don't want to have to type in that differing date every day and would rather DOS do this for you. Older versions of DOS do not include this variable as set by DOS itself, but there are ways to get the date into the environment. CTULD.bat shown elsewhere at this website explains how that is done. New versions of DOS already set it as " %DATE% " or similar. Once the current date is in the environment, one may issue commands such as: MD C:\BACKUP\%DATE% COPY C:\DATA\*.* C:\BACKUP\%DATE% This makes a directory which name will be the current date and then copies the data files to it. Regardless of what the date is, a directory by that name will be created and files copied to it every day. As you are aware, the date varies every day but the variable name called " %DATE% " does not. It always remains as %DATE%, or whatever you choose to use. Since I incorporate a lot of date operations in my batch files, I prefer " %CUR-DATE% " to differentiate it from other date variables I use.

http://www.chebucto.ns.ca/~ak621/DOS/DOS-Var.html (7 of 10)10/26/2006 12:57:22 PM

DDB: DOS Variables

How Do I Create a Variable? Simply use the "SET" command. The syntax to place a variable in the environment is " SET V=S ", where `V' is the name you wish to assign as the variable, and `S' is the text string to be assigned. Thus, if you place a line like "SET NAME=%1" in a batch file called LOGIN.bat, when Judy wants to log into whatever has been set up for that purpose, she would enter "LOGIN JUDY". Afterwards, one can test for the possible names and have DOS do work depending on the name used: :: LOGIN.bat :: @ECHO OFF SET NAME=%1 IF "%NAME%" == "ADAM" GOTO ADAM IF "%NAME%" == "JUDY" GOTO JUDY IF "%NAME%" == "MAHENDRA" GOTO MAHENDRA :ADAM (Adam's login procedure) GOTO END :JUDY (JUDY's login procedure) GOTO END :MAHENDRA (MAHENDRA's login procedure) :END That was a simple example with no error checking or security; nor was there any method presented to deal with a user entering lower or mixed case. However, it gives an idea of how variables might be used for that purpose.

Tell Me about those Percent Signs Whenever you set a variable, it will show up in the environment as a character string with percent signs at each end in most versions of DOS. For replaceable character purposes, the single leading percent sign is used, with exceptions at least for the FOR-IN-DO command and DOSKEY, if not others.

http://www.chebucto.ns.ca/~ak621/DOS/DOS-Var.html (8 of 10)10/26/2006 12:57:22 PM

DDB: DOS Variables

DOSKEY uses the `$' for replaceable characters, so instead of " %1 ", DOSKEY will use " $1 ". As already mentioned regarding the FOR-IN-DO command, when it's used at the command line, one percent sign is used, while in a batch file, two are needed for most versions of DOS. FOR

%F IN (*.TXT *.DOC) DO COPY

%F A:

FOR %%F IN (*.TXT *.DOC) DO COPY %%F A: Either one will copy the items listed inside the brackets to the `A' drive, but the second line is to be used in a batch file. DOS will eliminate the first percent sign preceding the `F' character in each instance when used in a batch file. As with earlier examples, DOS will replace the variable " %F " (or " %%F ") with the specified item(s). In the above example, the items are all the .txt and .doc files in the current directory.

Is there a Size Limit to the Variable? The total limit is a minimum of 128 characters and newer versions of DOS allow more. That means that the variable name and its contents could be any combination up to 123 characters long, with the balance being taken up by "SET", a space and the "equals" sign. However, excessively long variables can take away some of the shortcut nature of variables, but may have a use with long Internet URLs or for long server designations on a network. They might also be useful to some for deeply-nested subdirectories or for very long file names on systems which allow them.

There is much more to this, including other ways to use the environment, but the preceding will give you an idea about, and a start to, using DOS variables.

Using environmental variables is yet another lane on the road to becoming a DOS Power User. Learning about, and making usage of them will improve productivity, allowing you to automate more complicated tasks.

http://www.chebucto.ns.ca/~ak621/DOS/DOS-Var.html (9 of 10)10/26/2006 12:57:22 PM

DDB: DOS Variables

Return to the Main DOS Page Go to Richard Bonner's Homepage.

http://www.chebucto.ns.ca/~ak621/DOS/DOS-Var.html (10 of 10)10/26/2006 12:57:22 PM

DDB: DOS Batch File Tips

DOS BATCH FILE TIPS

Doctor DOS Betamax's

¦ Batch File Basics ¦

¦ Batch File Examples ¦

¦ Advanced Batch Files ¦

¦ Obtain 500+ Batch Files ¦

Now that you've taken the time to review the Batch File Basics, here are tips to help you when composing and running your batch files. If you get experienced at writing them, you will find that you will not need refer to everything here because you will be able to keep much of what you are doing straight in your head. However, if you write batch files infrequently, or you haven't done one in a while, the tips presented below will be of assistance.

INFORMATION BELOW MAY NOT BE REPRODUCED WITHOUT PERMISSION FROM THE AUTHOR ©



Outline a Prospective File. Before firing up your text editor, outline on scrap paper what the batch file is for and what each step should do. Be sure to include what the file is not supposed to do, as well. Then compose the file with the commands necessary to achieve that end. For more complicated batch files, outline as above, but write the code for each section, one at a time. Then combine them into a cohesive batch file. You may even test the sub-sections

http://www.chebucto.ns.ca/~ak621/DOS/Bat-Tips.html (1 of 8)10/26/2006 1:12:07 PM

DDB: DOS Batch File Tips

separately to see if they do what is, and is not, intended.



Use a Template. Most batch files will start with a commented area containing the file's name, a short description, and perhaps a syntax example. Then there will usually be an "Echo Off" command. The last line might contain the word "END" or perhaps a DIR listing. If enough files you do contain these elements, make up a template with them in the file. Save it as "Bat-Temp" and place it in your BATCH directory. Set its attribute to read-only.(*) Then write a batch file to call the template into your favourite editor where you may then insert the appropriate commands for the new batch file you are about to make. When finished, save it under the name of the new batch file. (*) Since the template file has a read-only attribute, it cannot be accidentally overwritten if you forget to rename it when saving the new batch file. For more on this technique, see DOS Tips.







Write Batch Files in ASCII (Plain) Text. DOS requires ASCII text in order to understand a batch file and execute it. If you've used a word processor and failed to save the file in plain text, it will not execute properly, if at all, and you'll receive an error message. Use a Title and Comments. Displaying a title and using comments within a batch file will help you and others to know what a file, or part of a file, does. This is helpful when viewing or editing a file at a later date when you've forgotten what it does and/or how it does it. Use Double Colons for Comments. To prevent comments from resulting in error messages, use a double colon before each line containing any remarks. The "REM" command may also be used, but DOS will read the entire line and actually perform some things such as redirectors and piping. That may cause problems. When DOS comes across a double colon, it ignores anything after the second colon because this second colon is an illegal label character. DOS then skips to the next line. I also place a space after the double colon. This gives an extra degree of readability when perusing a lengthy batch file. (See Batch Basics for how this is employed.)





Incorporate Blank Lines and Indenting. This separates each part of a file and aids in readability. Indenting is useful for sub-routine sections. Neither affects the running of the file because DOS ignores any blank lines and indents after an "Echo Off" command. Be Consistent with Text. Use the same style for all composing. This could be upper, lower, or mixed case and the use of blank lines and Indenting for certain purposes. Keep labels consistent from file to file, as well. This allows one to fall into a pattern that will aid in deciphering a file at

http://www.chebucto.ns.ca/~ak621/DOS/Bat-Tips.html (2 of 8)10/26/2006 1:12:07 PM

DDB: DOS Batch File Tips

a later date. It's also handy should you decide to slightly alter a batch file for another, but similar purpose, while keeping the original. These similar batch files will have exactly the same layout which is good for later deciphering, and for consistency among types used for a similar purpose.



Anything Typed at the Command Line may be Used in a Batch File. Whatever steps you type at the command line to execute a task may be included when composing batch file. In fact, the simplest, basic batch files are made up of such lines. Now some things such as percent signs and quotation marks must be doubled in some cases, but legitimate commands can always be included. Of course, batch files can include more complicated operations that go beyond most command lines in most versions of DOS, but don't forget DOS basic command issuing methods when composing your batch files.





Remember to Space Parameters and Switches. As at the command line, spaces must be placed between any commands and their switches or parameters such as replaceable characters or file names. With no spaces, DOS will assume anything lumped together to be a single parameter, not the separate ones you intend. Keep Labels to a Minimum. When most DOS versions read a batch file, they read one line, execute it, and then read the batch file from the start to locate the next line. If there are excessive labels, it will take DOS slightly longer to execute the file because it must first locate the label and then run the line. So whenever you can write a batch file that executes in sequence, choose it over one that does much jumping around. This won't matter with short batch files, but complicated ones may be noticeably slower. Note that newer versions of DOS don't do this. In particular, with 4DOS' .btm batch files, the entire file is read into memory and then is executed a line at a time with no re-reads.





Use the `@' Character to Prevent a Line from being Echoed to the Screen. The `@' (at) character or sign may be used at the beginning of any line in order to suppress that line from being seen on screen. This is useful at the beginning of a batch file to prevent "ECHO OFF" from showing up, but may be used any time Echo is on to suppress a given line on screen. Use ECHO to Place Screen Messages. Having DOS prompt you as to what a batch file is doing is handy. This is especially important if an operation is taking some time and it appears that the computer has locked up. The message might say something like "Update Operation In Progress Please Stand By...".

http://www.chebucto.ns.ca/~ak621/DOS/Bat-Tips.html (3 of 8)10/26/2006 1:12:07 PM

DDB: DOS Batch File Tips

Use tabs to place the message away from the left margin to make it easier to spot on a crowded screen. Use ECHO. to place blank lines before & after a message to separate it from preceding and succeeding text. Note that there is to be no space between the "ECHO" command and the dot. If this does not display a blank line with your DOS version, try placing a colon ( : ) or an ASCII space after the "ECHO" command. You may make such a space by pressing & holding the "ALT" key and then typing "255" on the number pad. Upon releasing the "ALT" key, an ASCII space will be produced. (Note that some text editors may use a different method for this - consult your manual.)











Use the PAUSE Command to Halt a Batch File Temporarily. After this command, the batch file places a message on screen asking if the user wants to terminate the batch file and then waits for user input. Use the SHIFT Command when more than Nine Parameters are Required. SHIFT moves all parameters down by a factor of `1' each time it is executed. This way, a tenth parameter becomes #9 and may be accessed by referring to it as `%9'. Issuing SHIFT again makes %9 equal to what had originally been the eleventh parameter, and so on. Make Your Batch Files Wait a Bit by Using CHOICE. CHOICE has an option to wait `n' seconds and then execute one of the choices as pre-selected by the batch file author. The limit is only 99 seconds, but may be useful if a short delay will suffice. See the `/T' switch option in your DOS documentation. Use ANSI.sys to Add Colour and Form to Screen Messages. Dress up your batch files by using ANSI.sys to add colour to your messages. You may also wish to reserve certain colours for specific purposes. A warning message might be in red; a cautionary message in yellow, with green perhaps being reserved for those prompting the user to follow a specific action. You may also place messages anywhere on the screen using ANSI.sys' cursor positioning capability. Give Full Path names for all Commands. This allows DOS to go immediately to the directory required, thus saving a path search. So, as an example, if the MOVE command is to be used, issue it as "C:\DOS\MOVE..." rather than just "MOVE..." . This method can also be extended to issuing full paths for the files on which actions are to take place. This means that a command will act upon only files in the stated drive & directory. This is good because if the batch file were to inadvertently be run in a wrong directory, unexpected things might happen. Unintended files might even be deleted. Directing DOS to the place where you want actions to occur, even if it's the current directory, is a wise and safe method.

http://www.chebucto.ns.ca/~ak621/DOS/Bat-Tips.html (4 of 8)10/26/2006 1:12:07 PM

DDB: DOS Batch File Tips





Place Often-Used Operations in Their Own Batch File. If many batch files run, say, a directory listing at the end, or display the same closing screen, don't add these lines to the end of every file. Instead, place them in their own batch file and refer to it at the end of each file. This saves a lot of typing, and should you change this closing screen, directory display, or whatever, only one file needs to be changed instead of every instance in every file. Leave Clean-Up Until the End. If ending a batch file requires resetting parameters, deleting temporary files and/or directories and/or environment variables, and so on, do that at the very end of the batch file unless required beforehand. This is especially important if a final display will appear. It will be on the screen sooner if the command to do so is issued before those commands that will do the clean-up. Then while you are viewing the display, clean-up will be happening in the background. This will make your batch files seem to run faster. This trick can also be used within a batch file any time a display command is issued for which the user will be reading for a bit. This is, of course, if the deletions and resettings can be made part way through the running of the batch file without affecting the running of the rest of that file, or without having to be reissued. The latter could mean extra, unnecessary work for the batch file, possibly slowing its execution.



Never Use the Name of Another File as a Batch File Name. This may cause user confusion, and under certain circumstances one file may conflict with the other, producing unexpected results. This is especially a problem if the batch file has the same name as a DOSKEY Macro, 4DOS alias or an internal DOS command. The batch file won't execute because these DOS commands take precedence. Similarly, if one tries to run a batch file from a directory that contains a .bat file having the same name as a .com or .exe in that same directory, the intended one will not run. Another conflict could arise with directories that come before BATCH in the PATH statement. If the batch file name is the same as a program name that happens to be located in a PATH statement directory that comes before BATCH, the program will be executed instead of your batch file because DOS will find it first. Experienced users can ignore this tip if they know their systems well and have limited their PATH to essentially just the BATCH and DOS directories, or made them the first two directories in the Path. Thus, one may have a WP.bat if the WordPerfect directory is after the BATCH one, or not included in the PATH statement at all. (See Remove Programs from the Path, farther on.)



Rehearse Your Batch Files in a "TEST" Directory. Make a test directory with a couple of sub & sub-sub directories. Into it, place new batch files, along with various small files copied from other directories. (The small files are so that your tests will run faster.) Then run the file to be sure it does what you want, and more importantly, see that it doesn't do want you don't want.

http://www.chebucto.ns.ca/~ak621/DOS/Bat-Tips.html (5 of 8)10/26/2006 1:12:07 PM

DDB: DOS Batch File Tips

If anything bad happens, it will likely only affect the test directories and their files. If you can't see what is wrong with a non-functioning batch file, debug it as described in the next tip. Once you are confident of the file, it may then be placed into the regular BATCH directory. As your batch file experience grows, you will need this less because you will most often write batch files that work correctly on the first try. Still, for complicated files, a test directory is a good precaution, even for experienced batch-file writers.



Debug Your Files. In keeping with the above tip, run the file to see what it does or doesn't do that was not intended. Use COMMAND /C /Y (batch file name) to step through each line of the file. When a line executes in a way unintended, you've located the problem. See STEP. bat in Batch File Samples for a detailed example.) Note that not all DOS versions are capable of this. 4DOS users may do it by issuing SETDOS /Y1 ^ (batch file name) Those with earlier DOS versions, may remove or "REM" (Remark) out the "Echo Off" command to see each step as DOS executes it. (Note, a better method is to use a double colon and a space in place of "REM". See farther back in this article and also Batch Basics for more on this.)



To Stop a Batch File, Use "Control-C". If something is not happening as you expect and the file continues to run or has locked up, press ^C (or ^Break) to stop it immediately. Answer `Y' to the prompt, `N' if you wish it to continue. Note, that to assure this works, set "Break" to "On". I have the "Break=On" command as part of my AUTOEXEC.bat so I know it's on all the time. Be aware too, that some programs turn off "Break" when loading. To see if this is the case, at the command line, issue the "Break" command with no parameter. It should be "On". If it's not, add "Break=On" as the last line in your AUTOEXEC.bat. If some programs do not act as desired when issuing a ^C, you will have to add "Break=Off" to the beginning of the batch file that starts the program and "Break=On" to the end of the file. Most programs like this will turn it off automatically, so you'll only need the closing "Break=On" line.



Remove Programs from the Path. Once you use a batch file to start a program, take its name out of your PATH statement. Since the batch file will start the program from anywhere in DOS, it need not be able to be found by DOS in order to start because the file directs DOS as to where it is. A shorter path makes less work for DOS, thus speeding up path and file search operations. Note however, that some programs require PATH statement inclusion if they sometimes operate outside their own directories. After removing the program from your path, if it does not function properly, see if in the program's setup you may specify the program's path and file

http://www.chebucto.ns.ca/~ak621/DOS/Bat-Tips.html (6 of 8)10/26/2006 1:12:07 PM

DDB: DOS Batch File Tips

locations. If so, modify that parameter. Some programs also allow an environmental statement that tells the program where to find its files. This is accomplished through the "SET" command. In your batch file, place such a statement on the line immediately preceding that which starts the given program. Place another "SET" command on the line immediately after the program-starting one to restore the values. This last step is important because these environment statements take up memory. Another method is to use APPEND statements in the batch file. If the program fails to function properly after trying each of these, you may have to restore it to the path. See your DOS and/or program manuals for more on the "SET" and "APPEND" commands. Also, see the DOS Tips section.



Use a Disc Cache. DOS reads a batch file a line at a time, starting from the top of the file for each line. So after Line `1' is read, it starts at the top, sees line `1', then looks at Line `2'. It then reads from the top looking for Line `3', and so on. If the file must be read from the hard drive for each line, this takes time. It would be better if the file could be read into memory once and then DOS could look through memory rather than reading from the hard drive. 4DOS and other new DOS versions do this, but older DOS versions use the method described above. One way to get older DOS versions to read the file from memory is accomplished by using a disc cache. The file is read into the cache and DOS will look there before trying to read from the hard drive. This is much faster. Another method to speed execution is to load all batch files on to a RAM drive. See next.







Load Batch Files on to a RAM Drive. Use a small-size RAM drive with small cluster size to preserve memory. Then place the RAM Drive batch directory at or near the start of your path statement. Your batch files will run much faster because they are always in memory. Automate the process by placing statements in your AUTOEXEC.bat to create a BATCH directory on the RAM Drive and to copy the batch files to it. Learn to Use Switches and Exit Codes. The usage of a command's switches allows one to tailor it to run in varying ways apart from its basic operation. Couple this with making use of that command's exit codes and one may write very powerful batch files that function on their own. These batch files can make decisions without further input based on your pre-selected criteria. You may apply the same idea to utilities and major applications. (See the DOS Switches and Exit Codes discussions elsewhere at this website.) Assign Your Most-Used Batch Files to Function or other Keys. Then you are only a keystroke away from your most common work. I use my DOWNLOAD, UPLOAD, and TEMP directories continually throughout the day, so they are available from "Alt-D", "Alt-U", and "Alt-

http://www.chebucto.ns.ca/~ak621/DOS/Bat-Tips.html (7 of 8)10/26/2006 1:12:07 PM

DDB: DOS Batch File Tips

T". Hitting any of those key combinations, runs the batch file for me and takes me to the chosen directory. It then displays that directory in wide format, alphabetical order, and with each file type in a different colour.



Buy DOS Books. DOS has been around long enough that many publications are easily available in used books stores at excellent prices. You don't necessarily need one geared to your DOS version; just look for a good chapter or section on Batch Files. Of course, additional information presented on DOS doesn't hurt either. A good library of books is a must if you are thinking of becoming a power user, or just wish to improve your DOS operating system knowledge. Once you are familiar with that information, modern facts on batch files may be had from your on-screen "Help" files or from numerous websites. See the Batch File Tips, Tutorials, and Utilities section of DOS Websites elsewhere at this site.

¦ Batch File Basics ¦

¦ Batch File Examples ¦

¦ Advanced Batch Files ¦

Return to the Main DOS Page

Obtain 500+ Batch Files

Go to the Richard Bonner's Home Page

http://www.chebucto.ns.ca/~ak621/DOS/Bat-Tips.html (8 of 8)10/26/2006 1:12:07 PM

DDB: DOS Batch File Tutorial

Doctor DOS Betamax's

Contents Batch File Basics (this page)

DOS BATCH FILE TUTORIAL

Batch File Examples Advanced Examples

EXCEPT FOR THE BATCH FILES THEMSELVES, INFORMATION ON THESE BATCH FILE PAGES MAY NOT BE REPRODUCED WITHOUT PERMISSION FROM THE AUTHOR ©

Batch File Tips Obtain 500+ Batch Files

BATCH FILE BASICS This is an elementary introduction to DOS batch files with examples. After the tutorial, you may link to sample files which may be copied or downloaded for your own usage. Each example will be explained as to what it accomplishes and what individual lines do towards that end. An additional tips section will aid you when composing your own batch files, and finally you may obtain 500 & more of the batch files I employ for my own purposes. This tutorial will assume that you are running DOS exclusively, although Linux, Macintosh, OS/2, Unix and Windows users may also make some, if not complete, usage of DOS batch files. However, be aware, users of those systems will likely need to modify some of the examples in order to comply. It is assumed you know the basics of DOS command issuing, its file & directory structures & conventions, and have some understanding of paths and the path statement. Some parts will be geared to users of newer DOS versions, but most of these batch files will work on all versions and manufacturers of stand-alone DOSes. There will be some notes to alert you to commands available only in certain versions, but this may not be the situation in all cases. Be sure the batch file is exactly as shown here and it is in a directory in your path statement, or run it from the current directory. Yet if after doing this, when you run it and get: "Bad Command or File Name", or odd things happen, chances are that particular file may not function with your DOS manufacturer or version. If you are unsure of which DOS version you are running, type "VER" at the command prompt. Again. modifications may be required to make the examples work on your system. Remember, Doctor DOS will not be responsible for any problems encountered through the use or mis-use of anything presented here.

http://www.chebucto.ns.ca/~ak621/DOS/BatBasic.html (1 of 8)10/26/2006 1:02:05 PM

DDB: DOS Batch File Tutorial

Batch files make a person a power user, and becoming so allows one to run circles around point & click friends and co-workers.

Here are the Main Advantages to Using Batch Files ●





Fewer keystrokes required to perform Computer operations.

A single command executes an extended series of complicated operations.

Less chance of making typing errors. ● ●

Short commands are easier to remember than a long series of keystrokes.



Major time savings.

Decisions can be automatically made for the user.

Batch File Tutorial

PRELIMINARY "What is a batch file and why might I need one?" A batch file is a series of commands that you might ordinarily issue at the system prompt in order to perform a computer operation. The most common uses are to start programs and to run utilities. Batch files do that with one command instead of the multiple commands usually required. They can be likened to shortcut icons as seen in point-&-click operating systems, but batch files are much more powerful. Using a batch file to start a program often means that your path statement may be made shorter. This means fewer directories through which DOS must search during its operations. Having a shorter path will also leave room for other programs which may require path inclusion in order to function properly. Further, sophisticated batch files can improve upon program starting by loading all or part of the program into upper or expanded/extended memory, thus freeing up more lower (conventional) memory. Allowing lots of lower memory means your programs have breathing room and there will be space for utilities to run. This same capability may be had at the command line, but the commands will likely be intricate and difficult to remember. Why not let a batch file do the work for you? With one command, a batch file can start the program in the desired configuration and in addition, can request an associated file such as a word processor document or spreadsheet be loaded once the main program is running. This saves the user a search for that document and the issuing of the commands necessary to load it. One simple command of the user's naming does it all. This is similar to Window's "File Association" feature, but with more advantages because a number of batch files could be written to load the same file each with its own, but different, attributes or start-up options. In fact, one could even have the same file loaded into different programs, each time with specific, yet different, options. The user never has to change these configurations manually. As just mentioned, once set up, the batch files can load the program in the user's chosen configuration automatically with the user's choice of command-line options. Using a batch file to run a utility means being able to have direct access to that utility. If there are any often-used specific options, they can be included in the batch file and thus save you from typing, and the possibility of mis-typing, http://www.chebucto.ns.ca/~ak621/DOS/BatBasic.html (2 of 8)10/26/2006 1:02:05 PM

DDB: DOS Batch File Tutorial

these parameters. DOS batch files can also make decisions to perform operations only if certain conditions exist or don't exist. The most sophisticated ones can even emulate commands not normally available with the DOS operating system.

WHAT YOU'LL BE DOING: The first section, below, will help you to understand and then write basic batch files. For those who are afraid of programming, this article will show you how simple it really is. DOS batch files are a good place to start learning programming because they use plain-English commands for the most part. Thus, they are easy to comprehend, even for those of you whose first language is not English. You may then move on to other sections which allow you to improve a batch file, and to make it look attractive & easy to decipher when looking back at it from some future point. You'll also see how to combine small batch files to do larger things. On the Advanced Batch Files page, you'll be shown more complicated example batch files which use command-line parameters. These allow the file to function in different ways depending on the circumstances and the desired outcome. On the other advanced pages, there are batch files given that prompt a user for input. These permit operations based on choices made by the user after the file is running.

RENAMING You may wish to rename the sample files and perhaps use a different path structure. Go ahead - this tutorial is just for example purposes. You may also see another use for a batch file if it had some modifications. Be creative and try the changes; however, be aware that no responsibility will be taken by Doctor DOS for any problems encountered when using (or mis-using) anything presented here.

TESTING I suggest that you always try out any batch file in a TEST directory. Copy some files there to try out the batch. If it works as you intend, and more importantly, if it does not work as you do not intend, then transfer it to your regular working batch directory. If anything did go wrong in the TEST directory, it would only affect copies of files and not corrupt or destroy the originals.

GETTING STARTED: First realise that a batch file typically takes the following form:

* Get Ready to Use the Program or Utility * Run the Program or Utility * Verify and/or Clean Up and/or Restore

The first line above, means to give some initial instructions that prepare the main program or utility to run. These might be to go to a specific directory, create or change an environment parameter, place a message on to the screen, or anything else required for the program/utility to run as you or it requires, as dictated by the task to be done. The next line runs the program or utility as directed by you, or in some cases, by the batch file itself under the guidance of some pre-selected criteria. Finally, the last line can be used to display closing messages or a directory listing as confirmation of a utility operation. It might delete temporary files made by the batch file, or it might restore the program or DOS to its default settings. Another use would be to deposit the user in some specific directory after the program or utility has finished, or return one to where one was before running the batch file.

"What do I do first?" To begin, create a directory called "C:\BATCH" where you will place your new batch files. Some people use "C:\BAT", but that bothers me because there would then be a directory with the same name as the batch file extensions, which must use ".bat". For housekeeping and organizational purposes, I try to not use names more than once - especially where conflicts might arise or confusion generated.

http://www.chebucto.ns.ca/~ak621/DOS/BatBasic.html (3 of 8)10/26/2006 1:02:05 PM

DDB: DOS Batch File Tutorial

PATH STATEMENT Next, modify your path statement as found in your "AUTOEXEC.BAT" file to include this new directory. I suggest placing it near the start so that DOS will find and execute your batch files quicker by not having to search through unnecessary directories first. I have my "Batch" directory placed second right after the C:\DOS directory. So the path statement would read in part: PATH=C:\DOS;C:\BATCH; and so on. It's also important that the BATCH directory be before any program directories. This is because any program executable with the same name as the batch file will initiate first, bypassing your carefully crafted batch commands. After saving this file, type "C:\AUTOEXEC" and press "ENTER", or reboot to initiate the change.

TEXT EDITOR After the AUTOEXEC.BAT completes, select a text editor to use in writing each line of your file. The only thing required is that it be able to save these files in plain DOS (ASCII) text. This can be done by all text editors such as DOS' Edlin and Edit, Windows Note/Wordpad, and a host of independent editors. Word processors like Wordstar, WordPerfect and MS Word also have the capability as long as you save the document in ASCII (plain text). I personally like to use a little text editor called "Tiny Editor" by Tom Kihlken. Type the files into your text editor as you see them here. DOS is not case sensitive except in certain instances, which will be noted when necessary. Otherwise, one may type all upper, all lower, or some combination. I prefer to use allcaps for this purpose, except for certain instances, or when case sensitivity is an issue. Alternatively, you may screen capture or download this page and edit everything out but the batch file(s) you wish to use. When finished, name the file as seen here or use some other name that you'll remember and be able to associate with the batch file and what it does. Be sure to stick to DOS file-naming conventions of up to eight characters before the dot and using a ".BAT" extension so that DOS will know it's a batch file and run it as such. Newer DOS versions allow longer file names but I don't recommend their use. Besides causing extra typing in order to run the file, if you decide to use it yourself on an older system, or pass it on to someone using an older DOS version, the file may not run.

NAMING Recalling the file-name conflicts I touched upon above, you should not name your batch file the same as any other file which is on your computer, if possible. Otherwise, whichever comes first during a DOS path statement search, will execute. This is unless one is in the directory in which the given file resides. In that case, the current directory's file takes precedence. There may be some cases where one wishes to use a batch file with the same name as another file, but to eliminate unexpected results, it's generally best to not use files with the same names. For programs, however, I do tend to use the same name as the given program's executable unless it is a long name. This is not generally a problem because I have no program directories in my path and never manually run programs from their own directories. Finally, remember to place your new file into your "C:\BATCH" directory.

Once you understand the basics, we'll discuss some improvements to make the file do more things with the same simple keyboard command. This technique will be followed throughout these lessons.

YOUR FIRST BATCH FILE: Let's select a task that involves only a few steps. I use WordPerfect a lot, so we'll do that one first. Ordinarily, without a batch file, one would log onto the appropriate drive, change to the WordPerfect directory, and then issue the command to start WordPerfect. A batch file will do this for you automatically. As you will see, each main line in the batch file emulates what one would ordinarily type when at the command line. I'll first show you the complete batch file and then explain what each line does. The following assumes that you are using WordPerfect 6.0 and that it resides in a WP60 directory on the `C' Drive. Change these parameters, if they differ from your word processor and directory. Note that the indents shown for each example are to make them stand out in your browser. You don't need to indent the lines in the batch file itself unless you want to use them as part of your batch file layout. Indents made by tabs or spaces are ignored by DOS during batch file execution. :: WP.bat :: Runs WordPerfect :: @ECHO OFF

http://www.chebucto.ns.ca/~ak621/DOS/BatBasic.html (4 of 8)10/26/2006 1:02:05 PM

DDB: DOS Batch File Tutorial

C: CD\WP60 WP The first line is the batch file name. This is useful to remind anyone looking at the file which one it is. The second line is a title remark that tells what the file does. Note the use of twin colons. DOS will not execute any batch file line with twin colons in front of it, nor display it on the monitor screen. After seeing the second colon, DOS ignores anything following and goes to the next line. That is because the colon is an illegal label character. With regards to batch files, a DOS "label" is a word or a series of numbers/characters used to identify a part of a batch file. Some people use a single colon to place remarks in a batch file, but since DOS uses this to identify a label and the line will be read, I suggest it not be used, except of course, as a label precursor. (You'll see more on labels in the tutorial's examples.) Alternatively, others will use "REM" which is short for "Remark". DOS does not execute these lines either, but it does read them, slowing things down. This is not a problem with today's fast processors and hard drives, but can make a difference on slower computers when the batch file is long and contains a lot of remarks. Regardless, my philosophy is that anything which speeds things should be used. I must mention there is a also a problem with "REM" lines which contain redirectors and piping, which I won't get into here because that is beyond the scope of this tutorial. I simply recommend the double colon as the best symbol for placing remarks/comments into a batch file. Getting back to the example batch file, for the third line I employ the double colons with the remainder of the line blank. It is used as a separator between the title and the file itself. Although they do not affect the running, or contribute to the mis-running, of the batch file, they should be used here because DOS will display an unnecessary prompt on the screen for any blank line before an "Echo Off" command. The fourth line is required to tell DOS not to display the succeeding lines on the screen, and the "@" symbol tells DOS not to show the line itself. The first command in typical batch files usually is "Echo Off". "Echo" means that the keys struck on the keyboard are "echoed" (displayed) on the screen. Since a batch file is just a series of commands, what is typed in the file would be echoed on the screen as DOS executes each of those lines, just as though you were typing each instruction at the command line. However, in most cases, the user is only interested in the end result and does not need to see everything that DOS is doing. Using the "Echo Off" command means each command issued by the file as it works toward completing the requested task will not appear on the screen. Again, if you wish even that line to be hidden, add the "at" sign ( @ ) before it, as in the example above. Next is a blank line. DOS ignores blank lines (if they occur beyond an "Echo Off" command), and executes the following line immediately. I insert such lines to separate the various steps the file does to complete its assigned task. It makes no difference to the running of the file but does give each part its own space, making for better readability. This is important for lengthy, complicated files, especially if you go back into them a long time after they were written. The succeeding line tells DOS to go to the `C' drive. This is important if you are on a floppy, CD-ROM, RAM, or another hard drive, yet wish to start word processing immediately. The next line changes to the WP60 directory and the final line tells WordPerfect to start. There you have it. Save that as "WP.BAT" in your BATCH directory. Now to start the program from the DOS prompt, type "WP" from any drive. If it works, you may now remove "C:\WP60;" from your path statement, making for a more efficient running of DOS. Since the batch file changes to the correct directory for WordPerfect, DOS does not have to search the path to locate the WP executable file. Since it no longer has to search, having WordPerfect in the path statement is unnecessary. The fewer directories through which DOS must search, the faster it locates what it wants, resulting in a more efficient, and thus faster, operation of your computer. Note that you should be sure you have told WordPerfect where its files are via the program's set-up section. Removing the WordPerfect directory from the path may result in some error messages while running WordPerfect if this has not been done. Typically, pressing Shift-F1 will display the set-up screen in WordPerfect.

IMPROVEMENTS: "Can this file do more work for me?" I have a screen that is my opening one after boot-up and it's the one to which I always return after exiting any program, save for a few. It's my desktop, which for me, is the `C' drive root directory with a specific custom prompt and a directory listing using an after-market program called Color Directory by Loren Blaney. It gives a wide-format display of the contents of the directory with sub-directories and file types in different colours. You could display the directory contents by incorporating DOS's "DIR" command, if you wish, but Color Directory dresses up the screen and makes it easier to discern file types. To return to this example desktop after exiting Wordperfect, the screen must be cleared, the `C' Drive root directory must be returned to and the Color Directory listing displayed. Now, we could add these commands to the end of the above batch files, but since I add it to most batch files I write, it's a better idea to make this its own batch file and then have each initial batch file refer to this new one as part of its list of commands. It will save typing those commands into every batch file and should you decide to change your desktop, only this one specific file needs to be altered, because all others will refer to this one.

Let's write the new file first:

http://www.chebucto.ns.ca/~ak621/DOS/BatBasic.html (5 of 8)10/26/2006 1:02:05 PM

DDB: DOS Batch File Tutorial

:: CLR.bat :: Clears Screen and Returns to the DeskTop :: @ECHO OFF C: CD\ CLS C:\BATCH\DR.BAT We have already discussed the opening information lines, the "@Echo Off" command, the double colons and blank lines, so I will skip them here and for all future discussions. Lines 6 & 7 return one to the `C' Drive's root directory. Line 8 is DOS's "Clear Screen" command. It erases everything on the screen except for the prompt and any special screen settings contained within, such as certain coloured text & backgrounds, among other options. The final command is for the Color Directory. It's also a batch file (called "DR.BAT") with one line (after "@ECHO OFF") that directs DOS to the directory containing the Color Directory program's executable file. The full path is given so that DOS will not have to search any directories for the "DR" batch file. I have named this batch file "CLR.BAT", which stands for "Clear Screen, Return to Desktop". I can now add this "CLR" command to the end of any batch file to clear the screen and return me to my desktop automatically after completing any task. The reason this works is that after a DOS batch file hands control over to a program, when the program finishes, it returns the reins to DOS which remembers that there is another line in the batch file to run. It therefore runs the "CLR" batch file upon exiting a given program, which then returns me to my desktop.

Using our word processing example farther back, the file now looks like: :: WP.bat :: Runs WordPerfect and Returns to the Desktop :: @ECHO OFF C: CD\WP60 WP C:\BATCH\CLR

FURTHER ENHANCEMENTS: "Could this file do yet more work?" The preceding improves the original "WP.BAT", but we can enhance it further through WordPerfect's built-in command-line options. I won't get into an explanation of those options here as that would be outside the subject of this article. You'll need to consult the WP manual to learn more; however I'll give you an example of some. I like to have WordPerfect use expanded memory (/r), and have it place & read its overflow, buffer, & temporary files on my RAM (F) Drive (/d-) for faster operation and the freeing of lower (conventional) memory. With these options, the batch file then becomes: :: WP.bat :: Runs WordPerfect and Returns to the Desktop :: @ECHO OFF C: CD\WP60 WP /r /d-F:\TEMP C:\BATCH\CLR Note to use this, you will have to create a TEMP directory on your RAM drive upon each bootup. Add such a line to your Autoexec.bat. If you don't make use of a RAM drive (and you should!), create the TEMP directory on your C http://www.chebucto.ns.ca/~ak621/DOS/BatBasic.html (6 of 8)10/26/2006 1:02:05 PM

DDB: DOS Batch File Tutorial

drive and point WordPerfect there. It won't be as fast, but there will be fewer files for WordPerfect to look through when it doesn't place its temporary files in its own, already crowded directory. WordPerfect, like many DOS programs, can also load a document upon startup. You might like to use a letter form (which is called a "template" in the word processing world) for most letters you type. Its name might be "LETTER. FRM" and it would likely include your name & address as a header, along with various options you like to use. (Never use the extension "TMP" for a "template" file as that may be used by DOS and its programs to designate a temporary file). If it's sitting in a subdirectory of WordPerfect's called "Template", the third-last line of the batch file becomes: WP /r /d-F:\TEMP C:\WP60\TEMPLATE\LETTER.FRM

This batch file version might be called "WPLET.BAT". Now that you have this basic batch file, a large stable of batch files could be written to run WordPerfect and load any number of different documents. In addition, you may also have your batch file start a WordPerfect macro using the "/ m-" switch. A macro is a type of batch file used by many programs. You write it much as you would these DOS batch files except a ".WPM" extension is used in Wordperfect. (See your word processor's manual for details on macro writing.) To run a WordPerfect macro from your DOS batch file, enter the macro option after any other options for WordPerfect. Now, with one simple command, you could start the program from anywhere within DOS, load a letter template, and then have the WP macro load the current project on which you are working into that template. The line might then look like: WP /r /d-F:\TEMP C:\WP60\DOCUMENTS\LETTER.FRM /m-PROJECT.WPM All this is from one simple batch file that might be named "WPCP.BAT" for "Wordperfect, Current Project". What a huge amount of typing saved! Can you imagine typing those lines every time you wanted to work on that particular project? Plus, with this method, you get the options you want, tailored to each type of program and document upon which you are working, without having to reconfigure a program's defaults. Note that these command-line options are done using "slash switches", that is a slash followed by a letter or character string. For more on this aspect of DOS, see DOS Switches, elsewhere at this website. Making use of command line options (switches) is a very powerful way to run programs.

When you are finished with one current project, rewrite the WordPerfect macro to refer to your next project. This way, the same DOS batch file will always start your current project regardless of what it is. This saves having to remember, or look up, a new batch file name for each new project.

If you always load the current project into the same template (which would be typical), after the first time, save the project and template as one, under the project name. Then redo the batch file to have WordPerfect load the project file directly, rather than having it load a template and then run a macro to load the project into that template. It saves a step and gains a small speed advantage.

In closing, a batch file may be made up from any system prompt commands. This allows one to funnel multiple-line typings down to just one simple command.

Batch files put one on the way to power-user status! As you go through the example pages at this website, you will often see the same batch file fragments used over & over. As you become proficient at writing them, you will begin to build a DOS toolbox of batch techniques that can be mixed & matched to achive the desired results so you can then automate any task that you attempt.

http://www.chebucto.ns.ca/~ak621/DOS/BatBasic.html (7 of 8)10/26/2006 1:02:05 PM

DDB: DOS Batch File Tutorial

http://www.chebucto.ns.ca/~ak621/DOS/BatBasic.html (8 of 8)10/26/2006 1:02:05 PM

¦ Batch File Examples ¦

¦ Advanced Batch Files ¦

¦ Batch File Tips ¦

Return to the Main DOS Page

Obtain 500+ Batch Files

Go to Richard Bonner's Home Page

DDB: DOS Fallacies

Doctor DOS Betamax's

DOS FALLACIES

Many people believe DOS to be an antiquated, useless operating system with few features and capabilities. This is clearly wrong. Those persons are usually not aware of the full capabilities of even older versions of DOS, nor of the many after-market utilities and upgrades currently available. They do not realise that DOS was not the limiting factor in most cases; it was the computer's hardware that was not up to the job due to the lack of processor speed, inadequate memory, a slow video card, etc. Few too, are aware of the power of the latest DOS versions and the newest applications obtainable.

Here are twenty-nine fallacies typically held about DOS, and my comments: INFORMATION BELOW MAY NOT BE REPRODUCED WITHOUT PERMISSION FROM THE AUTHOR ©



1/ "Those Using Exclusively DOS are Luddites." This popular misconception comes from the wrong idea that DOS has never progressed beyond a simple text interface. Hobbiests of classic computers aside, modern DOS users are far from being Luddites. They want the latest capabilities being used by others to be available with their chosen operating system, too. Today's DOS user wants a speedy hard drive, fast & capable video, quick communications and the latest software.

http://www.chebucto.ns.ca/~ak621/DOS/DOS-Fal.html (1 of 8)10/26/2006 12:56:14 PM

DDB: DOS Fallacies

He or she uses DOS because of a preference for having the great power and unpretentiousness of this established and stable operating system. They want the direct access DOS provides and the complete control it affords without the hand-holding and excessive supervision so prevalent in other systems.











2/ "DOS is Dead." If this is true, why does every modern operating system either come with DOS or have a DOS emulator? DOS by itself is available from a variety of manufacturers not bundled with any other operating system. The latest versions of 4DOS and DR-DOS just came out in 2004 and a DR-DOS 7.X upgrade was released in 2006. (Note that "DR-DOS" is pronounced "Dee-Arr DOS".) 3/ "DOS is Obsolete." This is a ploy by computer companies to make you spend money to switch to their systems. Unknown to many computer users, DOS has continued to be developed. Many applications are available to do the latest computing assignments, as are updates to DOS and its utilities. Companies other than Microsoft continue to market new, modern versions of DOS. As mentioned in Fallacy #2, the major operating systems include DOS or a DOS emulator: Linux, Macintosh, OS/2, Unix and Windows. In particular, users of Microsoft's Windows can use the included DOS or DOS emulator to automate Windows functions. 4/ "DOS is Slow." This seems to come from those persons that only ever used DOS on older, slower computers. Regardless, DOS is much faster than a GUI (Graphic User Interface) on an equivalent system. This is because without all those icons and excessive colour depth to describe, it occupies a smaller amount of RAM and requires much less processing power to do the same job. As a consequence, it also takes up far less room on your hard drive. DOS programmers tend to try to condense their code, in part so their programs will run quickly on older computers with older DOS versions. All of this means a system that runs very fast, and ultra fast on recent processors. 5/ "DOS Users Can't Run Programs Larger than 640K (655,360 bytes)." Many DOS programs have long been able to make use of Upper Memory along with Expanded & Extended memory. Two of the best known ones to first do so were Lotus 1-2-3 and WordPerfect. All newer programs are designed to make use of memory outside of the conventional (lower) range. 6/ "DOS Users Can't Load More Programs into Memory than will fit into 640K (655,360 bytes)." This is related to Fallacy #5, above, and has to do with loading programs at startup so they are at the ready when a DOS user needs them. By using DOS' RAM drive capability, one may set aside an area of memory in which to place programs. Although these programs are not active, they are in memory and can be made active in milliseconds due to the speed at which memory operates.

http://www.chebucto.ns.ca/~ak621/DOS/DOS-Fal.html (2 of 8)10/26/2006 12:56:14 PM

DDB: DOS Fallacies

A typical purpose is to have overlays, library files, batch files, etc. on the RAM drive so they are instantly accessible. In operation, it is indistinguishable from a multitasking setup except that the programs may not operate at exactly the same time. (See Fallacy #7, next.)



7/ "Only One Program at a Time May Be Used." This is related to both Fallacies #5 and #6, above. Firstly, one may run as many programs as can fit into 640K (655,360 bytes) and access them through each program's "Suspend" capability. Secondly, any program that makes use of Expanded and\or Extended memory can typically be run in tandem with other programs. This is the case for most all modern DOS programs. They run in protected mode, as do Windows programs. Thirdly, there are shell, or stand-alone task switcher programs and multi-taskers that allow as many applications of any type to be run as there is total available memory. These have been available since the 1980s.





8/ "No More than 16-Bit Programs Can be Run in DOS." DOS can run 32-bit programs because these programs are isolated from the 16-bit operating system. They are designed to run in protected mode via the "DOS Protected Mode Interface" (DPMI) or other similar interfaces. This bridges the 16/32 bit systems by operating as 32-bit within the program, but operating as 16-bit when DOS operations are required, then switching back to 32 bit for program operations. Thus, they run as do programs under systems such as Windows, and they can run in Extended Memory. 9/ "DOS Can't Handle Long File Names." DOS 95 and newer can handle long file names, and for those with other versions, LFN interfaces are available or are built in. Before that, WordPerfect could handle internally-generated long file names at least since version 5.1 which was released in 1990. Even before that, after-market programs allowed DOS itself to use & recognise long file names. However, in a command-line setup, few want to use too long a file name because of the extra typing involved. Also, directory listings must be in a single column or, if in multiple columns, not show all file information due to the room required to display just the file names. Regardless, long file name capability is available to users of older DOS versions as discussed above, and major new versions of DOS support long file names.



10/ "No New Stand-Alone Versions of DOS have come out Since 1994." No new Microsoft stand-alone versions have come out since 1994. Many other individuals and companies have brought out new DOSes and upgrades for them in the ensuing years. The latest versions of 4DOS and DR-DOS were released in 2004 and a DR-DOS upgrade was released in 2006. Still others

http://www.chebucto.ns.ca/~ak621/DOS/DOS-Fal.html (3 of 8)10/26/2006 12:56:14 PM

DDB: DOS Fallacies

continue to come out regularly. (Note that "DR-DOS" is pronounced "Dee-Arr DOS".)





11/ "No One is Developing New Programs for DOS." Hmm, odd then how I have Compact Disc & Media Players, a Graphic Internet Browser, a USB Flash Drive, program updates that are less than two years old, and utilities of which ages are only months old. Yes, programmers still write for DOS because it's easier to do than for equivalent GUI operating systems, and because DOS is so flexible. In particular, a lot of new DOS programs are coming out of Europe. 12/ "DOS is Hard to Learn." Basic DOS is easy to learn. GUI promoters want you to believe its hard. An operating system is meant to interface the user with the computer. Included in this are program starting, directory maintenance, and file creation, deletion and moving/copying. To start a program, type its name. To make a directory, use the "Make Directory" command, which has been conveniently abbreviated to "MD". Removing a directory means using the "Remove Directory" command, again abbreviated (RD). To copy a file, use COPY, to move a file use MOVE. A file's attributes are added, removed or changed using the "Attribute" command (ATTRIB); two files may be compared using the "File Compare" command (FC); to find a file containing specific text, use FIND, and so on. To create a file, use the DOS editor called - guess what? - EDIT. Could it be any more logical and simple?



13/ "There's Too Much Typing in DOS." There can be, but I am amazed that these same complainers are willing to type an eight-screen e-mail to a co-worker seated only a few metres away. In DOS, batch files and keyboard macros eliminate most typing. There are also programs that allow use of a pointer device such as a track ball, touch pad, light pen, graphics tablet, or mouse to run the commands without one having to type them. Even at the command line one may use the ` * ' and ` ? ' wildcards to reduce typing. Plus, many versions of DOS will auto-complete file & directory names, and there are utilities and upgrades for those using DOS versions without that capability. Many of these feature command recall. This allows one to select from a list of previously-typed commands or to type the first few letters and allow the program to auto-complete the rest.





14/ "DOS Users are Stuck with a Bland Desktop." DOS, using ANSI.sys or equivalent aftermarket replacements, can easily be programmed for colour and icon setups. There are also dozens of menu and desktop programs available if one wishes to dress up DOS even further than ANSI. sys capabilities. This includes the display of background images. 15/ "Newer Printers and Modems are not able to be Used." There are Internet sites that have

http://www.chebucto.ns.ca/~ak621/DOS/DOS-Fal.html (4 of 8)10/26/2006 12:56:14 PM

DDB: DOS Fallacies

newer drivers available. In fact, many manufacturers' sites provide them.





16/ "DOS Does Not Support Large Hard Drives." This is mainly a BIOS problem. Newer ones support larger drives if they show a setting for "LBA" (Logical Block Addressing). Although older DOS versions also imposed a limitation, starting with MS-DOS 5 (1991), support for drives larger than 528 megabytes was introduced. As well, after-market programs allowed for use of multi-gig drives with older BIOS and DOS versions. Versions of DOS since the late 1990s offer multi-gig drive capability built right in. 17/ "DOS Cannot Multitask." DOS could actually multitask before Windows via DOS extender technology. In fact, when Microsoft put multitasking capability into Windows, they used DOS extenders to do it! They did not put that capability into their MS-DOS, presumably so as to not compete with Windows. As for DOS itself, DOS 4 (Europe release) featured multitasking, but was not marketed in North America. Today's latest versions allow true multitasking from within DOS. In addition, programs such as DESQview and the SWORD desktop allow most any DOS system to multitask. However, since one can only input data or interact with one program at a time, most "multitasking" is really about having programs at the ready in memory, which DOS is capable of anyway. To that end, there have already been programs that allow task switching by DOS since the 1980s.



18/ "DOS Can't Display More than 16-Colour Images." This is one of the harder fallacies to overcome in people's minds. Most DOS versions place colour on the screen through ANSI.SYS or equivalent, and those colours are limited to 16 shades at any one time. Now, these 16 colours may be selected from a larger palette (typically 256 colours), so that DOS users may choose which 16 shades to use. That capability is available through after-market programs or is built into some versions of DOS. Regardless, this is only for the DOS display itself. If a user has a shell, or any other DOS program, it may be, and often is, written to display a wider colour range. These programs will present themselves, or any graphic image, in the colour depth of which that program is designed, and of which the video card and monitor are capable. Thus, DOS users can see 24-bit (~=16 million colours) pictures on their screens, if the system is capable. Some can display up to 32-bit targa (.tga) very high quality graphics, which are suitable for large-scale photo reproduction.



19/ "DOS Cannot Do Graphics." or "DOS is Text Only." This fallacy probably came about because either people were used to seeing DOS on slow, memory-lacking systems, and/or people assumed that not much could be done with graphics in the 640K (655,360 bytes) of RAM limit. However, DOS graphics programs are written to place the program kernel in lower memory while overlay and image files are placed into expanded or extended memory. Others are 32-bit,

http://www.chebucto.ns.ca/~ak621/DOS/DOS-Fal.html (5 of 8)10/26/2006 12:56:14 PM

DDB: DOS Fallacies

protected-mode programs that run in extended memory. There are many DOS Viewer, Image Manipulation, Paint, and CAD (Computer Assisted Design) softwares available. Modern fast computers make DOS graphics a reality, just as they do for other major operating systems.







20/ "DOS Cannot Do Multimedia." This is related to Fallacies #18 & 19. DOS multimedia programs are available that can do animations and audio. Many can run media presentations from other systems such as MacIntosh and Windows. The best of these programs use 32-bit, Protected Mode and thus, can run in extended memory. This is best for resource-hungry presentations such as MPEGs and AVIs. 21/ "DOS Users Can't Access More Than 64 Mb of RAM." There have been memory extenders around for over a decade. They allow DOS applications to access gigabytes of memory, although for most users, 64 Mb will run 10 or 15 applications easily because DOS programs are so much smaller than their GUI equivalents. 22/ "DOS is Not Y2K Compliant." I suspect this was started by computer companies and perpetuated by computer sales stores in order to get people to spend money to upgrade. Computer companies certainly had time to fix the problem, but software as late as 1998 still had not been changed. Modern DOS versions are Y2K compliant but older DOS versions are actually not affected by Y2K problems - if one uses four-digit years. I have not checked every DOS version, but there does not seem to be a problem with any DOS version with which I have come in contact, nor is it with most programs I use - and some are as old as the late 1980s. According to Microsoft's DOS manuals, using a four-digit year allows dates through 2079 for MS-DOS versions 3.x & 4.x, and through 2099 for Microsoft versions 5 & up. It appears that PC and DR DOS follow this, as well. The only difficulty seems to be with those few programs that keep track of the year using just two place positions. They were programmed to add "1" to the last two digits of the previous year's date on January 1st. Thus December 31, 1999, which showed as "12-31-99", became "0101-100" on January 1st, 2000. Since there were only two place positions available for the year, it was truncated to "10". ProComm Plus by Datastorm Technologies exhibits this. Even so, it does not affect the running of the program at all, and files created under it show the correct year in a DOS "DIR" listing . So any of you who are suffering with old dates because your DOS version does not have Y2K compliancy built in, or are trying to lessen the confusion by using a pre-2000 year (such as 1995, which was the last 20th-century year to coincide with 2006), then simply use the DOS "DATE" command and enter the month, day, and four-digit year. You'll be home, free!



23/ "DOS Can't Do GUI." DOS is a TUI (Text User Interface) system, so by nature, it doesn't

http://www.chebucto.ns.ca/~ak621/DOS/DOS-Fal.html (6 of 8)10/26/2006 12:56:14 PM

DDB: DOS Fallacies

interface with computer programs via point & click. However, that's not to say that someone using DOS cannot interface via GUI (Graphic User Interface). DOS graphic shells have been around since the mid 1980s for those whom want the advantages of the command line but may still wish to click icons to get some computer work done. In fact, the most famous graphic shell for DOS is Windows.













24/ "One Can't Access The Graphic Internet with DOS." There have been DOS graphic browsers since the mid 1990s, with Arachne probably being the best of them. DOS users may access The Internet via dial-up, cable modem, or high-speed wireless, and with features including add-ons and plug-ins. 25/ "The DOS Screen is Stuck with That One IBM VGA Font." DOS users can select from scores of different fonts supplied by a variety of programs. A simple program loads the font at bootup or from the command line. DOS users have had this capability for over a decade. There is even a program that allows multiple fonts on the DOS screen at the same time. 26/ "Hardware has Adopted the USB Standard, Which DOS Can't Support." One only needs a computer that is USB (Universal Serial Bus) capable and DOS USB drivers to be able to operate USB devices under DOS. This is no different from any other operating system. 27/ "DOS is Unable to Connect to Wireless Networks." As above, all that is needed is the proper card, such as a WaveLAN or ORiNOCO card, and suitable software in order for DOS to be able to connect to any wireless network. This has been available for over a decade and a half. 28/ "DOS Can't Display Modern Characters." This is mainly related to the "Euro" currency symbol, but applies to others. PC-DOS 2000, DR-DOS 7.02, and other new versions of DOS are "Euro" compatible. For those with older versions, drivers are available that will allow DOS programs to display it and other symbols. 29/ "The DOS Batch Language is Brain Dead." As with so much else regarding modern DOS, the DOS batch language has continued to be developed. Even Microsoft has improved the DOS batch language, although for DOS only as it runs under Windows. DR-DOS has had better batch file commands than Microsoft's DOS since at least version 6, which was released in 1991. 4DOS has had improved batch file commands for over a decade. The most recent version (2004) has a myriad of new commands and switches that allow the user to do very complicated DOS chores that were once only the realm of higher programming languages. For DOS itself, 4DOS, DR-DOS, and other versions have new batch commands and features. Plus, many third-party software authors have improved upon the batch language. As well, many of them have also devised batch utilities to further extend DOS' automation capabilities.

http://www.chebucto.ns.ca/~ak621/DOS/DOS-Fal.html (7 of 8)10/26/2006 12:56:14 PM

DDB: DOS Fallacies

Many of the above mentioned capabilities are discussed at websites. They usually contain the programs, utilities, and drivers used by DOS to overcome the imagined limitations detailed here. Get to them from this site via DOS Websites Go to the Main DOS Page. Go to Richard Bonner's Homepage.

http://www.chebucto.ns.ca/~ak621/DOS/DOS-Fal.html (8 of 8)10/26/2006 12:56:14 PM

DDB: DOS Switches

DOS COMMAND-LINE SWITCHES

Doctor DOS Betamax's

A Short Discussion of Command and Program Modifiers

New users of DOS are often mystified by command switches or don't bother to fully explore their uses. This is a loss and can contribute to frustration at the command line because not using them means more work. The following is a short discussion of what switches are and why they might be used. Afterward, some pitfalls and tips will be given. Deep detail will not be gone into. If you want to know more about using certain switches and how they modify a command, see your DOS manual's Command Reference or On-Screen Help. Be aware that not everything here may work for your version or manufacturer of DOS.

INFORMATION BELOW MAY NOT BE REPRODUCED WITHOUT PERMISSION FROM THE AUTHOR ©

What is a Command Switch? When one issues a DOS command, it does the work requested by the user as per the design of that command. Thus, DIR places a list of file(s) in the current directory on to the screen, DEL removes the file(s) typed after the command, COPY makes a duplicate of the given file(s), and so on. Each command

http://www.chebucto.ns.ca/~ak621/DOS/DOS-Swit.html (1 of 7)10/26/2006 1:03:12 PM

DDB: DOS Switches

works in a defined way. However, it is possible to modify most commands so as to tailor them to do more specific things, or to display the result in a different way. This is accomplished through "switches". A switch is simply a command modifier, making it somewhat akin to "Properties" in Windows. It is a single letter, word, or word abbreviation that follows a command or its parameters. A switch is used to make each command more versatile by changing its workings, display method, or how it acts upon some parameter.

Why Might I Need, or Want to Use, a Command Switch? A command issued with no switches will work as defined by its default operation. However, the user may wish to have it work differently, or perhaps requires a different outcome and/or display than the command alone provides. Switches make a command more powerful and can narrow its focus to do more specific tasks. With the latest updated DOS commands, switches are even more numerous and important. Programmers have included more and more with each new command. The old XCOPY of a decade or so ago had only 10 switches; XXCOPY alone has over 160! That makes XXCOPY far more powerful than XCOPY. Making use of these will escalate one to power user status and allow far more work to get done with less input. Another usage is to configure, or even make direct use of, a software program right from the command line. (See farther on.)

How Do I Use Command Switches? You use them by first determining what switches are available and their possible applications. This is found in the DOS manual's Command Reference, but is also available on screen in DOS versions after MS-DOS 5. (See the Tips section, farther on.)

SYNTAX

The syntax, ("sentence" structure) for using switches with DOS and most of the programs written for it is the command, followed by a space, a forward slash, and then a letter, word, or character string. If parameters such as file names and/or directory names are required, most times the switches go at the end of the command-line string. However, some commands such as MOVE, require the switches to be placed right after the command. In addition, some switches allow the use of a `-' (minus) sign to reverse the action of a switch. (See the Tips section farther on.) Check the manual or on-screen help to see where switches are to be placed and if they may be reversed by using a minus sign.

http://www.chebucto.ns.ca/~ak621/DOS/DOS-Swit.html (2 of 7)10/26/2006 1:03:12 PM

DDB: DOS Switches

CASE SENSITIVITY

Although being used in DOS, case sensitivity is not usually a problem with switches. However, it's possible that it might be an issue with some versions, in some cases. So consult the manual of your DOS version to see if case makes a difference. Realise, DOS itself can differentiate case when a user types at the keyboard, but most switches allow for this. So generally, the switch itself may be typed in upper or lower case (or mixed case for switches of more than one letter.) However, it is possible that there may be some exceptions. See the manual to check for sure. The exceptions for letter case lie with some DOS programs. They differentiate between upper and lower case switch letters. PKZIP is one such program. While studying a program's manual or on-line help, keep an eye out for this possibility. As a related aside, some command-line history/recall programs, such as Toddy or DR-DOS's HISTORY, can be case sensitive when it comes to command recall or file/ directory auto-completion. They may not display any previous commands (that could include switches) if the case does not match that which is typed at the keyboard. (Note that "DR-DOS" is pronounced "Dee-Arr DOS".) Remember then, when using switches with DOS programs, to note if the case needed to perform the work you require makes a difference.

MULTIPLE SWITCH USE

In some instances, more than one switch might be employed to narrow the focus of a given command further yet. As an example, if there are many files in a directory and one issues a DIR command to see a list of those files, they will scroll off the screen and the first ones will not be able to be seen. If there is a need to see those files, then instead of just typing "DIR", issue "DIR /P". That will Pause the screen when it fills so that you may read the first group. Then, striking a key allows the next screen-full of file names to be seen. This continues until all file and directory names have been listed. A multiple-switch example for this same command is if one wishes to see an alphabetised list and still pause the screen. Then the command is modified as "DIR /ON /P". This will give an Ordered list by file Name, Pausing after each screen fills. Note that the syntax requires a space between the switch & the command, but one between each switch is optional. For clarity and easy readability, I recommend placing spaces between switches, especially when composing batch files.

OTHER SWITCH METHODS

http://www.chebucto.ns.ca/~ak621/DOS/DOS-Swit.html (3 of 7)10/26/2006 1:03:12 PM

DDB: DOS Switches

In addition, switches may be employed through two other methods. One is by using DOSKEY macros, or in 4DOS(*) they are called "aliases". This is beyond the current scope of this article, but basically, one may use a DOSKEY macro or an Alias to define the default methods of a given command. Thus, one may invoke the command without typing any switches because it will be intercepted & run by DOSKEY/4DOS and automatically incorporate your pre-selected switches. Secondly, one may use the DOS environment to set switches for some commands. Again, this is beyond the scope of this article, but it works similarly to DOSKEY. However, it is not available for every command. This method employs the SET environment command. One may specify switches for the DIR, COPY, XCOPY and MOVE commands. They will be issued automatically whenever any of those commands are given. Perhaps later on, I will expand on both these methods. In the meantime, learn the basics of switches so you'll be ready should you choose to make use of either of these alternate ways in the future. (*)4DOS links may be found in DOS Websites.

What about Command-Line Switches Used by Programs? Most DOS programs have at least a few switches, while many have an extensive array available. They may be used to load one or more files upon startup, or to run the program in a particular configuration, usually including the ability to override one or more of the default configuration parameters. In addition, some programs and/or their utilities may be run directly from the DOS prompt without ever having to see or interface with the program on the screen. It can be a very powerful way to get work done faster and with fewer hassles. This is especially true if one places various switches into a series of batch files that can be run at will from anywhere within DOS. They can do file conversions, set up graphic slide shows, do disc maintenance or testing, etc. - all right from the command line. All the above can be implemented and varied through the use of switches, so it's well worth it to seek out their capabilities. As with DOS commands, one issues the program's or utility's executable followed by one or more of its switches. Note though, that some programs use a different switch syntax. Some use a `-' (dash) to signify a switch, and some prefer they be located right after the program name, as opposed to being placed at the end of the command string. Read the manual.

Are there any Problems I Might Encounter? SWITCH INCONSISTENCIES

Aside from the issuing of a switch other than the one intended, a common problem results from inconsistencies among DOS commands using the same letter for a switch. For example, in different commands, the `/S' switch can mean Case "Sensitive", "Scan", "Share", "Sort", "Subdirectory, "System", http://www.chebucto.ns.ca/~ak621/DOS/DOS-Swit.html (4 of 7)10/26/2006 1:03:12 PM

DDB: DOS Switches

and probably others. `/P' can be "Path", "Pause", "Permanent", "Print", "Prompt", and so on. Many other commands use similar switch designators, but which mean different things from one another. Not only that, some switch meanings changed from version to version of a command. This confusion can lead to errors when issuing a command from memory. The 4DOS(*) COMMAND.com replacement has addressed this problem and is much more consistent from command to command. One way around this for those using DOS versions that are inconsistent, is to spend time learning those differences for the commands you use that would be affected. A better method is to place commands and associated switches into batch files. These files would be named by you in such a way as to keep their purposes straight in your mind. This method also eliminates the possibility of mis-typing or mis-using a switch. Once you have tested & tweaked the batch file, it will always run with the switches as you intended. See Batch File Basics elsewhere at this website for help writing batch files. (Note too, that one might also use DOSKEY/Aliases or environment variables, as mentioned farther back, to achieve the same purpose. However, they are not as versatile as batch files, and thus, are not as capable when it comes to complicated operations.) (*)4DOS links may be found in DOS Websites.

SWITCH INTERFERENCE

Getting back to possible problems, another may occur if you use switches that are mutually exclusive or that interfere with one another. You might be lucky and just get an error message, but what may also happen is that one or more of the conflicting switches will be ignored. Thus, the command will likely not work as you intend. Another problem that comes to mind is in using switches with which one is not entirely familiar. Some switches with some commands can also be dangerous if their use is not fully understood. Examples are the automatic overwrite switch (/Y) in some DOS versions of the COPY, MOVE, XCOPY, etc. commands. Another is the same switch in DELTREE. It deletes files and subdirectories without prompting. DEFRAG /H will move hidden files. This is a problem because DOS and some programs expect to find certain hidden files in certain locations. There are other possible problems, but you need not worry as long as you study what each switch does. Then try them on duplicate files in a test directory where anything that goes wrong will not spell disaster. Once you become confident, the test directory will be less, or completely, unnecessary. Then you'll then be a switch guru!.

Here are Some Switches Tips. http://www.chebucto.ns.ca/~ak621/DOS/DOS-Swit.html (5 of 7)10/26/2006 1:03:12 PM

DDB: DOS Switches



Use ` /?' at the Command Line. Typing a DOS command followed by ` /?' will give a quick syntax, list of switches (if any are available), and in some cases, examples for the given command. Use this as a quick refresher for commands with which you are less familiar. (Note, this is not available in all DOS versions.) As well, this usually works with a program's or utility's executable. However, be aware that some use a different switch prefix other than a forward slash. The most common of these is the `-' (dash).







Use DOS Help. Regarding DOS commands, if you are unsure of one, but want more than the `/?' can provide, hit `F1', or type & enter "Help", or "Help (command)". More detailed information will be given on screen. Again, these are not available in all DOS versions. Place Switches in Alphabetical Order. When entering a command's switches into a batch file, put them in alphabetical order. It won't mean much if you have just a few switches, but if there are many, using the same order each time makes it easier to scan a batch file at a later date and see which switches you have used. Knowing that one switch can only be seen after another means quicker scanning and faster comprehension of a given command. This is an aid to troubleshooting or when upgrading a batch file. Use a Minus Sign to Reverse Some Switches. Placing a minus sign between the slash and switch letter or character string reverses some switch actions. As an example, with some versions of the COPY command, using "/Y" suppresses the prompt that asks if you wish to overwrite a file. Using "/-Y" will always prompt you if a file is about to be overwritten. Some switch reversals are performed by placing the minus sign after the switch character. As an example, PKZIP has a switch for "Recurse Subdirectories". It's ` -r '. (Note that PKZIP uses the `dash' as its switch character.) The action of this switch is reversed by placing a minus sign after it: ` -r- 'so that subdirectories will not be taken into account. In other cases, the minus sign will reverse a sort order, or not include a selected parameter. It can also be used to disable certain things that would ordinarily be the default. Since this varies so much among DOS versions, commands, utilities, and program executables, study the manual or on-screen help to see whether a minus sign does anything at all, and if it does, exactly how it changes a switch.



Be Aware of Case Sensitivity. As discussed in this article, some commands and programs have case-sensitive switches that perform differently depending on the case used. Be sure to check if this is so when using switches or writing them into batch files. As example is PKZIP. It uses `-t' to select files based on a date equal or newer to the one types, while `T' selects files that

http://www.chebucto.ns.ca/~ak621/DOS/DOS-Swit.html (6 of 7)10/26/2006 1:03:12 PM

DDB: DOS Switches

are older than that date.





Have Batch Files Issue Switch-Modified Commands. As already mentioned, placing commands & their switches into batch files is a good method. It serves several purposes. Firstly, one need not have to remember the switches. Secondly, using a batch file removes the possibility of mistyping a switch. Thirdly, a batch file reduces the amount of typing required to get your work done. Fourthly, by making up a number of similar-name batch files, one may have a range of command variations at one's fingertips. As an example, you could have several DIR command versions. They might be "DIRW", "DIRD" and "DIRA" for directory listings in Wide format, one which shows Directories only, and one which displays files in Alphabetical order. Use a Program's Switches. Look in your software manual and at the on-screen "Read-Me" or document files for a list of switches available to use with programs. You may find innovative ways to use the program's capabilities right from the command line. Using such options in a batch file often means the program can run unattended. Thus, one may use such a setup to do a program's work right out in DOS. A good example is using a graphic program's conversion capability to change say, a .jpg image to a .bmp. Set this up in a batch file with the program's executable and its conversion switches set to your preferences. You will never have to actually start the program interface. One command in DOS, and the conversion is done from within any directory.

Using switches is yet another key on the road to becoming a DOS Power User. Learning what switches are available and how they might be used, will improve productivity, lessen frustration, and make you a true command-line wizard!.

Return to the Main DOS Page Go to Richard Bonner's Homepage.

http://www.chebucto.ns.ca/~ak621/DOS/DOS-Swit.html (7 of 7)10/26/2006 1:03:12 PM

DDB: DOS Variables

Doctor DOS Betamax's ...

DOS VARIABLES A Short Discussion of DOS Replaceable Characters & Strings

As one's usage of DOS increases along with the knowledge that accompanies it, a desire to have DOS do more will emerge. One of the first power tools many DOS users implement is the batch file. A basic DOS Batch File Tutorial may be found at this website that will get you started, but this discussion is aimed at adding a batch file utensil to your DOS tool box. What follows is information on DOS variables, what they are, and their typical usage. As with other discussions at this website, deep detail will not be gone into. If you want to know more about this aspect of DOS, see your DOS manual's section on Variables, and/or the Command Reference, and/or the On-Screen Help. Be aware that not everything presented here may work for your version or manufacturer of DOS. As well, there are some variances among DOS versions and software. What is presented here will use the most common methods. Despite differences, the concepts still hold true, and learning to use variables will be one more brick in the structure of a strong DOS knowledge.

http://www.chebucto.ns.ca/~ak621/DOS/DOS-Var.html (1 of 10)10/26/2006 12:57:22 PM

DDB: DOS Variables

INFORMATION BELOW MAY NOT BE REPRODUCED WITHOUT PERMISSION FROM THE AUTHOR ©

What is a DOS Variable? A variable is a term to which a value or character string is assigned. It consists of the name, which can be one or more letters, numbers or ASCII characters. If letters are used, they are sometimes stored in uppercase by some versions of DOS, some of the time. ASCII characters may include spaces, but there are times when spaces can cause problems. Others that can cause havoc are the percent sign ( % ), redirection signs ( > < ), the pipe symbol ( | ), and equals ( = ). It is better if they are not used until one becomes proficient at using variables. There are two related types of variables: Environmental Variables and Replaceable Character Variables. Both replace one term with another and save the information to memory, but the former's information is kept in a memory area called the "DOS Environment" until it is removed or the computer is rebooted. The latter use single characters or numbers as the names and as soon as the operation is completed, the information is typically deleted. Because the value can change, or vary, these are known as "Variables". They are usually accompanied by percent signs, but there are DOS versions that deviate somewhat. As an example, the method used by 4DOS for its built-in environmental variables is to precede them with " %_" (percent underscore), whereas DR, MS, and PC DOS all use one percent before, and one after, an environment name. 4DOS recognises this and also makes usage of it. Other DOSes may differ slightly. Some may use a `$' on either side. Replaceable Character Variables use a single percent sign or a single dollar sign before the name in all the DOS versions I have come across, but that may not be universal.

ENVIRONMENTAL VARIABLES Each consists of the Variable Name to be assigned, and the second part of it is the contents of that variable. These are placed into memory by the DOS "SET" command. This command may be used to view, add, delete or modify environmental variables. The name given to each will usually be preceded and succeeded by a percent sign ( % ) after it is stored. This might look like: SET LEVEL=2. This sets a value of `2' for the name "LEVEL". This information is stored in the environment as " % LEVEL% ". So everywhere %LEVEL% is seen, it will be replaced with the value of `2'. Using such variables means that the user need not type the contents out every time. Instead, the shortcut of %LEVEL http://www.chebucto.ns.ca/~ak621/DOS/DOS-Var.html (2 of 10)10/26/2006 12:57:22 PM

DDB: DOS Variables

% can be employed. The above example may not seem like much a shortcut because it takes more to type "LEVEL" than `2', but as you read on, the advantage shall become apparent. A variable is not only a shortcut to fewer keystrokes. If one was to not assign information as a variable and then at some point wanted to change that information, every instance of this change would have to be retyped. If there are dozens of references to that information, it would mean a lot of retyping. Using a variable to represent that information means only one change needs to be made, and that is done in the place where the variable is defined. Once one assigns the variable its new contents, all the other locations where the variable is used will automatically reference that change of contents through the variable's name. Another usage is to assign DOS variables to do specific procedures. This places information into memory so that DOS may make use of it to perform a dedicated task - one typically assigned by the user. Barring a reboot, these variables may be kept in memory indefinitely and used by DOS or its programs as needed during any computer work session. If one wishes to use them in every session, they must be included in a start-up file which can be used to place them in memory during boot-up.

THE ENVIRONMENT All of this is held in the environment. The information in there is typically used by DOS and its software as a reference as to how it should act, or where to locate certain files, or how to configure itself - or a combination of these. DOS, or its software programs, consult this area to see if there are instructions or information in there for them. So essentially, the environment contains the conditions & surroundings in which DOS and its programs work. These conditions can be imposed by DOS itself, its software, or by a user.

DOS INTERNAL VARIABLES A few of the environmental variables that come with DOS are shown below. These are set by DOS itself, or they may be specified by the user, as necessary, either through the boot-up configuration file or during the course of any computer work session. The latter may be done at the comand line or from within a batch file. ● ● ● ● ● ● ● ●

BOOT - The Boot Drive COMSPEC - The command interpreter specifier CONFIG - Allows various start-up configurations to be referenced. COPYCMD - Sets the overwrite traits of COPY, MOVE, and XCOPY. CPU - The current processor in use. DAY - The current day of the month. DIRCMD - Sets the method by which DIR shows files. DATE - The current date.

http://www.chebucto.ns.ca/~ak621/DOS/DOS-Var.html (3 of 10)10/26/2006 12:57:22 PM

DDB: DOS Variables ● ● ● ● ● ●

OS - The current operating system's name and version. PATH - The directories DOS may search to locate files. PROMPT - That which appears before the cursor at the command line. TEMP or TMP - Where temporary information is stored or work done. TIME - The current time. VER - The DOS version in use.

Be aware that not all versions of DOS support all of the above, and that these are not the only ones available. The ones above can take different forms with different DOSes. Realise too, that any changes made to variables that had originally been set during boot up will return to their defaults after a reboot. If you want to keep any of them, you will have to place the SET commands into AUTOEXEC.bat or some other file which is accessed upon boot-up.

Why Might I Want to Use the Environment beyond What was Already Set? Well, first of all, as mentioned above, one may wish to change the DOS default settings. Many users prefer a custom prompt beyond " C:\> ". As well, users might wish to include other directories in the DOS path. To see what environment variables are already in memory, issue "SET" by itself. A list will appear. Each can be changed by specifying the variable name, an equals sign and a new value. Thus, resetting %PROMPT% by: SET PROMPT=$D $P will show the current date, a space, and then the current drive & directory at the command line.

An Aside:

Do not confuse the PROMPT command's usage of dollar signs with the usage of the dollar sign for variables as discussed in this article. PROMPT uses the dollar sign to designate what are called "meta strings" in order to set the prompt one sees at the command line. These are similar in execution to, but are not the same as environment variables. (See your DOS manual's PROMPT explanation.)

Another reason to want to reset some of the values differently from the DOS defaults is in regards to

http://www.chebucto.ns.ca/~ak621/DOS/DOS-Var.html (4 of 10)10/26/2006 12:57:22 PM

DDB: DOS Variables

the TEMP directory. This is where temporary work is performed and temporary information is stored - a scratch pad of sorts. One may wish to locate that directory on a faster RAM drive; that is done by creating the directory and then resetting the %TEMP% variable to point to that directory. Users may also want to use the environment to dictate how certain DOS commands will work, or to set up varying configurations. An example of the former would be the %COPYCMD%. This sets the overwrite characteristics of the COPY, MOVE, and XCOPY commands to make them ask the user before files are overwritten, or conversely, to allow automatic overwriting. The %CONFIG% variable references different startup configurations and permits additional choices to be made later on based on its contents. Beyond those types of assignments, one may want to set variables to automate work that DOS will do. That work might require a different value depending on circumstances, hence the need for a variable term. So this might be seen as: :LOOP (Commands) IF "%CYCLE%" == "20" GOTO END GOTO LOOP In operation, a batch file could be set up to run a procedure over & over until `n' cycles have passed. Lines could test for say, 20 cycles, and then determine what the file should do next. Above, this snippet of a batch file is testing to see if %CYCLE% has reached the value of `20'. If it hasn't, the batch file will run its procedure again. If it has, the batch operation will end. In operation, the value of %CYCLE% would vary (increase) by one for each time through the procedure as per instructions on other lines not seen here in this example. If you want to remove a variable entirely, issue "SET (name)=". So to delete the %CYCLE% variable in the above example, it would be "SET CYCLE=". Do not include any percent signs when issuing this command. The opposite side of this technique is if a value that must be kept constant might change in the course of operations. Placing the original value into the environment would preserve it for later usage: SET PARAMETER=%1 This takes the first item typed on the command line after the batch file name and assigns it the name of %PARAMETER%. If the contents of %1 were to change during the course of the procedure, but the first contents were those which are required, one could not use %1 anymore. However, since % PARAMETER% remembers those original contents, it may be referenced instead. This brings us to:

http://www.chebucto.ns.ca/~ak621/DOS/DOS-Var.html (5 of 10)10/26/2006 12:57:22 PM

DDB: DOS Variables

REPLACEABLE CHARACTER VARIABLES These are often used in conjunction with environment variables and can have changing values, too. They are always preceded by at least one percent sign ( % ) and some by two. Some, like DOSKEY use the dollar sign ( $ ) as a replaceable character identifier. All are just ways for the operating system or its programs to recognise variables. Here is an example of one used at the command line: Suppose you have a batch file called "CTF.bat". It is used to copy text files to a floppy in order to be taken home or elsewhere. You don't want it to copy all the files every time but only a certain one. So you set it up so that the batch file's main line reads: COPY %1.TXT A: The " %1 " is a replaceable character variable. The batch file will fill in whatever you type at the command line and issue the COPY command upon it. Thus, if you enter " CTF READ-ME " at the command line, CTF.bat will copy a file called READ-ME.txt from the current directory to your `A' drive floppy. The next time, you might issue " CTF DIRECTORY " so that time the file copied to the floppy would be DIRECTORY.txt. Note that this example uses only one percent sign. Generally, single ones are used for replaceable characters, while double percent signs are used when setting a string variable. More on that in a moment... Let's use a second variable in the above example. If you have two floppy drives and wish to copy a differing file to a differing drive, your batch file could read: COPY %1.TXT %2 The first variable would contain the file to be copied, while the second would be the drive to where it is to be copied. Thus, " CTF BACKUP B: " would copy BACKUP.txt to the `B' drive. %1 was replaced with " BACKUP ", while %2 was replaced with " B ". Then, DOS did the copy operation.

The FOR-IN-DO COMMAND Shown as the "FOR" command in documentation, this is often spoken of as "FOR-IN-0D". This command allows an operation to be carried out on a number of items which could be drives, files, or other commands. An example in a batch file might be: FOR %%F IN (TEST-1.txt TEST-2.txt) DO TYPE %%F

http://www.chebucto.ns.ca/~ak621/DOS/DOS-Var.html (6 of 10)10/26/2006 12:57:22 PM

DDB: DOS Variables

This means that FOR each file IN the brackets DO type (show) its contents on the screen. The variable " %%F " will represent each of the two text files in turn and display its contents. Note the two percent signs before the variable character of `F'. This is how this command is used in a batch file in most DOS versions. At the command line, this is reduced to one percent sign. Also realise that the `F' character could be any letter. I chose `F' because it represents the Files shown in the brackets. If drive letters were inside the brackets, I would use " %%D ", and so on. Alternatively, some DOS versions allow full words to be used as the variable: FOR %%FILES IN (TEST-1.txt TEST-2.txt) DO TYPE %%FILES

Where May I Use Variables? User variables may be employed at the command line and within programs. They are most commonly used with, or within, batch files.

Can You Provide More Examples? One would be using the current date. Perhaps you want to have a backup file made every day which name incorporates that day's date. You don't want to have to type in that differing date every day and would rather DOS do this for you. Older versions of DOS do not include this variable as set by DOS itself, but there are ways to get the date into the environment. CTULD.bat shown elsewhere at this website explains how that is done. New versions of DOS already set it as " %DATE% " or similar. Once the current date is in the environment, one may issue commands such as: MD C:\BACKUP\%DATE% COPY C:\DATA\*.* C:\BACKUP\%DATE% This makes a directory which name will be the current date and then copies the data files to it. Regardless of what the date is, a directory by that name will be created and files copied to it every day. As you are aware, the date varies every day but the variable name called " %DATE% " does not. It always remains as %DATE%, or whatever you choose to use. Since I incorporate a lot of date operations in my batch files, I prefer " %CUR-DATE% " to differentiate it from other date variables I use.

http://www.chebucto.ns.ca/~ak621/DOS/DOS-Var.html (7 of 10)10/26/2006 12:57:22 PM

DDB: DOS Variables

How Do I Create a Variable? Simply use the "SET" command. The syntax to place a variable in the environment is " SET V=S ", where `V' is the name you wish to assign as the variable, and `S' is the text string to be assigned. Thus, if you place a line like "SET NAME=%1" in a batch file called LOGIN.bat, when Judy wants to log into whatever has been set up for that purpose, she would enter "LOGIN JUDY". Afterwards, one can test for the possible names and have DOS do work depending on the name used: :: LOGIN.bat :: @ECHO OFF SET NAME=%1 IF "%NAME%" == "ADAM" GOTO ADAM IF "%NAME%" == "JUDY" GOTO JUDY IF "%NAME%" == "MAHENDRA" GOTO MAHENDRA :ADAM (Adam's login procedure) GOTO END :JUDY (JUDY's login procedure) GOTO END :MAHENDRA (MAHENDRA's login procedure) :END That was a simple example with no error checking or security; nor was there any method presented to deal with a user entering lower or mixed case. However, it gives an idea of how variables might be used for that purpose.

Tell Me about those Percent Signs Whenever you set a variable, it will show up in the environment as a character string with percent signs at each end in most versions of DOS. For replaceable character purposes, the single leading percent sign is used, with exceptions at least for the FOR-IN-DO command and DOSKEY, if not others.

http://www.chebucto.ns.ca/~ak621/DOS/DOS-Var.html (8 of 10)10/26/2006 12:57:22 PM

DDB: DOS Variables

DOSKEY uses the `$' for replaceable characters, so instead of " %1 ", DOSKEY will use " $1 ". As already mentioned regarding the FOR-IN-DO command, when it's used at the command line, one percent sign is used, while in a batch file, two are needed for most versions of DOS. FOR

%F IN (*.TXT *.DOC) DO COPY

%F A:

FOR %%F IN (*.TXT *.DOC) DO COPY %%F A: Either one will copy the items listed inside the brackets to the `A' drive, but the second line is to be used in a batch file. DOS will eliminate the first percent sign preceding the `F' character in each instance when used in a batch file. As with earlier examples, DOS will replace the variable " %F " (or " %%F ") with the specified item(s). In the above example, the items are all the .txt and .doc files in the current directory.

Is there a Size Limit to the Variable? The total limit is a minimum of 128 characters and newer versions of DOS allow more. That means that the variable name and its contents could be any combination up to 123 characters long, with the balance being taken up by "SET", a space and the "equals" sign. However, excessively long variables can take away some of the shortcut nature of variables, but may have a use with long Internet URLs or for long server designations on a network. They might also be useful to some for deeply-nested subdirectories or for very long file names on systems which allow them.

There is much more to this, including other ways to use the environment, but the preceding will give you an idea about, and a start to, using DOS variables.

Using environmental variables is yet another lane on the road to becoming a DOS Power User. Learning about, and making usage of them will improve productivity, allowing you to automate more complicated tasks.

http://www.chebucto.ns.ca/~ak621/DOS/DOS-Var.html (9 of 10)10/26/2006 12:57:22 PM

DDB: DOS Variables

Return to the Main DOS Page Go to Richard Bonner's Homepage.

http://www.chebucto.ns.ca/~ak621/DOS/DOS-Var.html (10 of 10)10/26/2006 12:57:22 PM

DOS Batch File Tutorial: Advanced Examples I

Doctor DOS Betamax's

DOS BATCH FILE TUTORIAL

ADVANCED EXAMPLES

¦ Batch File Basics ¦

¦ Obtain 500+ Batch Files ¦

¦ Batch File Examples ¦

¦ Advanced Batch Files II ¦

¦ Batch File Tips ¦

¦ Advanced Batch Files III ¦

Here are some additional examples of batch files. These are more advanced than the previous examples. As before, the title remarks should suffice so you'll understand their operation (although some may be preceded by a syntax example). The more ambiguous lines have notes to the side. These explanatory notes are not part of the file. Do not type them in. As well, the most complicated examples have explanations following the file itself. See Batch File Basics before delving into this page, for a discussion of the techniques for writing batch files. Note that because this is an advanced page and you should have read the previous batch file pages at this site, that not every little thing will be explained. Again, remember, the "DR" command is a batch file that runs the Color Directory program I use. You may substitute DOS' "DIR" command with its switches set to your preferences. Finally, be aware that Doctor DOS will not be responsible for any problems resulting from the use or mis-use of anything presented here.

An advisory to non-Canadians: Some characters shown in some batch http://www.chebucto.ns.ca/~ak621/DOS/Bat-Adv.html (1 of 29)10/26/2006 1:11:25 PM

DOS Batch File Tutorial: Advanced Examples I

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. EXCEPT FOR THE BATCH FILES THEMSELVES, INFORMATION ON THESE BATCH FILE PAGES MAY NOT BE REPRODUCED WITHOUT PERMISSION FROM THE AUTHOR © THE BATCH FILES ARE FOR PERSONAL USE ONLY. THEY MAY NOT BE SOLD OR OTHERWISE DISTRIBUTED.

Batch Files Presented on This Page

CDS.bat Change Directory Sideways

CTULD.bat Copies Files by Date

DBF.bat Directory of the `B' Drive Optional `B' Drive Format

DELE.bat Delete All Files Except (Improved Version)

DRTD.bat List Today's Files

EAE.bat Edit AUTOEXEC.bat

ECS.bat Edit CONFIG.sys

RETURN.bat Return to a Tagged Directory

SDEL.bat Safe Delete (Improved Version)

THOUGHT.bat Displays a Thought of the Day.

TOP.bat Go to the Root Directory of Any Drive

UPDATE.bat Updates a Directory With the Latest Files

http://www.chebucto.ns.ca/~ak621/DOS/Bat-Adv.html (2 of 29)10/26/2006 1:11:25 PM

DOS Batch File Tutorial: Advanced Examples I

CDS.bat :: CDS.BAT :: Changes Directory to One on the Same or Higher Adjacent Level :: Displays the Results :: @ECHO OFF IF IF IF IF

"%1" == "1" "%1" == "2" "%1" == "3" NOT "%1" ==

GOTO LEVEL-1 GOTO LEVEL-2 GOTO LEVEL-3 "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

Explanation 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.

Usage This file allows one to change to a directory adjacent to the current one. That is, if the directory one level up is

http://www.chebucto.ns.ca/~ak621/DOS/Bat-Adv.html (3 of 29)10/26/2006 1:11:25 PM

DOS Batch File Tutorial: Advanced Examples I

PARENT-1 and you are in CHILD-1, by issuing "CDS CHILD-2" or "CDS 1 CHILD-2", 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. Here is a display of that structure to help you visualise this better: Level 3

Level 2

Level 1

GRANDMOTHER ------------------------------| | PARENT-1 PARENT-1A ------------------------| | | | CHILD-1 CHILD-2 CHILD-1A CHILD-1B

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 back in CHILD-1. The next level up is still PARENT-1. Adjacent to it on the same level is PARENT-1A, and above both PARENT directories is GRANDMOTHER. To get to CHILD-1A under PARENT-1A, issue "CDS 2 PARENT-1A\CHILD-1A". You may do the same thing with the directory above GRANDMOTHER by using the number `3'. Note that the name "PARENT-1A" exceeds 8 characters. It is just used here to better represent that directory so as to keep it straight with the other directories shown. If your DOS is not long file name capable, this directory name would get truncated to "PARENT-1". As such, it could not be created because there is already a directory with that name. Again, this was just done here to help you comprehend this example. Of course, in actuality, you will be typing the names of directories on your own system. When you use this batch file, don't forget the space on either side of any number you use at the command line. 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.

Directory Display? For those of you thinking about having the batch file show 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 displayed was annoying every time 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

http://www.chebucto.ns.ca/~ak621/DOS/Bat-Adv.html (4 of 29)10/26/2006 1:11:25 PM

DOS Batch File Tutorial: Advanced Examples I

to which you might wish to go. Perhaps I'll write that version and add it here in the future.

AUTOMATIC COPYING BY DATE Here's the scenario: You work all day on some web page files in a given directory and you wish to copy just the modified ones to your UPLOAD directory to transfer them on line to your server. Unfortunately though, by the end of the day, you have forgotten the exact ones upon which you worked. You could issue a DIR /O:D command to sort by date, note which files have today's date, then issue copy commands for each, but that's a lot of typing. You could open up (shudder) Windoze Extorturer (Windows Explorer) or a similar file manager for DOS, hunt through the directory's file list and tag (mark) each file having today's date. Then you'd copy the marked files to the UPLOAD directory... but - why bother with such tedium? What would happen if you wanted files from the last few days copied? That adds an extra element of complexity. As always, efficient DOS can do this for you automatically. This next batch file uses the DOS environment to hold the value of the current date. This is called an "environment variable". That's because the variable may have different values at different times. (The "environment" is an area of memory which, in part, holds various parameters to which programs refer as a guide to how they should operate.) The batch file then uses this current-date variable in a fairly typical XCOPY operation to copy only today's files, or alternatively, those from a few days, weeks, or more ago, on up until & including today. This is very slick and saves a lot of typing, file perusing, note taking, and... best of all, it keeps you out of Windoze. (-:

CTULD.bat Syntax: CTULD (Optional Date) :: CTULD.bat :: Copies Files Modified or Created Today :: to the Upload Directory, or Copies :: from the Specified Date Forward :: @ECHO OFF IF "%1" == "" GOTO COPY-TODAY IF NOT "%1" == "" GOTO COPY-DATE

If no Date is Given, Today's Files will be Copied Otherwise, Files will be Copied Forward of the Date Typed

:COPY-TODAY (See Text, Below) ECHO. | DATE | FIND /I "Current" > C:\BATCH\CUR-DATE.BAT ECHO @SET CUR-DATE=%%4 > C:\BATCH\CURRENT.BAT CALL C:\BATCH\CUR-DATE.BAT XCOPY *.* C:\UPLOAD /D:%CUR-DATE% > NUL http://www.chebucto.ns.ca/~ak621/DOS/Bat-Adv.html (5 of 29)10/26/2006 1:11:25 PM

Copies Files based on

DOS Batch File Tutorial: Advanced Examples I

Today's Date ("NUL" Hides Screen Messages) GOTO END :COPY-DATE XCOPY *.* C:\UPLOAD /D:%1 /-Y

Copies Files based on The Date Given at the Command Line

:END ECHO. CALL C:\BATCH\DR.BAT C:\UPLOAD

Separates the Listing Gives a Listing for the UPLOAD Directory to Show the Operation's Success

SET CUR-DATE=

Removes the Date Variable From the Environment

DEL CUR-DATE.BAT DEL CURRENT.BAT

Deletes the Temporary Batch Files ________

Explanation For the "COPY-DATE" section, a partial explanation of what happens may be seen by typing "DATE" at the command line. DOS gives the current date (assuming you have correctly set the date and that it has been maintained by the CMOS battery). Then, DOS also asks you for a new date. This is so that a user may type in a given date, which is normally the current one. To keep this current date, hit "ENTER". The trick here in this batch file example is to have it hit "Enter" for you and also to isolate the current date and make it an environment variable. This variable may then be used for the XCOPY operation. In the batch file, the "ECHO." at the start of the "COPY-DATE" section sends a carriage return ("ENTER") to the DATE command, leaving the current date unchanged, as you just did when you ran "DATE" from the command line. The DATE command is then piped to the FIND command, which is asked to locate the line containing the word "Current". That line in turn is sent to a file called "CUR-DATE.bat", which is created by this (CTULD) batch file. The "CUR-DATE.bat" file reads "Current Date is (day) (date)". Note here, that the fourth parameter after the word "Current", is today's date. For the next line, using the DOS "SET" command, a variable called "CUR-DATE" is made from the fourth parameter (designated `%%4'). This is sent to another created file called "CURRENT.bat". Now everything is in place, so let's review: We have one batch file called "CUR-DATE" that has one line in it reading "Current Date is (day) (date)". Being a batch file, DOS tries to run it. Since the first word is "Current", DOS believes this is a command or executable file called "Current". Well, guess what? A batch file called "Current" was just created. So DOS runs it, because DOS sees "Current" to be a batch file name and the rest of the words to be http://www.chebucto.ns.ca/~ak621/DOS/Bat-Adv.html (6 of 29)10/26/2006 1:11:25 PM

DOS Batch File Tutorial: Advanced Examples I

parameters for that command. The fourth parameter is the current date, and the batch file requests that this fourth parameter be placed in the environment as "CUR-DATE". Now, since "CUR-DATE.bat" was called by CTULD.bat, when CUR-DATE finishes, control is returned to CTULD.bat, which runs the XCOPY line. When the XCOPY line is run, the current date will be substituted for "CUR-DATE" every time, and thus XCOPY will copy files from only today to the UPLOAD directory. Slick, huh? Since these files are created every time CTULD. bat is run, the XCOPY date parameter will always be the current date. Even if the files already exist, they will be overwritten with the latest date information. Regardless of when you run CTULD.bat, it always will be today's files that will be copied, unless you fill in a command line date parameter. In that case, control is transferred to the "COPYDATE" section. For the "COPY-DATE" section, DOS simply substitutes the date you type on the command line and runs XCOPY based on that date. Thus if you type "CTULD (last week's date), DOS will copy files to the UPLOAD directory on and forward of the date you typed at the command line. Remember to type the date in the same format as that which is recognised by your computer for your country. (Users of this batch file should be aware that it was based on MS-DOS. Those using other DOS manufacturers or using DOS under Windows will have to alter the batch file to match what their DATE commands output.)

Improvements This file can be shortened by using updated DOS commands or by using a version of DOS that includes the `% DATE%' variable built in. For those using DOS versions without the latter, one method is to get an update of the "SET" command, called "XSET". It can set a date variable with just one line of code without the extra files or even the piping, as was done in the above batch file example. This, and many other capabilities are built into the XSET command. If used in the above example, a one-line XSET command can set "CUR-DATE", with the next line immediately being the XCOPY-by-date command. It shortens and simplifies the batch file. See DRTD.bat, farther on, for an example of the XSET date technique. The other, even shorter method is done via an update to "XCOPY" called "XXCOPY". It can do either operation using just one line: XXCOPY *.* /DA:%1 One would go to the directory in which the command is to be issued, and enter "CTULD ." for today's files, or enter "CTULD (date)" to copy files on, or after, the given date. Excluding a title and comments, this method reduces the CTULD batch file to just one line! These updated commands may be obtained from: http://www.chebucto.ns.ca/~ak621/DOS/Bat-Adv.html (7 of 29)10/26/2006 1:11:25 PM

DOS Batch File Tutorial: Advanced Examples I

● ●

-XSET -XXCOPY

DBF.bat (Requires DOS 6 or Newer) 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 ECHO. ECHO ECHO. ECHO. ECHO ECHO. ECHO ECHO. ECHO. ECHO ECHO.

Full Format B Drive? Press `F' to Format

Quick Format B Drive? Press `Q' to Quick Format

Press "Escape" to Cancel

C:\DOS\CHOICE /C:FQ<- /N > NUL IF ERRORLEVEL 3 GOTO END IF ERRORLEVEL 2 FORMAT B: /Q /U IF ERRORLEVEL 1 FORMAT B:

http://www.chebucto.ns.ca/~ak621/DOS/Bat-Adv.html (8 of 29)10/26/2006 1:11:25 PM

DOS Batch File Tutorial: Advanced Examples I

: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.) ________

Explanation 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 ERRORLEVEL 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

http://www.chebucto.ns.ca/~ak621/DOS/Bat-Adv.html (9 of 29)10/26/2006 1:11:25 PM

DOS Batch File Tutorial: Advanced Examples I

Names are Given. MD SAVE

Makes a Temporary "SAVE" Directory.

:MOVE-FILES XCOPY %1 SAVE > NUL SHIFT IF "%1" == "" GOTO FILES-DELETE GOTO MOVE-FILES

:FILES-DELETE ECHO Y | DEL . > NUL MOVE SAVE\*.* . > NUL RD SAVE DR

Copies the First Specified File Uses the "SHIFT" Command to move each Specified File name to the "%1" Position after it copies that Specified File to the "SAVE" Directory until none are left. Deletes all Files in the Current Directory, showing no Prompts. Returns Excepted File(s) to the Current Directory. Removes "SAVE" Directory and Displays the Results of the Operation.

:END

DRTD.bat :: DRTD.bat :: Lists Files Made or Modified Today :: In the Current or All Directories :: @ECHO OFF C:\DOS\XSET CUR-DATE DATE MM-DD-YY IF "%1" == "" GOTO LIST-CUR IF "%1" == "\" GOTO LIST-ALL :LIST-CUR DIR *.* /O:N | Find "%CUR-DATE%" | MORE GOTO END :LIST-ALL DIR \*.* /O:N /S | Find "%CUR-DATE%" | MORE :END SET CUR-DATE=

http://www.chebucto.ns.ca/~ak621/DOS/Bat-Adv.html (10 of 29)10/26/2006 1:11:25 PM

DOS Batch File Tutorial: Advanced Examples I

Explanation The file starts by using the updated DOS command, "XSET" to place the current date into the environment variable "CUR-DATE". It is in the typical format the DOS "DIR" command shows for file dates. If you have set a different format, alter the date letters in the batch file to match. It is also assumed that XSET is in the C:\DOS directory. Next, two "IF" statements determine if you wish to show today's made or modified files from the current directory only, or those from the entire current drive. The decision made here directs the batch file to one of the "LIST" sections. In either section, DIR is used to get a list of files in order by name. This is piped (passed) to the FIND command. FIND looks for lines with today's date and displays them. Should the screen fill, the MORE command will present the list a screen at a time. The batch file ends by removing the CUR-DATE variable from memory by setting it to equal nothing. See here for obtaining XSET.

Usage Simply enter DRTD to get a list of files made or modified today in the current directory. To find all of today's files on the entire current drive, enter "DRTD \".

EAE.bat (Requires DOS 6 or Newer) :: EAE.bat :: Edits "Autoexec.bat" :: Prompts to run Autoexec or :: Return to the DOS Command Line :: @ECHO OFF C: EDIT C:\AUTOEXEC.bat ECHO. ECHO ECHO. ECHO ECHO.

Loads AE into DOS' "EDIT"

Run "Autoexec.Bat" ? Press: `R'. To Cancel,

Press: `Escape'

http://www.chebucto.ns.ca/~ak621/DOS/Bat-Adv.html (11 of 29)10/26/2006 1:11:25 PM

Places choices on to the screen with spacing.

DOS Batch File Tutorial: Advanced Examples I

CHOICE /C:R<- /N > NUL IF ERRORLEVEL 2 GOTO END IF ERRORLEVEL 1 GOTO RUN GOTO END

DOS 6's "Choice" command. Determines which key has been pressed and decides 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 (Requires DOS 6 or Newer) :: ECS.bat :: Edits the Config.Sys File :: Prompts to Reboot or Not :: @ECHO OFF EDIT C:\CONFIG.SYS ECHO. ECHO ECHO. ECHO ECHO.

Loads CS into DOS' "EDIT"

Reboot Computer? Press: `R'. To Cancel,

Press: `Escape'

Places choices on to the screen with spacing.

CHOICE /C:R<- /N > NUL IF ERRORLEVEL 2 GOTO END IF ERRORLEVEL 1 GOTO REBOOT GOTO END

DOS 6's "Choice" command. Determines which key has been pressed and decides 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.) ________ http://www.chebucto.ns.ca/~ak621/DOS/Bat-Adv.html (12 of 29)10/26/2006 1:11:25 PM

DOS Batch File Tutorial: Advanced Examples I

WARMBOOT.com This is a debug program that 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. WARMBOOT.com. 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.

Directory Tag & Return This batch file 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 that 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 :: 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 that does not allow the cursor to drop below the last line. http://www.chebucto.ns.ca/~ak621/DOS/Bat-Adv.html (13 of 29)10/26/2006 1:11:25 PM

DOS Batch File Tutorial: Advanced Examples I

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

Usage 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! http://www.chebucto.ns.ca/~ak621/DOS/Bat-Adv.html (14 of 29)10/26/2006 1:11:25 PM

DOS Batch File Tutorial: Advanced Examples I

Explanation "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 F11 and F12. 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. http://www.chebucto.ns.ca/~ak621/DOS/Bat-Adv.html (15 of 29)10/26/2006 1:11:26 PM

DOS Batch File Tutorial: Advanced Examples I

First, begin by creating or copying the following, beginning with the double colon lines:

HOME.txt :: HOME.txt :: Used With Home.bat :: @ECHO OFF (Blank Line) CLS Note the blank line. Do not type those words; simply leave a blank line.

SAVE-DIR.txt CD(Space) Note that 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 farther back, this file should be typed in a text editor that 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

http://www.chebucto.ns.ca/~ak621/DOS/Bat-Adv.html (16 of 29)10/26/2006 1:11:26 PM

DOS Batch File Tutorial: Advanced Examples I

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.

How it Works SAVE-DIR.bat works by first creating HOME.bat. It does that by copying the lines in HOME.txt to HOME.bat. Next, it 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. 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: CLS (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? 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. Below is an alternate version of SAVE-DIR.bat which does not rely on DRIVE-IS.com. It too, should work on most versions of DOS. Here, the "VOL" (Volume) command is used to save the current drive letter into a variable in the same manner as the "date" technique seen in CTULD.bat, farther back.

SAVE-DIR.bat :: SAVE-DIR.bat (Alternate) :: Remembers the Current Drive and Directory :: @ECHO OFF COPY HOME.TXT C:\BATCH\HOME.BAT /Y > NUL VOL | FIND "Volume in" > C:\BATCH\TEMP.BAT http://www.chebucto.ns.ca/~ak621/DOS/Bat-Adv.html (17 of 29)10/26/2006 1:11:26 PM

DOS Batch File Tutorial: Advanced Examples I

ECHO @SET CUR-DRV=%%3: > C:\BATCH\VOLUME.BAT CALL TEMP.BAT ECHO %CUR-DRV% >> C:\BATCH\HOME.BAT TYPE SAVE-DIR.TXT >> C:\BATCH\HOME.BAT CD >> C:\BATCH\HOME.BAT CLS ECHO. ECHO Ready to Return to ECHO the Current Directory ECHO. DEL C:\BATCH\TEMP.BAT DEL C:\BATCH\VOLUME.BAT SET CUR-DRV= In the final version of this batch file, farther on, is another method that can also work well under various DOS versions. 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 DRIVE-IS.com is a compiled debug script. DRIVE-IS.com and place it in a C:\UTIL (Utilities) directory.

Usage 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)

http://www.chebucto.ns.ca/~ak621/DOS/Bat-Adv.html (18 of 29)10/26/2006 1:11:26 PM

DOS Batch File Tutorial: Advanced Examples I

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 :: 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=

Explanation This works by first creating HOME.bat. If a previous version already exists, it is automatically overwritten. SAVEDIR.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. http://www.chebucto.ns.ca/~ak621/DOS/Bat-Adv.html (19 of 29)10/26/2006 1:11:26 PM

DOS Batch File Tutorial: Advanced Examples I

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.

Usage 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) Syntax: SDEL (File1, File2, File3...) :: SDEL.bat (Improved) :: Displays Files to Be Deleted

http://www.chebucto.ns.ca/~ak621/DOS/Bat-Adv.html (20 of 29)10/26/2006 1:11:26 PM

DOS Batch File Tutorial: Advanced Examples I

:: 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 ECHO.

NO FILES GIVEN!

:END C:\BATCH\DR.BAT

(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.)

http://www.chebucto.ns.ca/~ak621/DOS/Bat-Adv.html (21 of 29)10/26/2006 1:11:26 PM

DOS Batch File Tutorial: Advanced Examples I

Explanation 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 that 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.

Usage 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.

THOUGHT.bat This will display a random "Thought of the Day" or any message you want seen upon bootup, as taken from a pool of messages.

THOUGHT.bat :: THOUGHT.bat :: Displays a Random "Thought of the Day" Message Upon Bootup :: @ECHO OFF CLS C:\DOS\XSET NUMBER RANDOM 1 20 DIR C:\THOUGHTS\*.tht /B | XSET /LINE %NUMBER% FILE MORE < C:\THOUGHTS\%FILE% SET NUMBER= SET FILE= :END

Explanation http://www.chebucto.ns.ca/~ak621/DOS/Bat-Adv.html (22 of 29)10/26/2006 1:11:26 PM

DOS Batch File Tutorial: Advanced Examples I

This uses XSET, an update to the DOS "SET" command, to get a random number. Be sure XSET is in the C:\DOS directory. If not, change the path for it in the batch file. You will replace `20' with a number equal to the number of message files you will be creating. (See Usage, below.) DIR is then used to send a list of your message file names to XSET which selects one of the file names based on the random number it picked earlier. So if the number is `14', XSET will read the file name on Line 14. The chosen name is placed into an environmental variable called "FILE". That in turn, is used by the MORE command to display the file's contents on a cleared screen.

Usage You must first create a C:\THOUGHTS directory, or any other name and path you wish. Just be sure the batch file refers to whatever directory you have created. Then, simply make a number of text files with whatever message you wish. Give them a ".tht" extension and put them in the C:\THOUGHTS directory. To generate space around the message, have each file begin and end with some blank lines. Also, indent each message line a bit from both the left and right margins. Now, your message will stand out on the screen. Suggested messages might be proverbs, sayings, jokes, or new words. With the latter, place the word on the screen with its definition. If learned words keep appearing on the screen, replace them with new ones. Your vocabulary will increase with this idea. Another idea is to use this to learn to put names with images. These might be people, places or objects. Simply make up some .gif or .jpg images with names and place them into the THOUGHTS directory. Instead of using MORE to display a text file, use an image viewer to put one of your selected images on the screen at bootup. Place a line running THOUGHT.bat at the end of your AUTOEXEC.bat so that every time you boot, reboot, or run AUTOEXEC, a message will be displayed. Here is the link to get XSET.

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 that will allow you to go immediately to the root directory of any drive on your system and see a files listing when you arrive. The first method below 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

http://www.chebucto.ns.ca/~ak621/DOS/Bat-Adv.html (23 of 29)10/26/2006 1:11:26 PM

DOS Batch File Tutorial: Advanced Examples I

:: 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 IF IF IF IF IF

"%DRIVE%" "%DRIVE%" "%DRIVE%" "%DRIVE%" "%DRIVE%" "%DRIVE%"

== == == == == ==

"A" "B" "C" "D" "E" "F"

GOTO GOTO GOTO GOTO GOTO GOTO

A-DRIVE B-DRIVE C-DRIVE D-DRIVE E-DRIVE 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\ http://www.chebucto.ns.ca/~ak621/DOS/Bat-Adv.html (24 of 29)10/26/2006 1:11:26 PM

DOS Batch File Tutorial: Advanced Examples I

:END CLS CALL C:\BATCH\DR SET XSET= SET DRIVE=

Usage 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.

Explanation The first thing that 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 that 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 that 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 equal nothing. Here is the link to get XSET.

http://www.chebucto.ns.ca/~ak621/DOS/Bat-Adv.html (25 of 29)10/26/2006 1:11:26 PM

DOS Batch File Tutorial: Advanced Examples I

A Simpler Method 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 :: 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.

UPDATE A DIRECTORY So you like to keep a backup directory of your latest files but never seem to be able to keep it current. Here is a way to be sure that backup directory will contain all the source files and only their latest versions. Afterwards, another batch file will be presented that updates both the source and destination directories so that both will contain exactly the same files and only the latest versions. Finally, a third version demonstrates a different method.

UPDATE.bat :: UPDATE.bat :: Updates the Chosen Directory with the Latest Files :: @ECHO OFF http://www.chebucto.ns.ca/~ak621/DOS/Bat-Adv.html (26 of 29)10/26/2006 1:11:26 PM

DOS Batch File Tutorial: Advanced Examples I

REPLACE %1\*.* %2 /A REPLACE %1\*.* %2 /U :END ________

Explanation This simple batch file uses REPLACE to add files from the source that do not exist in the destination (second) directory via "/A". The second line overwrites files in the destination directory with those from the source directory only if the source files are newer. That is done by the "/U" (Update) switch. You specify both the source and destination directories via "%1" and "%2". Thus it may be used to backup any directory to any other directory even on another drive.

Usage Simply enter "REPLACE (source directory) (target directory)". You may specify any directories on any drives provided they already exist. Files in the first directory specified will be added to the second specified directory, or be updated if they already exist in that second directory. So if the destination of the files is D:\BACKUP, you would enter "UPDATE D:\BACKUP".

An Improvement Here's one step farther from the above. It will synchronise two directories by placing the same files from each directory into the other, and by being sure that they are the latest ones.

UPDATE.bat :: UPDATE.bat (Improved) :: Updates both the Source and Destination Directories with the Latest Files :: @ECHO OFF REPLACE %1\*.* %2 /A REPLACE %1\*.* %2 /U REPLACE %2\*.* %1 /A REPLACE %2\*.* %1 /U :END http://www.chebucto.ns.ca/~ak621/DOS/Bat-Adv.html (27 of 29)10/26/2006 1:11:26 PM

DOS Batch File Tutorial: Advanced Examples I

________

Explanation This works as before with the first lines adding and updating files from the first directory to the second. Then it repeats the process from the second to the first. So if there are three files in the first directory and only two in the second, the extra file will be added to the second directory. Also, if the first directory's files are newer, they will be copied to the second directory and overwrite the older versions in that directory. Next, if there are any files in the second directory that are not in the first, they will be added to the first. Also, any files not overwritten in the second directory by first operation, means that the second directory's files must be the same or newer. If they are newer, they will be used to overwrite their older versions in the first directory. Thus, when the batch file finishes, both directories will contain the same files, and those files will only be the latest versions.

A Variation Here's a different way to do just the single update. It uses a newer version of DOS' "XCOPY" command called "XXCOPY". This batch file will allow one to have a working directory and with the latest files kept in the backup directory. It also allows one to work in the destination (BACKUP) directory but have the latest files kept only in that BACKUP directory. This is without fear of overwriting by those in the work directory - unless the work directory files are newer.

UPDATE.bat :: UPDATE.bat (XXCOPY Version) :: Updates both the Source and Destination Directories with the Latest Files :: Updates the Chosen Directory with the Latest Files :: Deletes Copied Files from the Source :: @ECHO OFF XXCOPY C:\WORK\*.* C:\BACKUP /BN /RC /ED /YY > NUL :END ________

Explanation XXCOPY will copy all files from the WORK directory to the BACKUP if they are newer or if they do not exist in the BACKUP directory by using its "/BN" (Backup New) switch. Then, via "/RC" (Remove-after-Copy), it removes http://www.chebucto.ns.ca/~ak621/DOS/Bat-Adv.html (28 of 29)10/26/2006 1:11:26 PM

DOS Batch File Tutorial: Advanced Examples I

from the source only those files that were copied. Any files remaining there must be older, and thus must have been updated sometime previously right in the BACKUP directory. You may then delete the files remaining in the WORK directory because they are older. The "/ED" switch preserves any directories that become empty, while "/YY" and "> NUL" eliminate any prompts and messages.

Usage Enter "Update". No source & destination directories are needed because this batch file is dedicated to the WORK and BACKUP directories. Of course, if you use other directory pairs, you may substitute `%1' and `%2' as was done previously, and then specify the desired directories at the command line. XXCOPY may be obtained from -XXCOPY.com.

More of these are in Advanced Batch Files II.

¦ Batch File Basics ¦

¦ Obtain 500+ Batch Files ¦

¦ Batch File Examples ¦

¦ Advanced Batch Files II ¦

Return to the Main DOS Page

Go to the Richard Bonner's Home Page

http://www.chebucto.ns.ca/~ak621/DOS/Bat-Adv.html (29 of 29)10/26/2006 1:11:26 PM

¦ Batch File Tips ¦

¦ Advanced Batch Files III ¦

Related Documents

Dos Variables
November 2019 23
Funciones De Dos Variables
October 2019 30
Funciones De Dos Variables
October 2019 25
Variables
May 2020 17
Variables
November 2019 45
Variables
June 2020 24