Batch File Examples - A Doctor DOS Tutorial
Batch File Examples - A Doctor DOS Tutorial
Doctor
DOS
Betamax's
DOS
BATCH FILE EXAMPLES
Preliminary
Read This First!
PRELIMINARY
Here are the first example batch files. They will not necessarily be explained line by line; the title remarks
should suffice so you'll understand their operation, although some will be preceded by a syntax example.
Explanatory notes to the right are not part of the file. Do not type them in. For further understanding, review
Batch File Basics.
The "DR" command is a batch file that runs an after-market program that I use called Color Directory. It is
used with batch files to confirm that an operation has happened. You may substitute the DOS `DIR' or `XDIR'
command with its switches set to your preferences, or use any directory display program of your choosing.
Note that full paths to commands used in these batch files have not always been shown. This is to reduce
confusion for batch-file newbies because there paths to such programs will likely differ from mine. To be fully
efficient, any program called or run from a batch file should be preceded by its full path.
Be aware that Doctor DOS will not be responsible for any problems encountered through the use or mis-use
of anything presented here.
www.chebucto.ns.ca/~ak621/DOS/BatExamp.html 1/8
16/12/2022, 10:02 Batch File Examples: A Doctor DOS Tutorial
EXCEPT FOR THE BATCH FILES THEMSELVES,
INFORMATION ON THIS BATCH FILE PAGE MAY
NOT BE REPRODUCED WITHOUT PERMISSION
FROM THE AUTHOR ©
CDD.bat
CLU.bat
:: CLU.bat
::
:: Moves Up One Directory Level
:: Displays Directory on a Cleared Screen.
::
@ECHO OFF
CFB.bat
www.chebucto.ns.ca/~ak621/DOS/BatExamp.html 2/8
16/12/2022, 10:02 Batch File Examples: A Doctor DOS Tutorial
:: Copies All or Specified Files From the
:: B Drive Root to the Current Directory
:: (A B-Drive Sub-Directory Location May be Specified)
:: (Hidden Files are Excepted.)
::
@ECHO OFF
ECHO. Leaves a blank line for separation.
CTB.bat
________
DAF.bat
:: DAF.bat
::
:: Deletes All files in the Current Directory
:: With Prompts and Warnings
::
:: (Hidden, System, and Read-Only Files are Not Affected)
::
@ECHO OFF
DAF.bat
(Variation)
:: DAF.bat (Variation)
::
:: Deletes All files in the Current Directory
www.chebucto.ns.ca/~ak621/DOS/BatExamp.html 3/8
16/12/2022, 10:02 Batch File Examples: A Doctor DOS Tutorial
:: Skips "Are You Sure?" and Other Messages
:: (Hidden, System, and Read-Only Files are Not Affected)
::
@ECHO OFF
________
DELE.bat
DELT.bat
www.chebucto.ns.ca/~ak621/DOS/BatExamp.html 4/8
16/12/2022, 10:02 Batch File Examples: A Doctor DOS Tutorial
:NO-DIRECTORY
ECHO.
ECHO No Directory Specified
ECHO.
:END
________
MCD.bat
("Make/Change Directory)
Syntax: MCD (File Name)
:: MCD.bat
:: Makes and Changes to the Specified Directory
::
@ECHO OFF
CLS
MD %1
CD %1
________
MDEL.bat
This batch file uses the DOS "FOR-IN-DO" (FOR) command and replaceable parameters. Basically, it means "FOR
each Item INside the Parentheses, DO the given command". In this case, it will take each file name you give at the
command line and substitute it for one of the percent-numbers. These percent-numbers are replaceable parameters, with
"%1" representing the first file name, "%2, the second, and so on. Wild card characters, ` ? ' and ` * ', may be used in file
names.
The batch file deletes each item inside the parentheses, which will be those file names you typed at the command line.
Each file name is substituted for one of the percent numbers. You may specify up to nine file names or groups, but the total
must not exceed the character limit of your command line.
MDEL.bat (Improved)
This version allows one to type as many file names as the command line can hold. It uses the "SHIFT" command. This
permits each file name on the command line to move down one number to become the first replaceable parameter. Thus,
the second file name will become "%1" after the SHIFT command is issued, the third file name becomes "%2", and so on.
After yet another SHIFT command, the third file name will be in position "one" (%1). As long as there are file names left
on the command line, they will be shifted one at a time into position number "one". Then, each is deleted in turn with an
on-screen message to that effect being displayed. The "IF NOT..." statement says that as long as `%1' is not equal to
nothing (that is, equal to something), the batch file is to return to the "AGAIN" label.
Finally, when no file names are left, the "IF NOT" statement becomes false because "%1" will by then actually be equal
to nothing. Thus the batch file does not loop back up to "AGAIN" and instead goes on to display the directory listing
confirming that the files are gone.
________
MU.bat
:MOVE-ALL
MOVE /-Y *.* ..
GOTO END
:MOVE-SPEC
FOR %%F IN (%1 %2 %3 %4 %5 %6 %7 %8 %9) DO MOVE /-Y %%F ..
:END
ECHO. Adds a Blank Line to the Display.
F:\BATCH\DR.BAT
________
This allows one to move up to nine files or file groups into the parent directory one level up. I use it because I have
many directories in which there is a WORK subdirectory. After doing my work, I want to move the completed files into the
parent directory and use this batch file to do so. The "/-Y" will prompt you if any files in the parent directory are about to
be overwritten. You may choose to overwrite or not. The batch file will then resume and go on to the next file. (Be aware
that some versions of MOVE do not recognise this switch and will overwrite without prompting.)
You may modify this batch file into CU.bat (Copy Up) by replacing the MOVE commands with:
or
Note that the "Overwrite" switch comes at the end of the line when COPY is used.
________
www.chebucto.ns.ca/~ak621/DOS/BatExamp.html 6/8
16/12/2022, 10:02 Batch File Examples: A Doctor DOS Tutorial
SDEL.bat
ECHO.
IF "%1" == "" ECHO No File(s) Specified! Prompts if No File
ECHO. is Given.
GOTO END
:DISPLAY
ECHO %0 %1 Gives the Batch File Name
ECHO. and File to be Deleted.
ECHO These Files Will Be Deleted:
ECHO.
DIR %1 | FIND "Directory" Displays the Path and
DIR %1 /B /P Files to be Deleted.
ECHO.
ECHO To Delete Listed Files, Allows the User to
ECHO Press Any Key Continue or Abort.
ECHO.
ECHO To Cancel, Press: `Control-C'
ECHO.
:END
ECHO. Adds a Blank Line to the Display.
C:\BATCH\DR.BAT Confirms the Operation.
STEP.bat
("Step" Bat)
(Requires MS-DOS 6.2 or Newer)
Syntax: STEP (Batch File Name with No Extension, Parameters)
:: STEP.bat
:: Allows one to Step through a Batch File
:: To Test Each Line
::
@ECHO OFF
COMMAND /Y /C %1.bat %2 %3 %4
:END
________
This simple example allows one to run a batch file a line at a time to test it. It runs another copy of the DOS
COMMAND.com. The "/Y" switch is what does the stepping. It displays each line and asks if you wish to run it or not by
pressing "Y" or "N". You may exit this procedure at any time by pressing "CONTROL-C". Also, by pressing "Escape", the
batch file will continue on its own from the current line. Realise that some DOS versions will not respond to these key
www.chebucto.ns.ca/~ak621/DOS/BatExamp.html 7/8
16/12/2022, 10:02 Batch File Examples: A Doctor DOS Tutorial
commands in this situation. As an example, DR-DOS will ignore "CONTROL-C" and "ESCAPE", instead it simply passes
by each remaining line as though `N' was being pressed.
The "/C" switch runs the specified command and then returns to the base COMMAND.com -- either after the stepping
procedure finishes, or after pressing "CONTROL-C".
When running this batch file, don't type the ".bat" extension. STEP.bat does that for you via the "%1.bat" replaceable
parameter. If the batch file requires additional parameters, you may specify up to three via the " %2 %3 %4" replaceable
parameters. Here's a syntax example:
STEP SDEL TEST.txt
This will step through the "SDEL" batch file using "TEST.txt" as SDEL's file parameter. (SDEL.bat was presented here
as the previous example batch file.)
Remember:
To have batch file operations run
quickly, specify full paths for all
external DOS commands.
www.chebucto.ns.ca/~ak621/DOS/BatExamp.html 8/8