What a batch file is and why you'd write one

A batch file is a plain text file that contains a list of commands your computer runs one after another, without you typing each one. Windows reads the file line by line and executes whatever you've written — moving files, opening programs, renaming folders, running backups, or any other task you'd normally do by hand at the command prompt.

The reason to write one is time. If you do the same sequence of tasks every week — say, copy files from one folder to another, delete temporary files, then send yourself an email — you can write it once in a batch file and then just double-click the file every time you need it done. No typing, no mistakes, no forgetting a step.

Batch files run on any Windows computer without installing extra software. They're one of the oldest automation tools Windows has, and they still work exactly the way they did twenty years ago.

Key Takeaways

  • A batch file is a text file with a .bat extension that contains Windows commands, and you create it in Notepad or any plain text editor.
  • Each line in the file is one command, and Windows runs them in order from top to bottom when you double-click the file.
  • Common commands include cd to change folders, copy to move files, del to delete, and pause to stop and wait for you to press a key.
  • You can test a batch file by right-clicking it and choosing "Run as administrator" if the commands need permission, or just double-clicking it to run normally.
  • If something goes wrong, add pause at the end so the window stays open long enough for you to read the error message.

Creating a batch file in Notepad

Open Notepad — search for "Notepad" in the Windows search box, or press the Windows key and type "notepad". Type your commands, one per line, in the order you want them to run. Save the file with a .bat extension at the end of the name — for example, backup.bat or cleanup.bat. When Notepad asks what type of file to save, choose "All Files" so it doesn't add .txt to the end.

The file itself is just text. You can open it again later in Notepad to edit it, add more commands, or remove lines you don't need anymore. Every time you save and close it, the batch file is ready to run.

Common commands you'll use in a batch file

cd changes which folder you're working in. If you type cd C:\Users\YourName\Documents, all the commands after that line work inside the Documents folder. cd .. moves up one level to the parent folder.

copy copies a file from one place to another. copy C:\Users\YourName\Documents\report.txt D:\Backup\ copies report.txt to your D drive. xcopy is a more powerful version that can copy entire folders with all their contents.

del deletes a file. del C:\Users\YourName\Downloads\temp.txt removes that file permanently. rmdir deletes an entire folder, but only if it's empty. rmdir /s deletes a folder and everything inside it.

pause stops the batch file and displays "Press any key to continue..." so you can read what happened before the window closes. This is useful at the end of a file or after a command you want to check.

echo prints text to the screen. echo Backup complete displays that message. echo. (with a period) prints a blank line to make the output easier to read.

start opens a program or file. start notepad.exe opens Notepad. start C:\Users\YourName\Documents\report.pdf opens that PDF in whatever program is set to open PDFs.

A real example: backing up a folder

Here's a batch file that copies your Documents folder to an external drive, then displays a message when it's done:

@echo off echo Starting backup... xcopy C:\Users\YourName\Documents D:\Backup\Documents /s /i echo Backup complete pause

The first line, @echo off, tells Windows not to display each command as it runs — you'll only see the output. The next line prints "Starting backup..." so you know something is happening. The xcopy command copies everything in Documents to the Backup folder on the D drive. The /s flag means copy subfolders too, and /i means treat the destination as a folder. After it finishes, the batch file prints "Backup complete" and then pauses so you can see the message before the window closes.

Replace YourName with your actual Windows username, and replace D:\Backup with wherever your external drive is. You can find your username by opening File Explorer, clicking "This PC", and looking at the path in the address bar.

Running your batch file

Double-click the .bat file to run it. A command prompt window opens, and Windows executes each line. If the commands need permission to run — for example, if you're deleting system files or writing to a protected folder — right-click the .bat file and choose "Run as administrator" instead.

If something goes wrong, the window might close before you can read the error. Add pause at the very end of your batch file so the window stays open. Then run it again, and you'll see what went wrong.

You can also run a batch file from the command prompt itself. Open Command Prompt, navigate to the folder where your .bat file is, and type the filename. If your file is called backup.bat, just type backup.bat and press Enter.

Fixing problems when a batch file doesn't work

If a command fails, the batch file usually stops and shows an error message. The most common problem is a path that doesn't exist — if you type cd C:\Users\YourName\Documents but your username is actually spelled differently, Windows can't find that folder and the rest of the file won't run.

Check your paths by opening File Explorer and copying the exact path from the address bar. If you're copying files, make sure the source file actually exists and the destination folder exists too. If you're deleting files, double-check the path — there's no undo for del.

Add echo statements before commands you're unsure about. echo About to delete temp files followed by pause lets you stop the batch file before it does something you don't want. Once you're confident it works, you can remove the pause.

Frequently Asked Questions

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

Yes, using Windows Task Scheduler. Open Task Scheduler (search for it in the Windows search box), click "Create Basic Task", give it a name, set the trigger (time of day, weekly, etc.), and point it to your .bat file. After that, Windows runs it automatically on the schedule you set.

What's the difference between .bat and .cmd files?

.bat and .cmd files work almost identically on modern Windows. .bat is older and more widely compatible. .cmd is slightly newer and handles some advanced features better. For most purposes, .bat is fine and more recognizable.

Can a batch file run programs with special characters or spaces in the path?

Yes, but you need to put the path in quotes. Instead of start C:\Program Files\My Program\program.exe, write start "C:\Program Files\My Program\program.exe". The quotes tell Windows to treat the whole thing as one path, even though it has spaces.

What if I want to run a batch file and have it close automatically when it's done?

Don't add pause at the end. The window closes automatically after the last command finishes. If you need to see the output, add pause so you can read it, then close the window yourself.

Can I use a batch file to run commands on another computer?

Not directly from a simple batch file. You can use commands like psexec or wmic to run commands remotely, but those require extra setup and permissions. For most everyday tasks, batch files work on the computer they're running on.