A batch file is a text file that runs a list of commands in Windows automatically

A batch file is a plain text document that contains Windows commands, saved with a .bat or .cmd extension. When you double-click it, Windows reads the file line by line and executes each command in order. This is useful for automating repetitive tasks — like moving files to a folder, renaming multiple documents, or running programs in sequence — without typing the same commands over and over.

You do not need special software. You can create a batch file in Notepad, the text editor built into Windows. The file itself is just instructions; it does not contain any data or settings that could harm your computer, though you should only run batch files from sources you trust, since they can delete or move files if the commands tell them to.

Key Takeaways

  • Open Notepad, type your commands (one per line), then save the file with a .bat extension in the folder where you want it to run.
  • Use simple commands like cd to change folders, copy to duplicate files, and del to remove them.
  • Test your batch file in a folder with unimportant files first, because mistakes in commands can delete or move files permanently.
  • You can schedule a batch file to run automatically at a specific time using Windows Task Scheduler.

Creating a batch file in Notepad

Open Notepad by typing "notepad" into the Windows search box and clicking the result. Type each command on its own line. For example, a simple batch file might look like this:

@echo off cd C:\Users\YourName\Documents copy *.txt C:\Users\YourName\Documents\Backup

The first line, @echo off, tells Windows not to display each command as it runs — this keeps the window cleaner. The second line changes to your Documents folder. The third copies all .txt files to a Backup subfolder.

Once you have typed your commands, click File, then Save As. In the filename box, type a name followed by .bat — for example, backup_files.bat. Make sure the "Save as type" dropdown 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 and click Save.

Common batch file commands

You do not need to memorize many commands. Here are the ones you will use most often:

CommandWhat it doesExample
cdChanges to a different foldercd C:\Users\YourName\Downloads
dirLists all files in the current folderdir
copyCopies a file to another locationcopy document.txt C:\Backup
moveMoves a file to another locationmove document.txt C:\Archive
delDeletes a filedel old_file.txt
renRenames a fileren old_name.txt new_name.txt
pausePauses and waits for you to press a keypause

You can use wildcards to work with multiple files at once. The asterisk (*) means "any characters." For example, copy *.pdf C:\Backup copies all PDF files in the current folder to the Backup folder. The question mark (?) matches a single character, so del file?.txt would delete file1.txt, file2.txt, and so on.

Running your batch file

To run a batch file, navigate to the folder where you saved it, right-click the file, and choose "Open" or double-click it. A command window will appear and execute each line. If you included the pause command at the end, the window will stay open until you press a key, giving you time to read any messages. Without it, the window closes automatically when the batch file finishes.

If something goes wrong, the command window may close too quickly to read the error. Add pause before the last line so you can see what happened. For example, if a file path is wrong, Windows will display an error message before closing.

Testing safely before running on important files

Before running a batch file on real documents, test it in a folder with files you do not mind losing. Create a test folder on your Desktop, put a few dummy files in it, and run your batch file there. This way, if the commands are wrong and delete or move files, you have not damaged anything important.

Common mistakes include typing the wrong folder path (Windows will not find the files), forgetting to include the file extension in the command (so it looks for "document" instead of "document.txt"), or using del when you meant move. Once you see the batch file work correctly on test files, you can run it on your real documents.

Scheduling a batch file to run automatically

Windows Task Scheduler lets you run a batch file at a specific time or when certain conditions are met — for example, every morning at 9 a.m., or whenever you log in. Open Task Scheduler by typing "task scheduler" into the Windows search box. Click "Create Basic Task" on the right side. Give it a name, choose when you want it to run, then browse to your batch file and select it.

This is useful for backups or cleanup tasks you want to happen regularly without remembering to run them manually. Once you set it up, Windows handles the rest.

Frequently Asked Questions

Can I edit a batch file after I create it?

Yes. Right-click the .bat file, choose "Open with," and select Notepad. Make your changes and save. The next time you run the file, it will use the new commands.

What is the difference between .bat and .cmd?

Both work the same way on modern Windows. .bat is older and more widely recognized; .cmd is slightly newer. For most tasks, either extension works fine. Use .bat unless you have a specific reason to use .cmd.

Can a batch file harm my computer?

A batch file itself cannot install malware or bypass security. However, if the commands inside tell Windows to delete files or change settings, it will do exactly that. Only run batch files from sources you trust, and always test on unimportant files first.

How do I stop a batch file while it is running?

Press Ctrl+C in the command window. Windows will stop executing commands and ask if you want to terminate the batch job. Type Y and press Enter to confirm.

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

Yes, but you need to use the full path to the batch file. For example, if your batch file is at C:\Users\YourName\Documents\backup.bat, you can type that full path into the command prompt and press Enter to run it from anywhere.