What a .bat file is and why you might need one

A .bat file is a plain text file that contains a list of commands your Windows computer runs one after another, without you typing each one. The ".bat" stands for "batch" — it batches commands together. When you double-click a .bat file, Windows opens Command Prompt, reads each line, and executes it in order.

You might use a .bat file to automate something repetitive: backing up a folder every morning, renaming a batch of files at once, launching multiple programs with a single click, or running a series of system commands. Instead of typing the same ten commands every time, you type them once into a .bat file and reuse it.

The commands inside a .bat file are the same ones you would type into Command Prompt yourself. If you have never opened Command Prompt, you can still create a .bat file — you just need to know what commands you want to run, or find them online for your specific task.

Key Takeaways

  • A .bat file is a text file containing Windows commands that run automatically when you open it.
  • You create a .bat file by opening Notepad, typing your commands, and saving the file with a .bat extension instead of .txt.
  • The commands inside must be valid Windows Command Prompt commands, and each command goes on its own line.
  • You run a .bat file by double-clicking it, and a Command Prompt window opens and closes when finished (unless you add a pause command).
  • If a command fails or you need to see what happened, add pause at the end so the window stays open.

Creating a .bat file in Notepad

Open Notepad — press the Windows key, type "notepad", and press Enter. A blank text window opens. Type the commands you want to run, one per line. For example, if you want to create a folder called "Backup" on your Desktop and then open it, you would type:

mkdir %USERPROFILE%\Desktop\Backup start %USERPROFILE%\Desktop\Backup

Each line is a separate command. Do not worry about the exact syntax yet — the point is that each command occupies one line, and you can add as many lines as you need.

Once you have typed all your commands, go to File > Save As. In the "Save as type" dropdown, change it from "Text Documents (*.txt)" to "All Files (*.*)". Then type a name for your file followed by .bat — for example, "create_backup.bat". Choose where you want to save it (your Desktop is easy to find later), and click Save.

Windows now has a .bat file. The icon will look different from a .txt file — it usually shows a gear or command symbol. You can now run it by double-clicking.

Understanding common commands for .bat files

You do not need to memorize commands, but knowing a few common ones helps you write useful .bat files. Here are commands that work in almost every .bat file:

  • mkdir — creates a new folder. Example: mkdir C:\Users\YourName\NewFolder
  • cd — changes to a different folder. Example: cd C:\Users\YourName\Documents
  • copy — copies a file. Example: copy C:\file.txt C:\Backup\file.txt
  • del — deletes a file. Example: del C:\file.txt
  • start — opens a file or program. Example: start notepad.exe
  • pause — stops the .bat file and waits for you to press a key. Use this at the end so you can see what happened.
  • echo — prints text to the screen. Example: echo Backup complete

For file paths, use the full path starting from the drive letter (like C:\). If a folder name has spaces, put the whole path in quotes: "C:\Program Files\MyApp". Windows also recognizes shortcuts like %USERPROFILE% (your user folder) and %DESKTOP% (your Desktop).

Running your .bat file and troubleshooting

To run a .bat file, double-click it. A Command Prompt window opens, runs each command in order, and closes when finished. If a command fails or takes time, the window may close too fast for you to see what happened.

To keep the window open, add pause as the last line of your .bat file. This tells Command Prompt to wait for you to press a key before closing. Edit your .bat file by right-clicking it, choosing "Edit", making your change in Notepad, and saving.

If a command does not work, the most common reasons are: the file path is wrong, the program name is misspelled, or the command itself is not valid for your version of Windows. Open Command Prompt yourself (press Windows key, type "cmd", press Enter) and type the command to see the error message. The error message usually tells you what went wrong.

Using variables and file paths in .bat files

Windows provides built-in variables — shortcuts that stand for common folders — so you do not have to type your full username. Wrap them in percent signs: %USERPROFILE% means your user folder, %DESKTOP% means your Desktop, and %TEMP% means your temporary files folder.

You can also create your own variables at the top of your .bat file using set. For example:

set BackupFolder=C:\Users\YourName\Backup mkdir %BackupFolder%\Today copy C:\Documents\file.txt %BackupFolder%\Today\file.txt

This makes your .bat file easier to read and easier to change later — you only have to update the path once at the top instead of hunting through the whole file.

Scheduling a .bat file to run automatically

Once you have a working .bat file, you can set Windows to run it on a schedule — every morning at 9 AM, every Friday at 5 PM, or whenever you choose. Open Task Scheduler by pressing the Windows key, typing "task scheduler", and pressing Enter.

Click "Create Basic Task" on the right side. Give it a name (like "Daily Backup"), choose when you want it to run (daily, weekly, on startup, etc.), and then click "New" under "Action". Browse to your .bat file and select it. Click Finish, and Windows will run your .bat file on the schedule you set.

Task Scheduler is useful for backups or cleanup tasks that you want to happen without thinking about it. If something goes wrong, Task Scheduler keeps a log you can check — open Task Scheduler, find your task in the list, and look at the "Last Run Result" column.

Frequently Asked Questions

Can I edit a .bat file after I create it?

Yes. Right-click the .bat file, choose "Edit", and Notepad opens. Make your changes and save. The next time you run the .bat file, it will use the new commands. You can also open it with any text editor — Notepad, Notepad++, or VS Code all work.

What if I want to run a .bat file from a different folder?

Use the full path to the .bat file. For example, if your .bat file is on your Desktop, you can type C:\Users\YourName\Desktop\myfile.bat into Command Prompt. You can also create a shortcut to the .bat file and put the shortcut anywhere you want.

Can a .bat file run programs like Photoshop or Chrome?

Yes, use the start command followed by the program name or full path. For example, start chrome.exe or start "C:\Program Files\Adobe\Photoshop\Photoshop.exe". If the program is not in a standard location, you may need the full path.

What happens if a command in my .bat file fails?

By default, the .bat file keeps running the next command even if one fails. If you want it to stop on the first error, add setlocal enabledelayedexpansion at the top and use if errorlevel 1 exit /b 1 after commands that must succeed. For most simple .bat files, you do not need this — just add pause at the end to see any error messages.

Can I run a .bat file without the Command Prompt window showing?

Yes, but it is not recommended for learning. You would need to create a shortcut to the .bat file and change its properties to run minimized, or use a more advanced tool like a VBScript wrapper. For now, let the Command Prompt window show — it helps you see if something went wrong.