MS-DOS Tutorial

Started by Dilbert, September 21, 2006, 05:01:14 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.


Deleting entire directory trees
The Windows GUI uninstall feature hardly removed anything. Or, you're in the Recovery Console and you're trying to free up space on your hard drvie. Well, this command works perfectly.

NOTE: If you are using Windows 2000 or XP, the below command is not for you. Use the RD command with the /S switch.

For everything from DOS 5.0 to Windows ME:

DELTREE [path]

So, if you no longer need C:\Program Files\Real and everything in it, do this:

DELTREE C:\Program Files\Real


There is only one switch: /Y. Using this forces the DELTREE to run without prompting for you to give the OK.
[center][/center]"The geek shall inherit the Earth."


Some installation instruction requires you to edit the dreaded AUTOEXEC.BAT. Oh no! What are you going to do now? Well, it's really quite simple. AUTOEXEC.BAT contains nothing more than variables with set values. You csn see the list of variables by typing "SET" bar the quotes and pressing [Enter]. It will be a long list. Most of these values should not be modified at all. However, you can add variables without causing harm, and you can also add to the PATH variable without causing any trouble.

The AUTOEXEC.BAT is no longer a function after Win98. The same function is accomplished in Environment Variables.

For all other OS's: Modifying AUTOEXEC.BAT
Let's say you want to keep batch files in a directory, and have them work no matter where in your computer you are. Simple, right? Of course it is. Let's say you have your batch files in the same place I do - C:\Batch. To "install" this directory, go into AUTOEXEC.BAT, find the path variable, and go to the last directory. If there is not a semicolon (;) at the end, put one there. Then type the full path of the directory (no spaces in the whole variable). Save and exit. You should be able to use your batch files anywhere you choose. If not, type AUTOEXEC and press enter; you may need to have the PC cycle through the PATH variable again.

To add a variable of your own, simply go to the end of AUTOEXEC.BAT, add a new line, and use this syntax:




