What Do BAT Files Do? Scripting Explained
In this article
BAT files are plain-text scripts that run a sequence of Windows commands, letting you automate repetitive tasks like copying files, launching programs, or cleaning up folders with a single double-click. They have a .bat extension and are executed by the Windows command interpreter (cmd.exe), which reads each line and runs it as if you had typed it into the Command Prompt.
Background: Where BAT Files Came From
BAT files date back to the early days of MS-DOS, the operating system that preceded modern Windows. In the 1980s, DOS had no graphical interface—users typed commands like copy or dir at a prompt. To avoid retyping the same commands repeatedly, users saved them in a text file with a .bat extension. The name comes from the file extension itself, which is short for “batch.” A batch file processes a whole batch of commands in order, without requiring user input between each one.
When Windows arrived, it kept the command interpreter for compatibility, so BAT files continued to work. Even today, Windows 10 and 11 still run them with the same basic syntax, though PowerShell has largely replaced them for advanced automation.
How BAT Files Work
A BAT file is just a text file with commands on separate lines. When you run it, cmd.exe reads each line and executes it as if you had typed it into a Command Prompt window. For example, a file containing:
@echo off
echo Hello, world!
pause
would print “Hello, world!” and wait for you to press a key before closing. The @echo off line hides the commands themselves from the output, and pause keeps the window open so you can read the result.
You can also use variables, loops, and conditional logic. For instance, for %%f in (*.txt) do type %%f would display the contents of every text file in the current folder. The if command allows branching, like if exist file.txt (echo Found) else (echo Missing). These features make BAT files surprisingly powerful for a simple text format.
Common Uses of BAT Files
People use BAT files for a wide range of everyday tasks. Some typical examples include:
- File management: Copying, moving, renaming, or deleting files in bulk.
- Launching programs: Starting several applications at once, like opening your email client and browser together.
- System maintenance: Cleaning temporary files, flushing DNS, or checking disk space.
- Backups: Copying important folders to an external drive or network location.
- Automating builds: Developers often use BAT files to compile code or run tests.
For example, a simple backup script might look like:
@echo off
xcopy C:\Documents D:\Backup /E /I /Y
echo Backup complete.
This copies everything in Documents to a Backup folder on the D: drive, including subfolders, and overwrites existing files without asking.
How to Create and Run a BAT File
Creating a BAT file is straightforward:
- Open Notepad (or any text editor).
- Type your commands, one per line.
- Save the file with a
.batextension, for examplemyscript.bat. - Double-click the file to run it, or type its name in Command Prompt.
One common mistake is saving the file as myscript.bat.txt because Notepad adds .txt by default. To avoid this, choose “All Files” in the Save As dialog and type the full name with quotes, like "myscript.bat".
You can also run a BAT file from Command Prompt by navigating to its folder and typing its name. If the file name contains spaces, you need to put it in quotes, for example "my script.bat".
BAT vs. CMD vs. PowerShell
BAT files are often confused with CMD files, but they are nearly identical. The only real difference is that CMD files were designed for the newer OS/2 and Windows NT command interpreter, while BAT files originated in DOS. In practice, both run the same commands on modern Windows.
PowerShell is a more advanced scripting language that can do everything BAT files can, plus much more—like working with objects, accessing the registry, and managing system services. However, PowerShell scripts have a .ps1 extension and require a change in execution policy before they can run. For simple tasks, BAT files are easier to write and don’t require any extra setup.
Safety and Risks
BAT files are powerful, which means they can also be dangerous. Because they run commands automatically, a malicious BAT file could delete files, change system settings, or install malware. Always be cautious when opening a BAT file from an unknown source, just as you would with any executable file.
To stay safe:
- Only run BAT files you created or that come from a trusted source.
- Open the file in a text editor first to see what it does.
- Test new scripts on a non-critical folder or a virtual machine.
- Avoid running BAT files from email attachments or random websites.
If you accidentally run a harmful BAT file, you may not be able to undo the changes, so it’s better to inspect it beforehand.
Frequently Asked Questions
Can BAT files run on macOS or Linux?
No, BAT files are designed for Windows. However, you can run them on a Mac or Linux using Wine or a virtual machine, but they won’t work natively. For cross-platform scripting, people usually use shell scripts (for Unix) or Python.
How do I stop a BAT file that is stuck in a loop?
Press Ctrl+C in the Command Prompt window to interrupt the script. If that doesn’t work, you can close the window by clicking the X, but that may leave background processes running.
Can I schedule a BAT file to run automatically?
Yes, you can use Task Scheduler in Windows to run a BAT file at a specific time, on startup, or when certain events occur. This is a common way to automate backups or maintenance.
Why does my BAT file close immediately after running?
This usually happens because the script finishes and the Command Prompt window closes. To see the output, add the pause command at the end of the file, which will keep the window open until you press a key.
Can I convert a BAT file to an EXE?
Yes, there are third-party tools that can wrap a BAT file into an executable, but they essentially just run the same commands. The resulting EXE may be flagged by antivirus software, so it’s often simpler to keep the BAT file as is.