A bat file is a text file that tells Windows to run a series of commands automatically

A bat file (short for batch file) is a plain text document with a .bat or .cmd extension that contains a list of Windows commands. When you open it, Windows reads through the file line by line and executes each command in order, without you having to type them individually. It is the oldest form of automation on Windows — the system has used batch files since the 1980s.

The simplest bat file might contain just two or three lines. A more complex one could contain dozens of commands, loops, and conditional statements that run different commands depending on what happens. Because bat files are text, you can open and edit one in Notepad, and because Windows understands them natively, you do not need any special software to run them.

Bat files are useful when you need to repeat the same sequence of commands regularly, or when you want to automate a task that would otherwise require manual typing. They are also one of the easiest ways to learn how automation works on Windows, because you can see exactly what each line does.

Key Takeaways

  • A bat file is a text document containing Windows commands that run automatically when you open the file.
  • You can create and edit a bat file in Notepad or any plain text editor, then save it with a .bat or .cmd extension.
  • Bat files run commands in the order they appear, which makes them useful for repetitive tasks like copying files, renaming folders, or launching programs.
  • Windows executes bat files through Command Prompt, so understanding basic Command Prompt syntax is helpful but not required to start.

How to create and run a bat file

To create a bat file, open Notepad (or any plain text editor), type your commands, and save the file with a .bat extension instead of .txt. For example, if you want a bat file that creates a new folder and then opens it, you would type:

mkdir C:\MyFolder start C:\MyFolder

Save this as CreateFolder.bat (not CreateFolder.bat.txt — Windows will sometimes add .txt automatically, so check the file properties to be sure). Once saved, you can run the bat file by double-clicking it, or by right-clicking and selecting "Run as administrator" if the commands need higher permissions.

When you run the bat file, a Command Prompt window opens, executes each line, and then closes. If something goes wrong, the window may close too quickly to read the error. To keep it open longer, add this line at the end of your bat file: pause. This tells Windows to wait for you to press a key before closing the window, giving you time to see what happened.

Common commands you will see in bat files

Bat files use the same commands you would type into Command Prompt. Here are the ones that appear most often:

  • cd — changes to a different folder (directory)
  • dir — lists the files in the current folder
  • copy — copies a file from one location to another
  • del — deletes a file
  • mkdir — creates a new folder
  • start — opens a file or program
  • echo — prints text to the screen
  • pause — pauses execution until you press a key

A real bat file might combine several of these. For example, a bat file that backs up a folder could use cd to navigate to the source folder, copy to copy the files, and then echo to print a message saying the backup is complete.

The difference between .bat and .cmd files

Both .bat and .cmd files do the same thing — they run Windows commands in sequence. The difference is historical. The .bat extension dates back to MS-DOS, while .cmd was introduced with Windows NT and is technically more modern. In practice, Windows treats them identically, and you can use either extension.

If you are writing a bat file for a modern Windows computer (Windows 7 or later), .bat is fine and more widely recognized. If you are writing one that needs to work on very old systems, .bat is also the safer choice because it has been around longer. The .cmd extension is less common and rarely necessary unless you are working in a corporate environment with specific requirements.

When to use a bat file instead of other automation tools

Windows offers several ways to automate tasks. Bat files are the simplest and oldest, but they are not always the best choice. Use a bat file when you need something quick and straightforward — copying files on a schedule, launching multiple programs at once, or running a series of Command Prompt commands without typing them manually.

If your task is more complex, involves waiting for user input, or needs to interact with the Windows graphical interface (rather than just Command Prompt), you might want to use PowerShell, Task Scheduler, or a scripting language like Python instead. Bat files work best for tasks that can be expressed as a simple sequence of commands. If you find yourself writing many conditional statements or loops, the task may have outgrown what a bat file can do efficiently.

Common mistakes when writing bat files

The most frequent mistake is forgetting that commands are case-insensitive but file paths are not. If your bat file tries to copy a file from C:\MyFolder\Document.txt but the actual file is in C:\myfolder\document.txt, the command will fail. Windows will not find the file because the path does not match exactly.

Another common error is not including the full path to files and folders. If you write copy Document.txt Backup.txt without specifying where Document.txt is located, Windows will look for it in whatever folder the bat file is running from — usually not where you intended. Always use the complete path: copy C:\Users\YourName\Documents\Document.txt C:\Backup\Backup.txt.

A third mistake is forgetting that spaces in folder or file names require quotes. If you have a folder called My Documents and you write cd My Documents, Windows will think you are trying to navigate to a folder called "My" and then do something with "Documents" separately. Write it as cd "My Documents" instead.

Security considerations for bat files

Bat files can do anything that Command Prompt can do, which means they can delete files, modify system settings, or install software. Never run a bat file from an unknown source, because a malicious bat file could damage your system or steal data. If someone sends you a bat file, open it in Notepad first and read through the commands to understand what it does before running it.

If you write bat files for your own use, store them in a folder you control and keep track of what each one does. If you share a bat file with others, include a comment at the top explaining what it does. In a bat file, anything after rem is treated as a comment and ignored by Windows, so you can write: rem This bat file backs up the Documents folder to an external drive.

Frequently Asked Questions

Can I run a bat file from a network location or USB drive?

Yes, but Windows may block it depending on your security settings. If you copy a bat file from the internet or an external drive, Windows might mark it as unsafe. Right-click the file, select Properties, and look for an "Unblock" button at the bottom of the window. Click it, then run the bat file normally.

What happens if a command in my bat file fails?

By default, the bat file continues to the next command even if one fails. If you want the bat file to stop when an error occurs, add setlocal enabledelayedexpansion at the top and use if errorlevel 1 exit /b 1 after commands that must succeed. This tells Windows to stop the entire bat file if that command fails.

Can I schedule a bat file to run automatically at a certain time?

Yes, using Windows Task Scheduler. Open Task Scheduler, create a new task, and set it to run your bat file at a specific time or when a certain event occurs. This is useful for backups or cleanup tasks that should run without you having to remember to start them manually.

Is a bat file the same as an executable (.exe) file?

No. A bat file is a text file that Windows reads and executes line by line. An .exe file is a compiled program that Windows runs directly. Bat files are slower and less secure, but they are easier to read and modify. You cannot turn a bat file into an .exe without special tools, and you should not try unless you know what you are doing.

Can I use variables in a bat file?

Yes. You can create a variable with set variablename=value and then use it later by typing %variablename%. For example, set folder=C:\MyFolder followed by cd %folder% will navigate to that folder. This is useful when you need to use the same path or value multiple times in your bat file.