User Agents

Web browsers and everyday web tech, explained

Batch File Extension: What Is .bat?

· 5 min read · Extensions

In this article
  1. Background: Where Batch Files Come From
  2. How a Batch File Works
  3. Choosing Between .bat and .cmd
  4. Common Problems and Misconceptions
  5. Alternatives to Batch Files
  6. Safety and Best Practices
  7. Frequently Asked Questions

The file extension for a batch file is .bat (for MS-DOS and Windows) and, less commonly, .cmd (for Windows NT-based systems). Both are plain-text files containing a sequence of commands that the operating system executes in order via a command-line interpreter. The .bat extension is the standard and most widely recognized; .cmd is a functional alternative introduced later. This article explains what these extensions mean, how they work, how to choose between them, and common pitfalls.

Background: Where Batch Files Come From

Batch files date back to the early 1980s with MS-DOS. The name “batch” comes from the idea of batching multiple commands together into a single file, so you don’t have to type them one by one. The .bat extension was chosen by Microsoft for its simplicity—three letters, matching the 8.3 filename convention of the time (e.g., START.BAT). When Windows NT arrived in 1993, Microsoft introduced the .cmd extension to distinguish files processed by the new cmd.exe interpreter from those processed by the older command.com. Both extensions are still supported in modern Windows, but .bat remains the default and most familiar.

How a Batch File Works

A batch file is just a text file with one command per line. When you run it (by double-clicking or typing its name in a command prompt), the interpreter reads each line and executes it as if you had typed it manually. Common commands include echo, copy, del, if, for, and goto. For example, a file named cleanup.bat might contain:

@echo off
del /q C:\temp\*.*
echo Done!

The @echo off hides the commands themselves, showing only the output. The file runs top to bottom unless you use control-flow commands like goto or if. Because they’re plain text, you can create them with any text editor (e.g., Notepad) and save with the .bat extension.

Choosing Between .bat and .cmd

For most purposes, .bat and .cmd are interchangeable. However, there is a subtle difference in how the two are processed by the command interpreter. The .cmd extension is recognized only by cmd.exe (the default on Windows NT and later), while .bat is recognized by both cmd.exe and the older command.com (used in MS-DOS and early Windows). In practice, this means:

  • .bat – Works everywhere, including legacy DOS environments. Use it if you need maximum compatibility.
  • .cmd – Works only in Windows NT-based systems (Windows 2000 and later). It is sometimes preferred for scripts that rely on newer features, but there is no functional advantage for most users.

If you are writing a script for modern Windows, either works. If you might run it in a DOS emulator or older system, stick with .bat. There is no performance difference—both are interpreted the same way by cmd.exe.

Common Problems and Misconceptions

One frequent issue is that Windows hides file extensions by default, so a file named script.bat appears as script. If you rename it to script.txt accidentally, it won’t run. To avoid this, enable “File name extensions” in File Explorer’s View tab.

Another misconception is that .bat files are always safe. They are not—they can contain any command, including destructive ones like format C: or del C:\*.* /s. Never run a batch file you didn’t create or don’t trust, and always read its contents first with a text editor.

Also, batch files are case-insensitive: START.BAT and start.bat are the same. However, the @echo off line must be exactly that (though the command names themselves are case-insensitive).

Alternatives to Batch Files

While .bat is simple, it has limitations: no built-in error handling, limited string manipulation, and no object-oriented features. For more complex automation, consider:

  • PowerShell scripts (.ps1) – More powerful, with access to .NET and full scripting language features.
  • Windows Script Host (.vbs, .js) – For older VBScript or JScript automation.
  • Python or other languages – If you need cross-platform support or complex logic.

For simple tasks like copying files or launching programs, a batch file is often the quickest and most portable solution. For anything involving regular expressions, APIs, or complex data processing, PowerShell is usually a better choice.

Safety and Best Practices

Because batch files execute commands with the privileges of the user who runs them, they can be dangerous if misused. Follow these guidelines:

  • Always test on a non-critical system or in a virtual machine.
  • Use echo off and include pause at the end if you want to see output before the window closes.
  • Avoid using format, del, or rd unless you are absolutely sure of the paths.
  • Keep backups of important files before running any script that modifies them.
  • If you receive a .bat file from someone else, open it in Notepad first to inspect its contents.

There is no cost to creating batch files—they are plain text and require no special software. But the cost of a mistake can be high, so caution is essential.

Frequently Asked Questions

Can a batch file have a .cmd extension instead of .bat?

Yes, .cmd is a valid extension for batch files on Windows NT-based systems. It behaves almost identically to .bat, but is only recognized by cmd.exe, not the older DOS interpreter. For modern Windows, either works.

How do I create a batch file?

Open Notepad (or any text editor), type your commands, and save the file with a name ending in .bat (e.g., myscript.bat). Make sure “Save as type” is set to “All Files” so Notepad doesn’t append .txt.

Why won’t my batch file run?

Common reasons: the file is actually saved as .txt (Windows hides extensions), the file contains syntax errors, or you lack permission to execute it. Check the extension, open the file in a text editor, and try running it from the command prompt to see error messages.

Is .bat the same as .exe?

No. A .bat file is a text script that is interpreted, while an .exe is a compiled binary program. Both can be run by double-clicking, but .exe files are machine code and generally faster and more complex.

Can I run a batch file on Linux or macOS?

Not directly, because batch files use Windows command syntax. However, you can run them using Wine or a DOS emulator, or you can rewrite the logic in Bash (with .sh extension) for Unix-like systems.