MS-DOS: Advanced Commands

 

💥 Level Up with MS-DOS: Advanced Scripting & Batch Files

So, you've learned the basics of MS-DOS—commands like DIR, COPY, CD, and more. That’s awesome! But what if I told you that MS-DOS could automate tasks, create mini-programs, and even make decisions?

Welcome to the world of batch files and advanced scripting.

If basic DOS is like texting your computer, batch files are like teaching it to follow your script—step by step, automatically.


📁 What is a Batch File?

A batch file is a text file with .BAT or .CMD extension that contains a list of DOS commands. When you run it, the commands are executed in order, one after another.

Think of it like a playlist of commands.

Example:

bat

@echo off
echo Hello, World!
pause

Save that as hello.bat, double-click it, and boom—your first batch script is alive!


🧠 Why Use Batch Files?

  • ✅ Automate repetitive tasks
  • ✅ Create installers or setup tools
  • ✅ Perform backups or cleanups
  • ✅ Launch programs with conditions
  • ✅ Customize boot processes (especially in old systems)


🔧 Key Concepts in Advanced MS-DOS Scripting

Let’s get into the powerful stuff. We’ll cover:

  1. ECHO and @ECHO OFF
  2. Variables
  3. User Input
  4. Conditional Statements (IF)
  5. Loops (FOR)
  6. GOTO and Labels
  7. Error handling
  8. Real-world automation examples


🔹 1. ECHO and @ECHO OFF

By default, every command in a batch file is shown. Use @ECHO OFF to hide the commands and keep the output clean.

bat

@echo off
echo Welcome to the script!

🔹 2. Variables in Batch Files

You can store and use variables using the SET command.

bat

@echo off
set NAME=Deepak
echo Hello, %NAME%!

🧠 Note: Use %VARIABLENAME% to call the value.


🔹 3. Taking User Input with SET /P

Let users interact with your script.

bat

@echo off
set /p name=What is your name?
echo Welcome, %name%!

This makes your script dynamic!


🔹 4. IF Statements (Conditionals)

Batch files can make decisions using IF.

bat

@echo off
set /p age=Enter your age:
if %age% GEQ 18 (
echo You are an adult.
) else (
echo You are underage.
)

Other comparisons:

  • == (equals)

  • NEQ (not equal)

  • LSS (less than)

  • GTR (greater than)

  • LEQ (less than or equal)


🔹 5. FOR Loops

Use FOR to repeat actions over a set of values.

bat

@echo off
for %%x in (apple banana mango) do (
echo Fruit: %%x
)

Or loop through files:

bat

@echo off
for %%f in (*.txt) do (
echo Found file: %%f
)

🔹 6. GOTO and Labels

You can jump to specific parts of your script using GOTO.

bat

@echo off
echo 1. Start
goto menu
:menu
echo 2. In Menu

Useful for building menus, loops, or skipping steps.


🔹 7. Error Handling with ERRORLEVEL

Check if a command succeeded or failed.

bat

@echo off
ping localhost
if errorlevel 1 (
echo Something went wrong!
) else (
echo Ping successful.
)

🚀 Real-World Batch File Examples

Here are some cool, practical uses.


✅ 1. Auto Backup Script

bat

@echo off
set source=C:\MyFiles
set destination=D:\Backup
xcopy %source% %destination% /s /y
echo Backup Complete!
pause

✅ 2. Clean Temp Files

bat

@echo off
echo Deleting temp files...
del /s /q C:\Windows\Temp\*.*
echo Done!
pause

✅ 3. Shutdown Timer

bat

@echo off
set /p time=Enter time in seconds to shutdown:
shutdown -s -t %time%
echo Your system will shutdown in %time% seconds.
pause

✅ 4. Interactive Menu System

bat

@echo off
:menu
cls
echo === MAIN MENU ===
echo 1. Say Hello
echo 2. Show Date
echo 3. Exit
set /p choice=Enter your choice:
if "%choice%"=="1" goto hello
if "%choice%"=="2" goto date
if "%choice%"=="3" goto end
goto menu
:hello
echo Hello there!
pause
goto menu
:date
echo Today's date is: %date%
pause
goto menu
:end
echo Goodbye!

🧱 Bonus: Creating Your Own Command Utility

Want to create a command like mycopy?

1. Create a file called mycopy.bat.
2. Write this:

bat

@echo off
xcopy %1 %2 /s /y

3. Save it in a folder like C:\MyScripts
4. Add that folder to your system's PATH

Now you can run mycopy C:\Stuff D:\Backup from anywhere!


🧠 Final Thoughts: Why Learn MS-DOS Scripting in 2025?

Even in the age of AI, scripting matters.

  • It's lightweight and instant.
  • Works in offline or recovery environments.
  • A great way to understand automation logic.
  • Helps with legacy systems, old games, and custom tasks.

And let’s be real: there’s something incredibly satisfying about making a computer do exactly what you want with a few lines of plain text.


✍️ Ready to Create Your Own Batch Magic?

Start small: build a greeting script, a backup tool, or an auto-launcher. Then play with conditionals, loops, and user input.

Soon, you'll be building powerful tools with nothing but .BAT files—and a little imagination.


🔚 Summary Cheat Sheet

CommandDescription
SETCreate a variable
SET /PGet user input
IFMake decisions
FORLoop over items
GOTOJump to labels
ERRORLEVELCheck the command success
ECHODisplay text
PAUSEPause the script

🎬 Next Up: Want to build your own DOS-based game or retro menu interface? Or learn how to convert .BAT to .EXE?

Let me know, and I’ll write that next post too. 😊

Post a Comment

Previous Post Next Post