HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to MS-DOS BatchVariablesConditions (IF)Loops (FOR)File OperationsParameters and Subroutines
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
🔀 Condition
🔁 Loop
⚙️ Commands & Flow
MS-DOS›Introduction to MS-DOS Batch

Introduction to MS-DOS Batch

The first .bat file, printing with ECHO and clean output with @echo off.

Simple enough for a 5-year-old to understand

Your first batch file

A batch file is a text file where the commands you would type one by one into the command line are listed line by line. ECHO prints text to the screen.

Hello
1
@echo off
Hides the commands themselves, only the output is visible.
DefinitionHides the commands themselves, only the output is visible.
2
echo Hello!
Prints to the screen.
Output / InputPrints to the screen.
3
pause
Waits until a key is pressed.
CodeWaits until a key is pressed.
DefinitionOutput / InputCode
💡 Hint: Save the file with a .bat extension; you can run it by double-clicking.

Blank line and comment

Example code
1
REM This is a comment line
Lines starting with REM do not run.
CommentLines starting with REM do not run.
2
echo.
Echo ending with a dot prints a blank line.
Output / InputEcho ending with a dot prints a blank line.
3
echo Finished
CommentOutput / Input
💡 Hint: Instead of REM, a colon can also be used for comments.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (2 left)

1.Which command prints text to the screen in a batch file?
2.What does @echo off do?

About this topic

The MS-DOS command language is based on executing commands written in a file sequentially. These files are called batch files; it was the primary way to manage a computer before graphical interfaces and still runs on Windows today.

The language was not designed as a programming language; it started as a way to save what you typed into the command line, and support for conditions, loops, and variables was added over time. Therefore, its syntax is inconsistent and it has unexpected rules.

Nevertheless, batch files are still used for: installation steps, backup tasks, build scripts, and server maintenance jobs. Since they run on Windows without installing anything extra, they are the lowest-friction automation tool.

What you will learn

  • Batch file structure
  • Disabling command echo
  • Printing to the screen
  • writing comments
  • Terminating the script

Who is this for?

  • Those automating tasks on Windows
  • Maintainers of legacy installation scripts
  • Learners of the command line

When to use?

For simple automations that run a few commands in sequence, a batch file is the fastest solution; it requires zero dependencies.

In cases requiring complex logic, error handling, and text processing, PowerShell is significantly more suitable; batch files become unreadable very quickly in these areas.

Common mistakes

Wrong

echo off
echo Hello

The echo-off command itself is printed to the screen because there is no at sign at the beginning of the line. The at sign prevents that line from being echoed and is traditionally used on the first line of every script. It's a small detail, but it ensures the output looks clean.

Correct

@echo off
echo Hello

Wrong

@echo off
echo Hello
echo.
echo World
rem This is a comment
: This should also be a comment

A single colon defines a label, not a comment; trying to define an empty label may result in an error or affect the script's flow. For comments, either the comment command or a double colon is used.

Correct

@echo off
echo Hello
echo.
echo World
rem This is a comment
:: This is also a comment

Wrong

@echo off
echo Operation complete
exit

When the exit command is used without parameters, it closes not only the script but also the command window; if you called the script from another script, the calling one terminates as well. To exit only that script, the script mode parameter must be added.

Correct

@echo off
echo Operation complete
exit /b 0

Frequently asked questions

Which file extension should it be?

Both extensions work, but they have differences: one runs in legacy compatibility mode, while the other runs with the command processor's current behavior and handles error codes more consistently. The latter is preferred for new scripts.

Are commands case-sensitive?

Commands and variable names are not case-sensitive. File paths are also not case-sensitive on Windows. However, letter case matters in text comparisons; a separate option must be added to the condition for case-insensitive comparison.

How do I run the script?

Typing the file name in the command window is sufficient. If there are spaces in the path, you need to enclose the path in quotes. For operations requiring administrator privileges, it is mandatory to open the window as an administrator.

Summary

Echo is disabled on the first line.

The at sign prevents that line from being echoed.

Two colons are used for comments.

A single colon defines a label.

A parameter is required to exit the script.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code