What a batch script file is and why you write one
A batch script is a plain text file containing a list of commands that Windows runs one after another, without you typing each one. You save it with a .bat or .cmd extension, double-click it, and Windows executes every line in order. The file itself is just text — you can open it in Notepad and read exactly what it does.
Batch scripts are useful when you do the same sequence of tasks repeatedly: backing up a folder, moving files by date, clearing temporary files, launching multiple programs at once, or running a maintenance routine every week. Instead of typing the same commands by hand each time, you write them once and reuse the file.
Batch is the oldest scripting language Windows still supports, which means it works on every Windows machine without installing anything extra. It has real limits — it cannot handle complex logic the way PowerShell can, and it runs slower — but for straightforward, repetitive tasks, a batch file is faster to write and easier to understand than learning a more powerful tool.
Key Takeaways
- Open Notepad, type your commands one per line, save as filename.bat in a folder you can find later.
- Test your batch file in a new folder with test files before running it on real data, because mistakes can delete or move files you need.
- Use echo off at the top to hide the commands themselves and show only the output, making the window less cluttered.
- Common commands include copy, move, del, mkdir, and dir — all the same commands you would type into Command Prompt by hand.
- Save the file as plain text, not as .txt — Windows will not run a .txt file even if the commands inside are correct.
Opening Notepad and writing your first batch file
Press the Windows key, type Notepad, and open it. You now have a blank text editor. Type your commands one per line, exactly as you would type them into Command Prompt. For example, to create a folder called Backup and copy all files from your Documents folder into it:
mkdir C:\Backup copy C:\Users\YourName\Documents\*.* C:\Backup
Each line is a separate command. Windows will run mkdir first to create the folder, then run copy to move the files. If the folder already exists, mkdir will fail but the script will keep going and run the copy command anyway — that is usually what you want.
Start with @echo off on the first line. This tells Windows to hide the commands themselves as they run, so you see only the results. Without it, your screen fills with text showing every command being executed, which is distracting and makes errors harder to spot.
Add pause at the very end. This keeps the Command Prompt window open after the script finishes, so you can read any error messages before the window closes. Without pause, the window disappears in a second and you miss what went wrong.
Saving the file with the correct extension
Go to File, then Save As. In the filename box, type a name followed by .bat — for example, backup.bat or cleanup.bat. The name should describe what the script does. Do not use spaces in the filename; use underscores or hyphens instead, like backup_files.bat.
At the bottom of the Save As window, find the dropdown that says "Save as type" and change it from "Text Documents (*.txt)" to "All Files (*.*)". This is the critical step. If you leave it on Text Documents, Notepad will save your file as backup.bat.txt, and Windows will not run it.
Choose a folder you can find easily — your Desktop or a Documents subfolder work well. Click Save. You now have a batch file you can double-click to run.
Testing your batch file safely before using it on real data
Before you run a batch file on important files, test it in a separate folder with dummy files. Create a new folder on your Desktop called Test, put a few test files inside, and modify your batch script to point to that Test folder instead of your real data. Run the script and watch what happens.
This matters because commands like del (delete) and move are permanent — if your path is wrong or your logic is backwards, you can lose files. Testing takes five minutes and prevents hours of recovery work. Once you see the script working correctly on test files, you can edit it to use the real paths.
While the script runs, watch the Command Prompt window. If you see an error message like "The system cannot find the path specified", the folder path in your script is wrong. Check the path, fix it in Notepad, save the file, and test again.
Common commands you will use in batch scripts
These are the commands that appear in most batch files. You type them exactly as shown, replacing the parts in brackets with your own folder paths or filenames:
- mkdir [path] — Creates a new folder. Example: mkdir C:\Backup creates a folder called Backup on your C: drive.
- copy [source] [destination] — Copies files from one place to another. Example: copy C:\Users\YourName\Documents\*.pdf C:\Backup copies all PDF files from Documents to Backup.
- move [source] [destination] — Moves files (cuts and pastes them). Example: move C:\Downloads\*.exe C:\Programs moves all .exe files from Downloads to Programs.
- del [path] — Deletes files. Example: del C:\Temp\*.tmp deletes all temporary files in the Temp folder. Use with caution.
- dir [path] — Lists files in a folder. Example: dir C:\Backup shows all files in the Backup folder.
- cd [path] — Changes the current folder. Example: cd C:\Users\YourName\Documents moves to your Documents folder.
- echo [text] — Displays text on screen. Example: echo Backup complete shows that message when the script finishes.
The asterisk (*) is a wildcard that means "all files". The period and extension after it narrows it down — *.pdf means all PDF files, *.tmp means all temporary files. If you type just *.*, you mean all files of any type.
Using variables and loops for more complex scripts
Once you are comfortable with basic commands, you can use variables to store information and loops to repeat commands. A variable is a container that holds a value — for example, you can store today's date and use it in a filename so each backup gets a unique name.
To create a variable, use set. For example, set backupname=backup_%date% creates a variable called backupname that contains the word "backup_" followed by today's date. Then you use %backupname% anywhere you need that value:
set backupname=backup_%date% mkdir C:\%backupname% copy C:\Users\YourName\Documents\*.* C:\%backupname%
A loop repeats a command for each file in a folder. The for command does this. For example, to delete all files older than a certain date or to process each file one at a time, you use for. Loops are more complex to write, so start with straightforward scripts first and add loops only when you need them.
Running your batch file automatically on a schedule
Once your batch file works, you can set Windows to run it automatically at a specific time or when certain events happen. The easiest way is through Task Scheduler, which is built into Windows.
Press the Windows key, type Task Scheduler, and open it. Click Create Basic Task on the right side. Give it a name like "Daily Backup", choose when you want it to run (daily, weekly, at startup, etc.), and then tell it which program to run. In the program field, type the full path to your batch file, for example C:\Users\YourName\Desktop\backup.bat. Click Finish, and Windows will run that batch file on the schedule you set.
Task Scheduler runs the script even if you are not logged in, which is useful for backups or cleanup routines you want to happen overnight. If something goes wrong, Task Scheduler keeps a log you can check to see what happened.
Frequently Asked Questions
What is the difference between .bat and .cmd?
Both work the same way on modern Windows. .bat is older and .cmd is newer, but Windows treats them identically. Use .bat unless you have a specific reason not to. Some people use .cmd for scripts that will only run on newer versions of Windows, but for most purposes it does not matter.
Can I edit a batch file after I create it?
Yes. Right-click the .bat file, choose Edit, and Notepad opens. Make your changes, save the file, and the next time you run it, the new commands will execute. You can also drag the .bat file onto Notepad to open it for editing.
What if my batch file does not do anything when I double-click it?
The script may have run and finished so fast the window closed before you could see what happened. Add pause at the end of your script so the window stays open. Also check that you saved the file as .bat, not .txt — if Windows does not recognize the extension, it will not run the file as a script.
Can I run a batch file from a USB drive?
Yes, but use relative paths instead of absolute paths. Instead of C:\Users\YourName\Documents, use just Documents or .\Documents so the script looks for the folder relative to where the batch file is stored. This way the script works no matter which drive letter the USB gets assigned.
Is batch scripting safe, or can it damage my computer?
A batch file can only run commands you type into it. It cannot install malware or bypass security on its own. The danger is user error — if you write a delete command with the wrong path, it will delete the wrong files. Always test on dummy files first, and never copy a batch file from the internet and run it without reading what it does.