What a batch file is and why you would make 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 batch files end in .bat or .cmd and let you automate repetitive tasks — backing up folders, deleting temporary files, renaming groups of documents, or running the same program every morning at the same time.
You write batch files in Notepad or any text editor. They run on Windows only (Mac and Linux use different script formats). A batch file is useful when you do the same sequence of steps more than once, or when you want something to happen on a schedule without you being there to start it.
Key Takeaways
- Batch files are text files you write in Notepad, saved with a .bat extension, containing Windows commands that run in order.
- Each line in a batch file is a single command — you can copy commands directly from what you would type into Command Prompt.
- You run a batch file by double-clicking it, dragging files onto it, or scheduling it to run at a specific time using Task Scheduler.
- Start with straightforward tasks like moving files or deleting a folder, test them in Command Prompt first, then save them as a batch file.
- Batch files run with the same permissions you have — if you need administrator access, right-click the file and choose "Run as administrator".
Creating a batch file in Notepad
Open Notepad (search for it in the Windows Start menu). Type your commands, one per line, in the order you want them to run. Each command is exactly what you would type into Command Prompt if you were doing it by hand.
For example, a batch file that deletes all files in your Downloads folder and then creates a new folder called "Archive" would look like this:
del C:\Users\YourUsername\Downloads\* mkdir C:\Users\YourUsername\Downloads\Archive
Replace YourUsername with your actual Windows username. You can find your username by opening Command Prompt and typing echo %username%.
When you are done typing, go to File > Save As. In the "Save as type" dropdown, select "All Files" instead of "Text Documents". Name the file something descriptive with a .bat extension — for example, cleanup.bat or backup_documents.bat. Save it somewhere you will remember, like your Desktop or Documents folder.
Common commands to use in batch files
Here are commands that work in almost every batch file. Test each one in Command Prompt before putting it in a batch file, so you know it does what you expect.
| Command | What it does | Example |
|---|---|---|
| cd | Changes to a different folder | cd C:\Users\YourUsername\Documents |
| dir | Lists all files in the current folder | dir |
| copy | Copies a file from one place to another | copy C:\file.txt C:\Backup\file.txt |
| move | Moves a file from one place to another | move C:\file.txt C:\Archive\file.txt |
| del | Deletes a file | del C:\file.txt |
| mkdir | Creates a new folder | mkdir C:\NewFolder |
| pause | Stops and waits for you to press a key | pause |
| echo | Prints text to the screen | echo Backup complete |
Always use the full path to a file or folder (starting with C:\ or the drive letter). If a folder name has spaces, put the whole path in quotes: "C:\Program Files\My App".
Testing your batch file before running it
Before you run a batch file, especially one that deletes or moves files, test each command in Command Prompt first. Open Command Prompt (search for it in the Start menu), type one command, and press Enter. Watch what happens. If it does what you expected, write it down and move to the next command.
If you are deleting files, create a test folder with dummy files and run the delete command on that folder first. This way, if something goes wrong, you lose test files instead of real ones.
Once all your commands work in Command Prompt, copy them into your batch file in the same order. Save the file, then right-click it and choose "Run as administrator" (if you need permission to do what the commands do). A Command Prompt window will open, run your commands, and close when it is done.
Running a batch file manually or on a schedule
To run a batch file by hand, double-click it. A Command Prompt window opens, runs the commands, and closes. If you want to see what happened, add the word pause as the last line of your batch file — this keeps the window open until you press a key.
To run a batch file on a schedule (for example, every morning at 9 AM or every Sunday at midnight), use Windows Task Scheduler. Search for "Task Scheduler" in the Start menu and open it. Click "Create Basic Task" on the right side. Give it a name, choose when you want it to run, and then point it to your batch file. Task Scheduler will run it automatically at that time, even if you are not at your computer.
If your batch file needs to access files on a network drive or needs administrator permissions, right-click the task in Task Scheduler, choose "Properties", and check the box that says "Run with highest privileges".
Keeping your batch files safe and organized
Store all your batch files in one folder so you can find them later. A folder called C:\BatchFiles or C:\Scripts works well. Do not store them in Windows system folders like System32 — this can cause problems if Windows updates itself.
Name your batch files clearly so you remember what they do: delete_temp_files.bat is better than cleanup.bat. If you write a batch file that deletes anything, add a comment at the top explaining what it deletes and why. In a batch file, a comment starts with REM (short for "remark"):
REM This file deletes temporary files from the Downloads folder REM Created on January 15, 2024 del C:\Users\YourUsername\Downloads\*.tmp
Never run a batch file from an email or a website you do not trust. Batch files can do anything your user account can do, including delete files or install software. If someone sends you a batch file, ask them what it does before you run it.
Frequently Asked Questions
What is the difference between .bat and .cmd files?
Both work the same way on modern Windows. .bat is older and more widely recognized; .cmd is slightly newer. Use .bat unless you have a specific reason not to. The commands inside are identical.
Can I run a batch file from a different folder?
Yes. Use the full path in your batch file commands, starting with the drive letter (C:\, D:\, etc.). If you use just a filename with no path, the batch file looks for it in the current folder. To change the current folder, use the cd command at the start of your batch file.
What if my batch file needs to run as administrator?
Right-click the batch file and choose "Run as administrator". If you scheduled it in Task Scheduler, open Task Scheduler, right-click the task, choose "Properties", and check "Run with highest privileges". You may see a prompt asking for permission — click "Yes" to continue.
How do I stop a batch file that is running?
Click the Command Prompt window and press Ctrl+C, then press Y and Enter. This stops the batch file when ready. Any commands that already ran will stay done — for example, if the batch file deleted files before you stopped it, those files are still gone.
Can I make a batch file run when I log in?
Yes, using Task Scheduler. Create a task that runs "At log on" instead of at a specific time. Or put a shortcut to your batch file in the Startup folder: press Windows+R, type shell:startup, and drag your batch file shortcut into the folder that opens.