Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Backup with Batch Files in Windows 10

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Yes, you can back up files and folders in Windows 10 using a simple batch file. This method uses the built-in robocopy command to copy files from a source to a destination, and it can be automated with Task Scheduler. You don’t need any third-party software, and the whole process takes about 10 minutes to set up.

What You'll Need

  • A Windows 10 PC with an administrator account (to create the batch file and schedule it).
  • A destination drive: an external USB drive, a network share, or a second internal drive. Make sure it has enough free space for your data.
  • Notepad (or any text editor) to write the batch file.

Step-by-Step

  1. Decide what to back up. For example, you might want to back up your Documents, Pictures, or a specific project folder. Note the full path of the source folder (e.g., C:\Users\YourName\Documents).
  2. Decide where to back it up. This is the destination folder. For an external drive, it might be E:\Backup\Documents. For a network share, it might be \\Server\Share\Backup. Make sure the destination folder exists or let robocopy create it (it does by default).
  3. Open Notepad. Click Start, type “Notepad”, and press Enter.
  4. Write the backup command. In Notepad, type the following line, replacing the source and destination with your own paths:
    robocopy "C:\Users\YourName\Documents" "E:\Backup\Documents" /E /COPY:DAT /R:3 /W:5 /LOG:"C:\Users\YourName\backup_log.txt"
    Here’s what each switch does:
    • /E – copies all subdirectories, including empty ones.
    • /COPY:DAT – copies Data, Attributes, and Timestamps (default).
    • /R:3 – retries 3 times if a file is locked or busy.
    • /W:5 – waits 5 seconds between retries.
    • /LOG:file – writes a log file so you can see what was copied.
    If you want to mirror the source (delete files in the destination that no longer exist in the source), add /MIR. Be careful: this will delete files in the destination that aren’t in the source. For a simple backup, it’s safer to omit /MIR.
  5. Add a second command (optional) to copy multiple folders. You can add more robocopy lines, one per folder, each with its own destination. For example:
    robocopy "C:\Users\YourName\Pictures" "E:\Backup\Pictures" /E /R:3 /W:5 /LOG+:"C:\Users\YourName\backup_log.txt"
    Note the /LOG+ (plus sign) to append to the same log file instead of overwriting it.
  6. Save the file as a batch file. In Notepad, click File > Save As. In the “Save as type” dropdown, choose “All Files (*.*)”. Name the file backup.bat (or any name ending in .bat). Save it somewhere you can find, like your Desktop.
  7. Test the batch file. Double-click the .bat file. A command prompt window will open and show the robocopy progress. When it finishes, check the destination folder to confirm files were copied. Also open the log file to see any errors.
  8. Schedule the backup (optional but recommended). To run this automatically, use Task Scheduler:
    1. Press Windows + R, type taskschd.msc, and press Enter.
    2. Click “Create Basic Task…” on the right.
    3. Give it a name like “Daily Backup”. Click Next.
    4. Choose how often you want it to run (e.g., Daily, Weekly). Click Next.
    5. Set the start time and recurrence. Click Next.
    6. Choose “Start a program”. Click Next.
    7. In “Program/script”, browse to your backup.bat file. Click Next and then Finish.
    Make sure your external drive is connected at the scheduled time, or the backup will fail.

Common Mistakes

  • Using a single backslash in paths. In batch files, use double backslashes (\\) for network paths, but for local paths, single backslashes are fine. However, the command line itself uses single backslashes, so write C:\Users\... not C:\\Users\\....
  • Forgetting to close the destination drive. If the external drive is not plugged in, robocopy will fail. Always check that the drive letter matches when the drive is connected.
  • Using /MIR without understanding it. /MIR will delete files in the destination that aren’t in the source. If you accidentally point it to the wrong destination, you can lose data. Use it only if you want an exact mirror.
  • Not testing the batch file. Always run it manually at least once to ensure it works before scheduling.

Tips

  • Use a log file. Always include /LOG or /LOG+ so you can verify that the backup ran successfully.
  • Add a pause at the end. If you run the batch file manually, add pause at the end so the window stays open and you can read any error messages.
  • Use relative paths if possible. If you always plug the external drive into the same letter (e.g., E:), that’s fine. If the letter changes, consider using the volume label with %~d0 or a PowerShell script, but that’s more advanced.
  • Back up open files. Robocopy will retry if a file is in use, but it may skip files that are locked by applications. Close programs that use the files you’re backing up.

Troubleshooting

If the backup doesn’t work as expected, try these in order:

  1. Check the log file. The log will show which files failed and why. Common errors include “Access Denied” (permissions) or “The system cannot find the path specified” (wrong path).
  2. Run the batch file as administrator. Right-click the .bat file and choose “Run as administrator”. This can fix permission issues.
  3. Verify the destination drive is connected. Open File Explorer and confirm the drive letter exists. If it’s a network drive, make sure it’s mapped and accessible.
  4. Check for spaces in paths. If your paths contain spaces (e.g., C:\Users\John Doe\My Files), you must enclose them in double quotes, as shown in the examples.
  5. If the scheduled task doesn’t run, check that the task is enabled and that the “Run whether user is logged on or not” option is set. Also, make sure the batch file path is correct in the task.

Frequently Asked Questions

Can I back up multiple folders with one batch file?

Yes, just add multiple robocopy lines, one per folder, each with its own source and destination. Use /LOG+ to append to the same log.

Does robocopy copy open files?

It tries, but if a file is locked by a program, robocopy will retry (based on /R and /W) and then skip it, logging the error. Close programs that have the files open for a complete backup.

Can I use this to back up to a cloud drive like OneDrive?

Yes, if the cloud drive is synced to a local folder (like C:\Users\YourName\OneDrive), you can set that as the destination. However, cloud sync may not be immediate, so it’s not a real-time backup.

How do I restore files from the backup?

Simply copy the files back from the destination to your original location. You can use File Explorer or another robocopy command with source and destination swapped.

Is robocopy available in all Windows 10 editions?

Yes, robocopy is a standard command-line tool included in all editions of Windows 10.

Can I schedule the backup to run when the computer is locked?

Yes, with Task Scheduler, you can set the task to run whether the user is logged on or not. You may need to store the password for the account.