Doctor
DOS Betamax's

DOS
BATCH FILE TUTORIAL


EXAMPLES


(Image: DOS Logo)

¦ Batch File Basics ¦ ¦ Advanced Batch File Examples ¦
¦ Batch File Tips ¦
  ¦ Obtain 500+ Batch Files ¦  



 
    Here are sample batch files. They will not be explained line by line, except for the more ambiguous ones. 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 explanation, see Batch File Basics, before reading this section.

    The "DR" command is a batch file that runs an after-market directory program I use. It is used with the batch files here to confirm that an operation has happened. You may substitute DOS' "DIR" command with its switches set to your preferences.

    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. To be fully efficient, any program called or gone to in 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.
 


An advisory to non-Canadians: Some characters shown in some batch
files here may not be able to be reproduced on your system unless
the Country Code is changed or you type them in as ASCII characters.
Consult your text editor/word processor manual to see how to do the latter.


Batch Files Presented
on This Page


CDD.bat  Change Directory,
               Display Directory

CLU.bat  Clear Screen,
               Move Up One Level

CFB.bat  Copy from `B' Drive

CTB.bat  Copy to `B' Drive

DAF.bat  Delete All Files

DELE.bat  Delete All Files Except

DELT.bat  Delete Tree

MCD.bat  Make/Change Directory

MDEL.bat  Multiple Delete

MU.bat  Move Files Up One Level

SDEL.bat  Safe Delete

STEP.bat  Test Your Batch Files




  *    CDD.bat

Syntax: CDD (Directory Name)
:: CDD.bat
::
:: Changes to a Specified Directory
:: Displays a File and Subdirectory List
::
@ECHO OFF

CD %1                             "%1" Represents the Directory Name
                                     You Type at the Command Line
ECHO.                             Adds a Blank Line to the Display
C:\BATCH\DR
                                 ________

  *    CLU.bat
:: CLU.bat
:: 
:: Moves Up One Directory Level
:: Displays Directory on a Cleared Screen.
::
@ECHO OFF

CD..
CLS
ECHO.                             Adds a Blank Line to the Display
C:\BATCH\DR
                                 ________


  *    CFB.bat

Syntax: CFB (Optional File name)
:: CFB.bat
:: 
:: Copies All or Specified Files From the 
::   B Drive Root to the Current Directory
::    (A Sub-Directory Location May be Specified)
::    (Hidden Files are Excepted.)
::
@ECHO OFF
ECHO.                            Leaves a blank line for separation
                                    between prompts and directory listing.
IF "%1" == "" XCOPY B:\*.*       If there is no file name, all 
IF NOT "%1" == "" XCOPY B:\%1       files in the B drive root will 
ECHO.                               be copied.
C:\BATCH\DR                      If there is a file name, only   
                                 it will be copied.
                                 ________
                               
                               
  *    CTB.bat

Syntax: CTB (Optional File Name)
:: CTB.bat
:: 
:: Copies All or Specified Files to a B-Drive Floppie
::  (Hidden Files are Excepted.)
::
@ECHO OFF

ECHO.                            Adds a Blank Line to the Display
IF "%1" == "" XCOPY *.* B:\      If there is no file name, all files 
IF NOT "%1" == "" XCOPY %1 B:\   in the current directory will be 
C:\BATCH\DR B:\                     copied.
                                 If there is a file name, only
                                    it will be copied.
                                        

                                 ________


  *    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

