Sunday, September 14, 2025

Learn MS-DOS (Part 8) - Batch Programming in MS-DOS

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

ECHO [message] ECHO ON ECHO OFF

Examples:

ECHO Hello Daddy, welcome to DOS!

Prints a message to the screen.

@ECHO OFF

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:

PAUSE

Example:

ECHO Insert a disk and press any key to continue... PAUSE

🔹 3. REM – Add Comments

Purpose: Adds remarks (comments) inside batch files. Comments are ignored by DOS.

Syntax:

REM [comment]

Example:

REM This batch file backs up documents COPY C:\Docs\*.* D:\Backup

🔹 4. IF – Conditional Execution

Purpose: Runs commands only if a condition is true.

Syntax:

IF [condition] command

Examples:

IF EXIST notes.txt COPY notes.txt D:\Backup

Copies notes.txt only if it exists.

IF NOT EXIST data.txt ECHO File not found!

🔹 5. GOTO – Jump to Label

Purpose: Redirects execution to a labeled section of a batch file.

Syntax:

GOTO [label]

Example:

:START ECHO Welcome! GOTO END :END ECHO Goodbye!

🔹 6. CALL – Run Another Batch File

Purpose: Runs another batch file from within a batch file and returns after it finishes.

Syntax:

CALL [batchfilename]

Example:

CALL backup.bat

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:

SHIFT

Example:
If you run:

MYFILE.BAT one two three

Inside the batch file:

  • %1 = one, %2 = two, %3 = three
    After SHIFT, %1 = two, %2 = three.


🔹 8. FOR – Loop Through Files

Purpose: Repeats commands for a list of files.

Syntax:

FOR %%variable IN (set) DO command

Example:

FOR %%f IN (*.txt) DO COPY %%f D:\Backup

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:

@ECHO OFF ECHO Starting backup... IF NOT EXIST D:\Backup MD D:\Backup FOR %%f IN (*.txt) DO COPY %%f D:\Backup ECHO Backup complete! PAUSE

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

Learn Microsoft Word (Part 1) - Introduction to Microsoft Word

🖥 What is Microsoft Word? Microsoft Word is the world’s most popular word processing program . It’s used to create documents such as lette...