What a batch script file is and how to make one

A batch script is a text file containing a list of commands that Windows runs one after another. You create it in Notepad, save it with a .bat extension, and double-click it to run all the commands at once instead of typing them individually. This is useful for repetitive tasks like backing up folders, renaming multiple files, or launching several programs together.

The simplest batch script takes about two minutes to write. You do not need special software — Notepad works fine. The commands themselves are the same ones you would type into Command Prompt, so if you have used that tool before, you already know most of the syntax.

Key Takeaways

  • Open Notepad, type your commands (one per line), and save the file with a .bat extension in the location where you want it to run.
  • Common commands include cd to change folders, copy to duplicate files, del to delete files, and pause to keep the window open so you can see the results.
  • Test your script in a safe folder first — batch files run commands immediately without asking for confirmation, so a mistake can delete or move files you did not intend to touch.
  • You can schedule batch scripts to run automatically using Windows Task Scheduler, which is useful for backups or cleanup tasks that happen on a set day and time.

Creating a batch file in Notepad

Open Notepad (search for "Notepad" in the Windows Start menu). Type your commands, one per line, in the order you want them to run. Here is a simple example that creates a folder and then opens it:

mkdir C:\MyBackup cd C:\MyBackup pause

The mkdir command creates a new folder. The cd command changes to that folder. The pause command keeps the Command Prompt window open so you can see what happened — without it, the window closes immediately after the script finishes.

Save the file by pressing Ctrl+S. In the Save As dialog, type a name with the .bat extension — for example, create_backup.bat. Make sure "Save as type" is set to "All Files" (not "Text Documents"), or Windows will save it as a .txt file instead. Choose where you want to save it — your Desktop or Documents folder is fine for testing.

Running your batch script

Once saved, you can run the script by double-clicking it. A Command Prompt window opens and executes each command in order. If you included pause at the end, the window stays open until you press a key, which gives you time to read any messages or errors.

If something goes wrong, the error message will appear in the Command Prompt window. Common mistakes include typing a folder path that does not exist, misspelling a command, or forgetting to include pause so the window closes before you can see what happened. If you see an error, close the window, open the .bat file in Notepad again, fix the mistake, save it, and run it again.

Useful batch commands for common tasks

Here are commands you will use frequently in batch scripts:

  • cd C:\path\to\folder — Changes to a specific folder. Use cd .. to go up one level.
  • dir — Lists all files and folders in the current location.
  • copy source.txt destination.txt — Copies a file from one location to another.
  • del filename.txt — Deletes a file. Use del *.txt to delete all .txt files in the current folder.
  • mkdir foldername — Creates a new folder.
  • ren oldname.txt newname.txt — Renames a file.
  • start program.exe — Launches a program.
  • echo This is a message — Displays text on the screen.
  • pause — Pauses the script and waits for you to press a key.

Each command does one thing. By combining them in a script, you can automate a sequence of tasks that would otherwise take several minutes to do by hand.

Testing your script safely

Before running a batch script that deletes or moves files, test it on a practice folder with files you do not care about. This protects you from accidentally deleting important data if you made a typo in a file path or command.

A good testing approach is to create a temporary folder on your Desktop, put a few test files in it, and run your script there first. Watch what happens. If the script works as expected, you can then modify it to work on your real files. If something goes wrong, you have only lost the test files.

Another safety step is to add echo statements before commands that delete or move files, so the script tells you what it is about to do. For example:

echo About to delete old files del C:\OldFiles\*.txt echo Deletion complete

This way, you can see the messages and stop the script (by closing the window) if something looks wrong.

Running batch scripts on a schedule with Task Scheduler

Windows Task Scheduler lets you run a batch script automatically at a specific time or on a specific day. This is useful for backups or cleanup tasks you want to happen without remembering to run them manually.

Open Task Scheduler by searching for "Task Scheduler" in the Start menu. Click "Create Basic Task" on the right side. Give it a name, choose when you want it to run (daily, weekly, at startup, etc.), and then specify the action. For the action, select "Start a program" and browse to your .bat file. Click Finish, and the script will run automatically on the schedule you set.

Task Scheduler runs scripts even if you are not logged in, which makes it ideal for overnight backups or maintenance tasks. You can check the Task Scheduler history to see whether the script ran successfully or if there were any errors.

Common mistakes and how to fix them

The most frequent error is using a folder path that does not exist. If your script tries to change to C:\MyFolder but that folder is not there, the script stops with an error. Double-check all folder paths before running the script, or create the folders first.

Another common issue is forgetting to include pause at the end. Without it, the Command Prompt window closes immediately after the script finishes, and you cannot see whether it worked or what errors occurred. Always add pause while you are testing.

File paths with spaces can also cause problems. If a folder is named "My Documents", you need to put quotes around it: cd "C:\Users\YourName\My Documents". Without the quotes, the script treats "My" and "Documents" as separate things and fails.

Finally, remember that batch scripts run commands immediately without asking for confirmation. A script with del *.* will delete every file in the current folder without warning. Always test destructive commands in a safe location first.

Frequently Asked Questions

Can I edit a batch file after I create it?

Yes. Right-click the .bat file and select "Edit" to open it in Notepad. Make your changes, save the file, and run it again. You can edit a batch file as many times as you need.

What happens if a command in my script fails?

By default, the script continues to the next command even if one fails. If you want the script to stop when an error occurs, add @echo off at the very top and if errorlevel 1 exit /b 1 after commands that must succeed. For most simple scripts, letting it continue is fine.

Can I run a batch script from a different folder than where it is saved?

Yes, but you need to use the full path to the file. For example, if your script is on the Desktop, you can type C:\Users\YourName\Desktop\create_backup.bat into Command Prompt and press Enter. You can also create a shortcut to the .bat file and put it anywhere you want.

How do I pass information into a batch script?

You can use variables called parameters. In your script, %1 refers to the first thing you type after the script name, %2 refers to the second, and so on. For example, if your script contains copy %1 %2, you can run it as myscript.bat source.txt destination.txt and it will copy source.txt to destination.txt.

Is it safe to run batch scripts from the internet?

Only run batch scripts from sources you trust. A malicious script can delete files or install unwanted software. Always open the .bat file in Notepad first and read through the commands to understand what it does before running it.