Doctor |
DOS |
¦ Batch File Basics ¦ | ¦ Advanced Batch
File Examples ¦ |
¦ Batch File Tips ¦ |
¦ Obtain 500+ Batch Files ¦ |
|
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.
|
:: 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
:: 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
:: 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 DRDAF.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
:: 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 :: :: 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
:: MCD.bat :: Makes and Changes to the Specified Directory :: @ECHO OFF CLS MD %1 CD %1 ________MDEL.bat
:: 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 (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
:: 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
:: 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
:: 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 |