A batch file is a text file that runs Windows commands automatically
A batch file is a plain text document that contains a list of commands your computer executes one after another. When you run the file, Windows reads each line and performs that action — whether that's copying files, deleting folders, launching programs, or renaming documents. Batch files end with the .bat extension and let you automate repetitive tasks without clicking through menus each time.
You create a batch file using Notepad (or any text editor), type the commands you want to run, save it with a .bat extension, and then double-click it to execute. This guide walks you through building a simple batch file, understanding the commands that go inside it, and troubleshooting when something does not work as expected.
Key Takeaways
- Open Notepad, type your commands (one per line), and save the file with a .bat extension instead of .txt to create a batch file.
- Common batch commands include cd to change folders, copy to duplicate files, del to delete files, and echo to display text on screen.
- Test your batch file in a folder where mistakes will not damage important files, and add pause at the end so you can see any error messages before the window closes.
- If a batch file does not run, check that the file extension is actually .bat (not .bat.txt), and right-click to run it as administrator if it needs permission to access certain folders.
Creating a batch file in Notepad
Open Notepad by pressing the Windows key, typing "Notepad", and pressing Enter. You will see a blank document. Type the commands you want to run, one command per line. For example, a simple batch file might look like this:
echo This batch file is running cd C:\Users\YourName\Documents dir
Each line tells Windows to do something: the first displays text on screen, the second moves to your Documents folder, and the third lists all files in that folder. Once you have typed your commands, go to File > Save As. In the filename field, type a name followed by .bat — for example, my_backup.bat. Make sure the "Save as type" dropdown is set to "All Files" (not "Text Documents"), then click Save. If you save it as a .txt file by mistake, Windows will treat it as text instead of a command file.
Essential batch commands you will use
You do not need to memorize every command, but a few appear in almost every batch file. The echo command displays text: echo Hello World prints "Hello World" on screen. The cd command changes your current folder: cd C:\Users\YourName\Desktop moves you to the Desktop. The dir command lists all files in the current folder.
The copy command duplicates a file: copy C:\source\file.txt C:\destination\file.txt copies a file from one location to another. The del command deletes a file: del C:\Users\YourName\Desktop\oldfile.txt removes that file permanently. The pause command stops the batch file and displays "Press any key to continue", which is useful for reading error messages before the window closes. Always add pause as the last line when you are testing a new batch file.
The @echo off command at the very top of your batch file hides the commands themselves and shows only the results — this makes the output cleaner and easier to read. For example, without @echo off, you would see both the command and its result; with it, you see only the result.
Running your batch file safely
Before you run a batch file that deletes or moves files, test it in a folder where mistakes will not matter. Create a test folder on your Desktop, put a few dummy files inside, and run your batch file there first. This way, if something goes wrong, you lose only test files, not important documents.
To run a batch file, navigate to the folder where you saved it, right-click the .bat file, and select "Open" or "Run". If the batch file needs to access protected folders or install software, right-click and choose "Run as administrator" instead. A command window will open, execute your commands, and close when finished — unless you added pause at the end, in which case it will wait for you to press a key.
If the window closes too fast for you to read any error messages, add pause before the final line. This gives you time to see what went wrong. Once you are confident the batch file works correctly, you can remove the pause command if you prefer.
Fixing common batch file problems
The most common mistake is saving the file as filename.bat.txt instead of filename.bat. Windows hides file extensions by default, so you might not notice. To check, right-click the file, select Properties, and look at the full filename in the General tab. If it ends in .bat.txt, rename it to remove the .txt part. You can also enable "Show file extensions" in File Explorer (View menu > Show file extensions) so you always see the real extension.
If a batch file runs but does nothing, the commands might be wrong or the file paths might not exist. Add echo statements before each command to confirm the batch file is reaching that point: echo About to copy files will print that message if the batch file gets that far. If you see the message but the copy does not happen, the file path is probably incorrect. Double-check that the folder and filename are spelled exactly right, including capital letters.
If you get an "Access Denied" error, the batch file does not have permission to access that folder or file. Right-click the batch file and select "Run as administrator". If that does not work, the file itself might be locked by another program — close any program that might be using that file and try again.
Batch file examples for common tasks
Here is a batch file that backs up a folder by copying it to another location:
@echo off echo Starting backup copy C:\Users\YourName\Documents\* C:\Users\YourName\Backup\ echo Backup complete pause
Here is a batch file that deletes temporary files from a specific folder:
@echo off echo Deleting temporary files cd C:\Users\YourName\AppData\Local\Temp del *.tmp echo Temporary files deleted pause
The asterisk (*) is a wildcard that means "all files" — so del *.tmp deletes every file ending in .tmp. You can also use wildcards with copy: copy *.jpg C:\Backup\ copies all JPG files to the Backup folder. Always test batch files with wildcards on dummy files first, because a mistake can delete more than you intended.
Scheduling a batch file to run automatically
Once you have a batch file that works, you can set Windows to run it on a schedule — for example, every morning at 8 AM or every Friday at 5 PM. Open Task Scheduler by pressing the Windows key, typing "Task Scheduler", and pressing Enter. Click "Create Basic Task" on the right side. Give your task a name and description, then click Next.
Choose when you want the task to run (daily, weekly, monthly, or on startup), then click Next again. Set the specific time and date pattern. On the next screen, select "Start a program" and click Next. In the "Program/script" field, type the full path to your batch file — for example, C:\Users\YourName\Desktop\my_backup.bat. Click Finish, and Windows will run that batch file automatically on the schedule you set.
Frequently Asked Questions
Can I run a batch file from a USB drive or external hard drive?
Yes. Save the batch file to the USB drive, plug it into any Windows computer, and double-click it to run. If the batch file references specific file paths (like C:\Users\YourName\Documents), those paths must exist on that computer, or the batch file will fail. For portable batch files that work on any computer, use relative paths instead — for example, use .\Documents\ to mean "the Documents folder in the same location as this batch file".
What is the difference between a .bat file and a .cmd file?
.bat and .cmd files work almost identically on modern Windows. .bat is the older format, and .cmd is slightly newer. For most purposes, use .bat. The only practical difference is that .cmd handles certain advanced features slightly differently, but you will not encounter those in basic batch files.
Can I edit a batch file after I create it?
Yes. Right-click the .bat file, select "Open with", choose Notepad, make your changes, and save. Do not change the filename or extension. If you want to test changes without overwriting the original, save it with a different name first — for example, save it as my_backup_v2.bat — then test the new version.
What happens if a batch file encounters an error in the middle?
By default, the batch file stops running and displays an error message. If you want the batch file to skip errors and continue with the next command, add @echo off at the top and use the || operator: for example, copy file.txt destination || echo Copy failed will attempt the copy, and if it fails, it will display "Copy failed" and move on.
Can I make a batch file that asks for user input?
Yes, using the set /p command. For example, set /p username=Enter your name: displays "Enter your name:" and stores whatever the user types in a variable called username. You can then use that input in later commands — for example, echo Hello %username% will display "Hello" followed by whatever name the user entered.