The Broadband Guide
SG
search advanced
 Username:
 Password:
Register
 forgot password?

How to Backup using Batch Files under Windows 10

2018-10-01 (updated: 2020-10-06) by

It is sometimes useful, or even necessary to simply copy common directories and files to another/external drive, or a network attached storage (NAS). This article provides a comparatively simple solution using a batch file to backup folders. Even though it can copy only changed/newer files to save time, it is still not intended as a replacement for a full-featured backup software with versioning, incremental backups, etc.

This article works well for backing up common files/folders under Windows 10 using robocopy. It is an updated, expanded and improved version of an older article we wrote for Windows XP using batch files and xcopy. The batch file below is designed to work with the newer Windows directory structure, and robocopy seems to be more robust than xcopy in Windows 10.

Batch files have a comparatively easy syntax. You can simply copy the text below, paste into Notepad and modify it to your needs. Note that the batch file should have either .bat or .cmd extension, rather than .txt so that it would be executed when you double-click on it.

Here is a working sample backup script, you should modify at least the destination drive to your needs. This batch file includes many commonly needed variables, such as dates and timestamps. Explanation for each section is included below the script. For a simpler variant of the script, you can skip down to the "TL;DR;" section.


@echo off
:: variables
set drive=G:\backup
set backupcmd=robocopy /e /xo /R:5 /XJD
set excludefiles=/xf *.vsdelta *.tmp Thumbs.db
set excludedirs=/xd "%LocalAppData%\Google\Chrome\User Data"

:: -+-+-+- DATE Variables  (optional) -+-+-+-
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
set "datestamp=%YYYY%-%MM%-%DD%"

:: Use the Windows Environment Variable %UserName% to get the current user, used as part of the destination folder name
set user=%UserName%
set folder=%UserName%_%YYYY%

:: -+-+-+- start backup below -+-+-+-
echo ### Backing up user %user% ###
%backupcmd% "C:\Users\%user%\Desktop" "%drive%\%folder%\Desktop" %excludefiles%
%backupcmd% "C:\Users\%user%\Documents" "%drive%\%folder%\Documents" %excludefiles%
%backupcmd% "C:\Users\%user%\Pictures" "%drive%\%folder%\Pictures" %excludefiles%

:: -+-+-+- Backup Outlook files (optional) -+-+-+-
echo ### To backup Outlook files, the program must be stopped! ###
:: stop Outlook.. may need /f switch to force-close it
taskkill /im outlook.exe /f
TIMEOUT 30
%backupcmd% /J "%LocalAppData%\Microsoft\Outlook" "%drive%\%folder%\Outlook"
:: optionally start Outlook again with the below command
:: start outlook.exe

echo ### Backup Complete! ###
@pause


Explanation of the script

In the above file, all lines that begin with ":: " are comments. Below we will examine most of the sections from the above batch file, so that they are easier to understand and modify. You can simply omit sections that you do not need to use, for example the "DATE Variables" and the "Backup Outlook" sections may not be needed by everyone. Here are the file sections explained, from the beginning ot the batch file:

set drive=G:\backup

This defines the backup destination drive/folder. The "set drive=..." defines the destination hard drive, it can also be a network location, just copy the name from Windows Explorer, for example "drive=\\router-nas", or "drive=Z:\backups". Variables in general are defined using the "set variablename=..." command. Those same variables can leter be referenced by the same vairable name, surrounded by percentage symbols, as in "%variablename%", or %drive%, etc.

set backupcmd=robocopy /e /xo /R:5 /XJD

This sets the backup command, we are using the robocopy command that is included with morern Windows installations. There are some useful command-line switches that can be added to the robocopy command as follows:

:: ROBOCOPY switches
:: /E -- copy subdirectories, including Empty ones.
:: /PURGE -- delete dest files/dirs that no longer exist in source.
:: /MIR -- MIRror a directory tree (equivalent to /E plus /PURGE).
:: /R:5 -- five retries on failure.
:: /XJD -- don't include system/protected dirs like "My Documents/My Music"


set excludefiles=/xf *.vsdelta *.tmp Thumbs.db
set excludedirs=/xd "%LocalAppData%\Google\Chrome\User Data"

These two variables define excluded files/folders that you may not want to copy. As an example, the above batch file excludes *.tmp temporary files, Thumbs.db files, and the Google Chrome temporary/user files to improve speed and reduce unnecessary clutter at the destination.

:: -+-+-+- DATE Variables (optional) -+-+-+-
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
set "datestamp=%YYYY%-%MM%-%DD%"

