Create a Backup Using a Batch File: Step-by-Step Guide
In this article
Yes, you can create a backup using a batch file. A batch file is a plain text file with a .bat extension that runs a series of Windows commands when double-clicked. By writing a few lines that use the built-in robocopy or xcopy command, you can copy files from one location to another automatically. This is useful for simple, repeatable backups of documents, photos, or project folders to an external drive or network share. No extra software is required beyond what ships with Windows.
What You'll Need
- A Windows PC (batch files are a Windows feature; macOS and Linux use shell scripts instead).
- A destination for the backup: an external USB drive, a second internal drive, or a mapped network folder. Make sure it has enough free space.
- Notepad or any plain-text editor to write the script.
- Basic familiarity with file paths (e.g.,
C:\Users\YourName\Documents).
Menu labels and command behaviour can differ slightly between Windows versions and editions, but the core commands below work on Windows 10 and 11.
Step-by-Step
- Create the batch file. Open Notepad. You will type your commands here. Do not save yet.
- Write the backup command. The most reliable built-in tool is
robocopy. A simple example that mirrors your Documents folder to an external drive E: is:robocopy "C:\Users\YourName\Documents" "E:\Backup\Documents" /MIR /R:2 /W:5 /LOG+:E:\Backup\backup.log
Explanation:
/MIRmirrors the source to the destination (copies new files, deletes files in the destination that no longer exist in the source)./R:2retries failed copies twice./W:5waits 5 seconds between retries./LOG+:appends output to a log file so you can check what happened.If you prefer not to delete anything in the destination, use
/Einstead of/MIRto copy all subdirectories including empty ones, without deleting extra files. - Add a second command for another folder (optional). You can stack multiple
robocopylines, one per source folder. For example:robocopy "C:\Users\YourName\Pictures" "E:\Backup\Pictures" /MIR /R:2 /W:5 /LOG+:E:\Backup\backup.log
- Save the file with a .bat extension. In Notepad, choose File > Save As. Set "Save as type" to "All Files" and name the file something like
backup.bat. If you save it as .txt, it will not run. - Test the batch file. Double-click
backup.bat. A command prompt window will open and show progress. When it finishes, check the destination folder to confirm files were copied. Also open the log file (if you used/LOG+) to see any errors. - Schedule it to run automatically (optional). To run the backup on a schedule, open Task Scheduler from the Start menu. Create a Basic Task, choose how often you want it (e.g., daily), set the action to "Start a program," and browse to your
backup.batfile. This way you do not have to remember to double-click it.
Common Mistakes
- Saving as .txt instead of .bat. The file will open in Notepad instead of running. Use "All Files" in the Save As dialog and type the .bat extension manually.
- Using spaces in paths without quotes. Always wrap paths in double quotes, especially if they contain spaces (e.g., "C:\My Documents").
- Forgetting that /MIR deletes files. Mirror mode removes files from the destination that are not in the source. If you are not sure, use
/Einstead, or test with a small folder first. - Backing up to the same drive. If the drive fails, both the original and the backup are lost. Use a separate physical drive or a network location.
- Not checking the log. Robocopy may skip files that are in use or locked. The log shows which files were skipped and why.
Tips
- Use
/LOG+to append to a log file so you have a history of each backup run. - Add
/TEEto see the log output in the command window as well as the file. - For a simple copy without mirroring,
xcopywith/E /I /His an alternative, butrobocopyis more robust for large folders. - Test your backup by restoring a file from the destination to a temporary folder. A backup you have never restored is not proven.
- Keep the batch file itself backed up or stored in a known location, and document what each line does.
Troubleshooting
If the batch file does not run or the backup fails, work through these checks in order:
- The window flashes and closes immediately. This usually means the command finished quickly or there was an error. Open Command Prompt, navigate to the folder containing the .bat file, and run it by typing its name. That way the window stays open and you can read any error message.
- "Access is denied" errors. The destination may be read-only or you may lack permission. Check that you can create a folder in the destination manually. If the destination is a network share, ensure you are signed in with an account that has write access.
- Robocopy returns exit code 1 or higher. Robocopy uses exit codes to indicate success or failure. Codes 0–7 generally mean success (files copied, extra files detected, etc.). Codes 8 and above indicate errors. Check the log for details.
- The backup is very slow. Copying many small files over USB can be slow. Try using a USB 3.0 port and cable, or back up to an internal drive first.
- Scheduled task does not run. Open Task Scheduler, find your task, and check the "Last Run Result." Common issues include the task not having the correct path to the .bat file or not set to run whether the user is logged on or not.
If none of that works, simplify: create a batch file with a single robocopy line copying one small folder, run it manually, and confirm it works before adding more folders or scheduling.
Frequently Asked Questions
Can I use a batch file to back up to a network drive?
Yes. Use the UNC path (e.g., \\Server\Share\Backup) as the destination in your robocopy command. Make sure your account has write permission to that share.
What is the difference between /MIR and /E in robocopy?
/MIR mirrors the source, meaning it copies new files and deletes files in the destination that are not in the source. /E copies all subdirectories, including empty ones, but does not delete anything in the destination.
Do I need administrator rights to run a backup batch file?
Only if you are backing up system folders or writing to protected locations. For personal folders like Documents or Pictures, standard user rights are enough.
Can I run the batch file automatically every day?
Yes, use Task Scheduler to create a daily trigger that runs your .bat file. This is built into Windows and does not require third-party software.
What happens if a file is open when the backup runs?
Robocopy may skip files that are locked by another program. The log will show which files were skipped. Close the program and run the backup again, or use a backup tool that supports volume shadow copies.
Is robocopy available on all Windows versions?
Robocopy is included in Windows 7 and later, including Windows 10 and 11. It is not available on macOS or Linux.