(Image: DOS Logo)


Doctor
DOS
Betamax's

DOS
BATCH FILE EXAMPLES


******


Preliminary
Read This First!

(Skip to the Batch Files)


Batch File Examples
Table of Contents

CDD.bat
Change/Display
Directory


CLU.bat
Clear Screen,
Move Up


CFB.bat
Copy from the
`B' Drive


CTB.bat
Copy to the
`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 File
Delete


MU.bat
Move Files Up
One Level


SDEL.bat
Safely
Delete Files


STEP.bat
Test Your
Batch Files



 

  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.



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.


 

EXCEPT FOR THE BATCH FILES THEMSELVES,
INFORMATION ON THIS BATCH FILE PAGE 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.


  THE BATCH FILES

 
*    CDD.bat

("Change/Display Directory" 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
                                     that You Type at the Command Line.
ECHO.                             Adds a Blank Line to the Display.
C:\BATCH\DR.BAT
                                 ________

  *    CLU.bat

("Clear Screen -- Move Up" Bat)

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

CD..                              `..' Represents the Parent Directory
CLS
ECHO.                             Adds a Blank Line to the Display.
C:\BATCH\DR.BAT
                                 ________


  *    CFB.bat

("Copy From `B'" Bat)
(Substitute a USB flash-drive
letter to copy from it.)

Syntax: CFB (Optional File name)
:: CFB.bat
:: 
:: 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.

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.BAT                  If there is a file name, only   
                                  it will be copied.
                                 ________
                               
                               
  *    CTB.bat

("Copy to `B'" Bat)
(Substitute a USB flash-drive
letter to copy to it.)

Syntax: CTB (Optional File Name)
:: CTB.bat
:: 
:: Copies All or Specified Files to a B-Drive Floppy
::  (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.BAT B:\                 copied.
                                 If there is a file name, only
                                    it will be copied.
                                        

                                 ________


  *    DAF.bat

("Delete All Files" 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)

("Delete All Files" Bat)

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

                                 ________


  *    DELE.bat

("Delete Except..." Bat)
Syntax: DELE (File name) to Not be deleted)
:: DELE.bat
:: 
:: Deletes Directory Entries Except for Specified File(s)
:: Wildcards (* and ?) may be Used in the File Name
::  (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.BAT               Displays the Results of the Operation.

    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

("Delete Tree" 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.BAT
GOTO END                         Directs DOS to End the Batch File 
                                    Operation.

: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

("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.BAT                   Confirms the Operation.

    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)

("More Powerful "Multiple Delete"" Bat)
Syntax: MDEL (File Name File Name File Name, etc.)
:: MDEL.bat (Improved)
:: 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.BAT                   Confirms the Operation.

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

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

PAUSE >: NUL

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

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

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

                                 ________


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

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




Batch Basics
Obtain 500+ Batch Files
Batch Tips
Advanced Batch I
Advanced Batch II
Advanced Batch III



Main DOS Page