This section is optional, it can be commented out if you do not plan to use dates in your backup folder names. This section defines a number of variables that you can use, if you want. For example, you may want to use the %datestamp% variable as a directory name to create different backups based on date. You can only use portions of the date, as in "%YY%-%MM%" to get a two-digit year and two digit month. In this batch file, only the %YYYY% (four digit year) is used in the destination folder name below.

:: Using the Windows Environment Variable %UserName% to get the current user
set user=%UserName%
set folder=%UserName%_%YYYY%

This section defines the %user% variable to the currently logged in Windows user, and the %folder% variable that we will use as a destination for our backups. Note the %folder% variable will contain both our Windows username and the current year from previously defined variables.

:: -+-+-+- start backup below -+-+-+-
echo ### Backing up user %user% ###
%backupcmd% "C:\Users\%user%\Desktop" "%drive%\%folder%\Desktop"
%backupcmd% "C:\Users\%user%\Documents" "%drive%\%folder%\Documents" %excludefiles%
%backupcmd% "C:\Users\%user%\Pictures" "%drive%\%folder%\Pictures"

This section simply uses our backup command defined above, and copies the currently logged in Windows user's Desktop, Documents, and Pictures folders to the destination drive/folder. Only modified files will be copied on subsequent runs of the batch file.


:: -+-+-+- Backup Outlook files (optional) -+-+-+-
echo ### To backup Outlook files, the program must be stopped! ###
:: stop Outlook.. may need /f switch to force-close it
taskkill /im outlook.exe /f
TIMEOUT 30
%backupcmd% /J "%LocalAppData%\Microsoft\Outlook" "%drive%\%folder%\Outlook"
:: optionally start Outlook again with the below command
:: start outlook.exe

This last section is completely optional, if you use Outlook it backs up your email files. It stops Outlook in order to copy the files. If you do not use MS Outlook, you can comment it out, or delete this section completely.

echo ### Backup Complete! ###
@pause

This simply pauses at the end of the batch file, so that you can review what has been copied.



TL;DR;

Too Long? Didn't Read? Want something simpler? Here is a very simple working batch file that also backs up the Desktop, Documents and Pictures of the current Windows user (without most variables, outlook files, or excluded filetypes/folders):


@echo off
:: -+-+-+- start backup -+-+-+-
echo ### Backing up user %UserName% ###

robocopy /e /xo /R:5 /XJD "C:\Users\%UserName%\Desktop" "G:\backup\Desktop"
robocopy /e /xo /R:5 /XJD "C:\Users\%UserName%\Documents" "G:\backup\Documents"
robocopy /e /xo /R:5 /XJD "C:\Users\%UserName%\Pictures" "G:\backup\Pictures"

echo ### Backup Complete! ###
@pause


The above simply backs up three sample folders, "C:\Users\{username}\Destkop", "Documents", and "Pictures" to a backup destination, in this example their corresponding folders under G:\backup\... You only have to modify the destination drive letter to your external hard drive to make this script work. The global Windows %UserName% is the only variable in this file, corresponding to the name of the currently logged in Windows user.  Alternatively, you can also simply copy/paste the source directory paths from Windows Explorer.


Notes:

Directory paths need to be enclosed in parenthesis if the directory structure contains any spaces.
"::" or "REM" at the beginning of new lines indicates comments. "echo ..." simply prints text to standard output. "@echo off" prevents output of all commands. "@pause" waits for any key from the user.  
Any batch file can be interrupted with CTRL+C or CTRL+Break if needed. They can also be paused with the Pause/Break key. A Task can be created to run the batch file on a schedule using the Windows Task Scheduler.

Useful global Windows Environment Variables that can be included in batch files:
%AppData% expands to C:\Users\{username}\AppData\Roaming
%LocalAppData% = C:\Users\{username}\AppData\Local
%UserProfile% = C:\Users\{username}
%UserName% = {username}


For an older version of this article that works well with Windows XP using xcopy instead of robocopy, see this.


  Post your review/comments
    rate:
   avg:
News Glossary of Terms FAQs Polls Cool Links SpeedGuide Teams SG Premium Services SG Gear Store
Registry Tweaks Broadband Tools Downloads/Patches Broadband Hardware SG Ports Database Security Default Passwords User Stories
Broadband Routers Wireless Firewalls / VPNs Software Hardware User Reviews
Broadband Security Editorials General User Articles Quick Reference
Broadband Forums General Discussions
Advertising Awards Link to us Server Statistics Helping SG About