You can also set variables with the prompt, but they only exist for that particular session. If you reboot (or exit, if you're using the Prompt) the variable will be gone. To so this, use the SET command:

SET color=red

You can set the same variable an infinite number of times, limited only by your boredom with endlessly setting a variable; however, there may be a limit to the number of variables you can set. If you run into this, consider reusing a variable or exiting the session/rebooting (for true MS-DOS).

As you will learn in the next lesson, variables are a great asset to the MS-DOS experience.
[center][/center]"The geek shall inherit the Earth."


Complex Batch Files
(Otherwise known as making the simplest of tasks as confusing as possible)

You should know the basics of how to write batch files. As your knowledge of DOS commands grows, so too does the available commands you can use in your batch files. However, there are some commands that can only be used in batch files, or at least only effectively in a batch file. They are some of the most powerful tools available in batch. They are:

IF - Executes code only if a certain condition is true.
SET - Assigns a variable; in a batch file, can also trigger a special prompt.
LABEL - A code block given a special name for reference purposes.
GOTO - Code which allows you to go to a specific label.
FOR - Code which is executed a certain number of times based on certain conditions.

IF executes a code block only if a certain condition is true. Typically used with the GOTO command, it makes an excellent way of directing the "traffic" or your code. Example: Let's say you want to do a DIR only if there are files in the directory. This line of code would work:


The IF EXIST function checks to see if the specified files exist. It also can be used to check to see if they don't exist - IF NOT EXIST.

IF can also be used to see if an operation worked/can be done. If you want to copy a file but you want to make sure the destination directory exists, use ERRORLEVEL. ERRORLEVEL is normally 0, but will become 1 if ever something goes wrong. So:

COPY blah.txt yada\yada.txt
IF NOT ERRORLEVEL 0 ECHO Error: Could not find destination

More info on IF covered later.

SET - Just like in the prompt, set makes a variable that remains until the end of the DOS session (meaning typing the SET command after the batch is executed will show this new variable). Example use: What if you wanted to change directory to the A: drive, and back? Well, how would you know where "back" is, if you have 100+ directories on your PC? And what about other PC's? You can't just guess at it. You can do this:

SET CurrDIR=%cd%
[do stuff]
cd %CurrDIR%

The %cd% variable is the current directory. But when you change directories, %cd% changes. However, saving the %cd% in another variable that does not change allows you to go back to where the user was before.

SET can also be used to make custom prompts, allowing the user to make choices. An example: User can choose between opening AUTOEXEC.BAT, going to A:, or going to C:. Choices can be displayed with the ECHO command, and SET is used to create a custom prompt:

ECHO What would you like to do?
ECHO 2. Go to A:
ECHO 3. Go to C:

SET /P CHOICE="Enter 1/2/3>"

The /P switch creates a prompt, and CHOICE is a variable. The value in quotation marks is what is displayed at the prompt. When the user types something and presses ENTER, the variable is changed to whatever they entered. Because of this, you can use the IF command to check if the number that was entered is valid. It also allows you to GOTO a certain label based on what was chosen. For this example, string comparison will be used.

String comparison is simply comparing two variables or two strings or one variable and one string to see if they are equal.

IF "%CHOICE%"=="2" A:
IF "%CHOICE%"=="3" CD C:\

LABEL - The best (and only) way to effectively manage what code gets executed, and when. Labels are made by putting text on a line of its own and preceding it with a semicolon (:). It should be one word only. Examples are:


They can really be anything. They are used with GOTO.

GOTO - Used to "go to" the specified label. So, if your code read like this:


You would never see the word "H1!" as it is skipped. This can be used to create sections of code that handle errors, other sections to check for errors, etc.. It's a good idea to put the label "End" at the end of all your batchfiles, so whenever you need to terminate the batch file, you can just GOTO End, and you will effectively end the batch file.

Continued on next post...
[center][/center]"The geek shall inherit the Earth."


FOR - The FOR loop has a wide range of uses. It's main purpose is to execute a command for all files/directories that meet a certain standard. The syntax:

FOR {switch} %%variable in (set) do [command]

Switches available:

/D - Directories only; only directories are valid answers
/R - Executes command in all subdirectories. Syntax is a little different:
FOR /R [drive :][path] ...
The ... is the normal syntax for FOR.
/F - used to parse a file. The syntax for it is:
FOR /F "keywords" %%variable(s) in (filename) do [command]

Replace "keywords" with as many of these as you need:
eol=c - End Of Line character (one character). Lines with this at the beginning are a comment and not parsed. Replace "c" with desired character.
skip=n - Replace "n" with a number. n number of lines are skipped at the beginning of the file.
tokens=1,2,3-7 - Specifies which lines are parsed. At least two must be there. 1,2 would do first and second lines, 1,3 would leave out every second line, etc.. The "3-7" in the example would parse every fourth through seventh line. Place a * at the end if results are not what you expect; that may fix it.

%%variable is the first variable to be used. The number of tokens you use is how many variables are added by the FOR command. So, if you started with %%a and used tokens 1 and 2, you would have %%a, %%b and %%c.

(set) is whatever you want. It can be a text file (for use with /F), it can be a directory, or even a text string. It's up to you.

How /F works with tokens

In our example, we have the following:

FOR /F "tokens=1,2*" %%i in (file.txt)

%%i is set to the first line of file.txt, %%j is assigned to the next line, etc.. This is repeated until the file is parsed.

So, the correct code would be:

for /F "tokens=1,2*" %%i in (file.txt) do echo %%i %%j %%k

Assuming @ECHO OFF is at the top of the batch, the output would be to echo the entirety of the file.

Another example: Let's say you wanted to save all the file names in a directory into a text file, but don't want the clutter of a DIR. You could do this:

FOR %%A in (*.*) do ECHO %%A >> for.txt
[center][/center]"The geek shall inherit the Earth."


Graphical interfaces for batch files
So, you wanna make a cool looking box to put your batch file in? You want it to look something like this?:

Well, you need to use some extended characters in the ASCII Dos-Extended Character Set.

No matter how much you hate the EDIT window, you must use it to make a graphical interface. No exceptions.

It is advised that you hit the [Insert] key during graphical design so you don't end up with a vertical line moved a long way away from the table you are making. Also, there are many more extended characters that only work in EDIT. See the full list here.
[center][/center]"The geek shall inherit the Earth."


IV. Reference
All posts below this one are additional topics for reference on various "fads" commonly being asked about in the DOS/Programming forums at the time of writing. Because of the changing fads, all users will be welcome to add references to this thread, and I will add hyperlinks and adjust formatting as necessary (please respect the bold header for each post and underlined sub-topics, it makes it easier for me).

The DATE variable

If you need the date for anything at all, use the variable %DATE%. To find the date's format, type at any prompt:


As of this writing, the echo'd text is:

Sun 05/14/2006

But what if you only want part of this? You can use the ~ modifier to change what you get. The syntax is:

%[variable]:~[starting character],[number of characters]

For example, to just get the date without the day, you would use


The characters start at 0, like this:

Wed 02/14/2007

The code parses the DATE variable, starting with character 4 (the first character is zero). Since there is no comma and nothing after that, it simply finishes displaying the variable from that point.

What if you wanted to ECHO just the numbers, or use that as a file (05142006)? Well, try this:

ECHO %DATE:~4,2%%DATE:~7,2%%DATE:~10%

Doing this will echo just the date without any spacing at all.
[center][/center]"The geek shall inherit the Earth."