DEL .                           `.' Represents the Current Directory
DR


*    DAF.bat (Variation)
:: DAF.bat (Variation)
:: 
:: Deletes All files in the Current Directory
:: Skips "Are You Sure?" and Other Messages
::  (Hidden, System, and Read-Only Files are Not Affected)
::
@ECHO OFF

ECHO Y | DEL . > NUL      Echoes (sends) a "Yes" Answer to
                             the "Delete" Prompt and Hides
                                Other Messages.
ECHO.                     Adds a Blank Line to the Display
C:\BATCH\DR
           

                                 ________


  *    DELE.bat

Syntax: DELE (File name(s) to *NOT* be deleted)
:: DELE.bat
:: 
:: Deletes Directory Entries *Except* for Specified File(s)
:: Wildcards (* and ?) may be Used in File Names
::  (Hidden, System, and Read-Only Files are Not Affected)
::
@ECHO OFF

MD SAVE                         Makes a Temporary "SAVE" Directory.
XCOPY %1 SAVE > NUL             "> NUL" Suppresses On-Screen Messages.
ECHO Y | DEL . > NUL            Deletes all Files in the Current
                                   Directory showing no Prompts.

MOVE SAVE\*.* . > NUL           Returns Excepted File(s) to the
RD SAVE                            Current Directory.
                                Removes "SAVE" Directory
ECHO.                           Adds a Blank Line to the Display
C:\BATCH\DR                     Displays the Results of the Operation.

    To make this be more efficient, have the batch file create the "SAVE" directory on your RAM Drive. To see a variation of this batch file which will allow multiple files of differing names to be excepted, go to Advanced Batch Files.



  *    DELT.bat
(Requires DOS 6 or Newer)

Syntax: DELT (Directory Name)
:: DELT.bat
:: 
:: Deletes Specified Directory and All Files 
::    and Directories Below
:: Prompts "Are You Sure?" Before Deletion Commences.
::
@ECHO OFF

IF "%1" == "" GOTO NO-DIRECTORY  Prompts if No Directory was Specified

ECHO.                            Displays a Blank Line.
ECHO.                            Displays a Blank Line.

TREE %1                          Displays the Directory Structure
                                    to be Deleted.
DELTREE %1                       Deletes Directory Structure.
DR
GOTO END                         Directs DOS to End the Batch File 
                                    Operation.

:NO-DIRECTORY
ECHO.
ECHO   No Directory Specified
ECHO.

:END
                                 ________


  *    MCD.bat

Syntax: MCD (File Name)
:: MCD.bat
:: Makes and Changes to the Specified Directory
::
@ECHO OFF

CLS
MD %1
CD %1
                                 ________


  *    MDEL.bat
(Multiple Delete Bat)
Syntax: MDEL (File Name File Name File Name, etc.)
:: MDEL.bat
:: Allows Deletion of Up to Nine Files
::      with Different Names and Extensions
:: Wildcards are Permitted
::
@ECHO OFF

CLS                                    Clears the Screen
FOR %%F IN (%1 %2 %3 %4 %5 %6 %7 %8 %9) DO DEL %%F  See Text

ECHO.                                  Adds a Blank Line to the Display
C:\BATCH\DR                            Confirms the Operation

    This uses the "FOR-IN-DO" DOS command and replaceable parameters. Basically, it means "For each item inside the brackets, 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 brackets, 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.


  *    MDEL.bat
(More Powerful "Multiple Delete" Bat)
Syntax: MDEL (File Name File Name File Name, etc.)
:: MDEL.bat (mproved)
:: Allows Deletion of Multiple Files
::      with Different Names and Extensions
@ECHO OFF

CLS                                    Clears the Screen

:AGAIN                                 See Text
ECHO Deleting %1
DEL %1
SHIFT
IF NOT "%1" == "" GOTO AGAIN

ECHO.                                  Adds a Blank Line to the Display
C:\BATCH\DR                            Confirms the Operation

    This version allows one to type as many file names as the command line can hold. It uses the "SHIFT" command. That allows each file name on the command line to shift 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.

    Finally, when no file names are left, the statement "IF NOT "%1" == "" GOTO AGAIN" becomes false because "%1" will then be equal to nothing. Thus the batch file does not loop to "AGAIN" and instead goes on to display the directory listing confirming the files are gone.

                                 ________


  *    MU.bat
(Move Up Bat)

Syntax: MU (File Name File Name File Name, etc.)
:: MU.bat (Move Up)
:: Move All or Specified Files Up One Level
::
@ECHO OFF

If "%1" == "" GOTO MOVE-ALL
If NOT "%1" == "" GOTO MOVE-SPEC

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

    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.

    You may modify this batch file into CU.bat (Copy Up) by replacing the MOVE commands with:

COPY *.* .. /-Y

or

COPY %%F .. /-Y .

    Note that the "Overwrite" switch comes at the end of the line when COPY is used.

                                 ________


  *    SDEL.bat
(Safe Delete Bat)

Syntax: SDEL (File name)
:: SDEL.bat (Safe Delete)
:: Displays File to Be Deleted
:: Prompts to Delete File or Abort Operation
:: Wildcards May be Used to Delete File Groups
::
@ECHO OFF
CLS

IF NOT "%1" == "" GOTO DISPLAY

ECHO. 
IF "%1" == "" ECHO   No File(s) Specified!  Prompts if No File
ECHO.                                           is Given
GOTO END

:DISPLAY
ECHO        %0 %1
ECHO. 
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.

PAUSE > NUL

:DELETE                                Deletes Selected File(s)
DEL %1                                               
ECHO.

:END
ECHO.                                  Adds a Blank Line to the Display
C:\BATCH\DR                            Confirms the Operation

    An imnproved SDEL.bat may be found in Advanced Batch Files.

                                 ________


  *    STEP.bat
(Requires 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 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". The "/C" switch runs the specified command and then returns to the base COMMAND.com - either after it 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 make these batch files run faster,
specify full paths for all external
DOS commands.

For even faster operation, run the files
and DOS commands from a RAM drive.




¦ Batch File Basics ¦ ¦ Advanced Batch Files ¦ ¦ Batch File Tips ¦
Return to the
Main DOS Page
Obtain
500+ Batch Files
Go to
Richard Bonner's
Home Page