What a batch file is and why you'd create one
A batch file is a text file that contains a list of commands Windows runs one after another, without you typing each one. The file ends in .bat or .cmd. When you double-click it, Windows executes every command inside, in order. People use batch files to automate repetitive tasks — backing up folders, renaming groups of files, launching multiple programs at once, or running the same sequence of steps every day.
You do not need special software. You write a batch file in Notepad, save it with the right extension, and it works. The commands are the same ones you would type into Command Prompt yourself, just written down and saved so you never have to type them again.
Key Takeaways
- A batch file is a plain text file containing Windows commands, saved with a .bat extension, that runs all commands in order when you double-click it.
- You create a batch file by opening Notepad, typing your commands line by line, and saving the file with a .bat extension instead of .txt.
- Common commands include cd (change folder), copy, del (delete), and dir (list files), the same commands you use in Command Prompt.
- Test your batch file in a safe folder first, especially if it deletes or moves files, so you can catch mistakes before they affect important data.
- You can schedule a batch file to run automatically on a set day and time using Windows Task Scheduler.
Opening Notepad and writing your first batch file
Start by opening Notepad. Press the Windows key, type "notepad", and press Enter. A blank window opens. This is where you write your commands.
Type each command on its own line. For example, if you want to create a folder called "Backup" on your Desktop, you would type:
cd Desktop mkdir Backup
The first line changes to your Desktop folder. The second line creates a new folder named Backup. When the batch file runs, it does both things in order. Do not worry about capitalization — Windows commands work in uppercase or lowercase.
If you want to pause and see what happened before the window closes, add this line at the end:
pause
This tells Windows to wait for you to press a key before closing the Command Prompt window, so you can read any messages.
Saving the file with the correct extension
Go to File > Save As. A dialog box opens. At the bottom, you see "Save as type:" with a dropdown menu. Click that dropdown and select "All Files (*.*)" instead of "Text Documents (.txt)". This is the critical step — if you skip it, Notepad saves your file as .txt and Windows will not run it as a batch file.
In the filename field, type a name followed by .bat. For example: backup.bat or organize_files.bat. The .bat extension tells Windows this is a batch file. Click Save.
You now have a batch file on your computer. Navigate to where you saved it, and you will see the file with a different icon than a regular text file — usually a small gear or command prompt symbol.
Common commands you can use in a batch file
You do not need to memorize commands. These are the ones that handle most everyday tasks:
| Command | What it does | Example |
|---|---|---|
| cd | Change to a different folder | cd C:\Users\YourName\Documents |
| mkdir | Create a new folder | mkdir NewFolder |
| copy | Copy a file from one place to another | copy file.txt C:\Backup\ |
| del | Delete a file | del oldfile.txt |
| dir | List all files in the current folder | dir |
| ren | Rename a file | ren oldname.txt newname.txt |
| start | Open a program or file | start notepad.exe |
When you use copy or del, you need to tell Windows which file and where. Use the full path if the file is not in the current folder. For example: copy C:\Users\YourName\Documents\report.txt C:\Backup\ copies report.txt from Documents to a Backup folder.
Testing your batch file safely
Before you run a batch file that deletes or moves files, test it in a folder where mistakes do not matter. Create a test folder on your Desktop, put a few dummy files in it, and run your batch file there first.
Double-click your batch file. A Command Prompt window opens and runs your commands. Watch what happens. If you added pause at the end, the window stays open so you can read any error messages. If something went wrong, you can edit the batch file in Notepad, fix the mistake, save it, and try again.
Once you are confident the batch file works the way you want, you can move it to a permanent location or set it to run on a schedule. Never run a batch file that deletes files without testing it first — there is no undo button once the files are gone.
Running your batch file automatically with Task Scheduler
If you want your batch file to run on its own — for example, every morning at 8 a.m. or every Friday at 5 p.m. — you can use Windows Task Scheduler. Press the Windows key, type "task scheduler", and press Enter.
Click "Create Basic Task" on the right side. Give your task a name, like "Daily Backup". Click Next. Choose when you want it to run — daily, weekly, monthly, or on a specific date. Click Next again and choose the time. Click Next once more.
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\backup.bat. Leave the other fields blank and click Finish. Your batch file will now run at the time you set, without you having to do anything.
Frequently Asked Questions
What is the difference between .bat and .cmd files?
Both work the same way on modern Windows. .bat is the older format and .cmd is newer, but Windows runs them identically. Use .bat unless you have a specific reason to use .cmd. Most people stick with .bat because it is more widely recognized.
Can I edit a batch file after I create it?
Yes. Right-click the batch file and select "Edit". Notepad opens with your commands. Make your changes, save the file, and the next time you run it, the new commands will execute. You can also open it in Notepad the normal way through File > Open.
What if my batch file does not run when I double-click it?
The most common reason is the file was saved as .txt instead of .bat. Right-click the file, select Rename, and change the extension from .txt to .bat. If it still does not work, check that your commands are spelled correctly — typos in command names will cause errors.
Can I run a batch file from a USB drive?
Yes, but use the full path to any files you reference. If your batch file copies a file from the same USB drive, use the drive letter — for example, E:\myfolder\myfile.txt — rather than a relative path. This way it works no matter which computer you plug the USB into.
Is it safe to run batch files from the internet?
Only run batch files from sources you trust. A batch file can delete files, change settings, or install software, so a malicious batch file could damage your computer. If you read a batch file, open it in Notepad first and read the commands to understand what it does before you run it.