🖥 Introduction
Typing commands one at a time is fine — but what if you need to run 10 commands in a row every day? Instead of repeating them manually, MS-DOS lets you use batch files.
A batch file is just a text file with a .BAT
extension containing DOS commands. When you run it, DOS executes the commands line by line. Batch files were the first step toward automation on personal computers.
In this part, we’ll cover the most important batch programming commands and how to use them to write simple scripts.
🔹 1. ECHO – Display Messages
Purpose: Displays text or controls whether commands are shown as they run.
Syntax:
Examples:
Prints a message to the screen.
Turns off command display — commonly used at the start of batch files for a cleaner look.
🔹 2. PAUSE – Halt Until Key Pressed
Purpose: Stops execution until the user presses a key.
Syntax:
Example:
🔹 3. REM – Add Comments
Purpose: Adds remarks (comments) inside batch files. Comments are ignored by DOS.
Syntax:
Example:
🔹 4. IF – Conditional Execution
Purpose: Runs commands only if a condition is true.
Syntax:
Examples:
Copies notes.txt
only if it exists.
🔹 5. GOTO – Jump to Label
Purpose: Redirects execution to a labeled section of a batch file.
Syntax:
Example:
🔹 6. CALL – Run Another Batch File
Purpose: Runs another batch file from within a batch file and returns after it finishes.
Syntax:
Example:
Runs backup.bat
and then returns to the original batch file.
🔹 7. SHIFT – Handle Multiple Parameters
Purpose: Changes the position of command-line parameters passed to a batch file.
Syntax:
Example:
If you run:
Inside the batch file:
-
%1 = one
,%2 = two
,%3 = three
AfterSHIFT
,%1 = two
,%2 = three
.
🔹 8. FOR – Loop Through Files
Purpose: Repeats commands for a list of files.
Syntax:
Example:
Copies all .txt
files to D:\Backup one by one.
🧰 Sample Batch File
Here’s a simple batch file that backs up text files and pauses:
Save as backup.bat
and run it.
✅ Conclusion
Batch programming makes DOS much more powerful. With commands like ECHO
, IF
, GOTO
, FOR
, and CALL
, you can create automated scripts that save time and effort.
👉 In Part 9, we’ll explore Advanced Commands like ATTRIB, DEBUG, SUBST, and FC — the powerful but less commonly used tools in DOS.
No comments:
Post a Comment