How to backup photos automatically

eye-catch Tips

This article shows how to automatically copy files that don’t exist in the destination directory. I often copy files from my smartphone to my laptop because I want to remove pictures from my smartphone. The disk size of my external card is only 4 GB.

I periodically copy those files to my external HDD for backup but don’t want to do it manually. Therefore, I prepared a batch file to do it automatically.

Sponsored links

Robocopy basic usage

The basic command is following.

  • robocopy <source directory> <destination directory>

Following is an example that copies all files, directories and sub directories in the current directory to “E:\destination”.

robocopy %CURRENT% "E:\destination"
Sponsored links

Complete commands

The command that I always use is following. Create a file like “copy.bat” and copy this and paste it to “copy.bat”. Once you create the bat file you need to just do double click the file when you want to copy files.

SET CURRENT=%~dp0
SET LOGPATH=%CURRENT%%date:~0,2%%date:~3,2%%date:~6,4%copy.log
robocopy %CURRENT% "E:\destination" ^
/XF *.CR2 *.log ^
/XD sdk-tools-windows-4333796 ^
node_modules ^
eclipse ^
.* ^
dist ^
build ^^
/E /XO /XC /XN /NP /LOG+:%LOGPATH%
pause

Hat mark “^” makes the code readable. We have to write everything on the same line if we don’t use it.

How to exclude files

Let’s check the options used in the example above one by one. I want to exclude files that have already existed in the destination directory.

Following code is not necessary if you don’t need log. Remove “/LOG+:%LOGPATH%” as well in this case. It gets current directory path and create a path to write log file. “date” returns the date but its format depends on your system. My format is “MM/DD/YYYY”, e.g “07/14/2021”. The result of this command is “07142021copy.log” for example. Change the order if you want to change the format.

SET CURRENT=%~dp0
SET LOGPATH=%CURRENT%%date:~0,2%%date:~3,2%%date:~6,4%copy.log

This is the main command. It copies files and directories but “XF” option excludes files whose extension is CR2 and log. Add this option if you have some files that you don’t want to copy.

robocopy %CURRENT% "E:\destination" ^
/XF *.CR2 *.log ^

XD option excludes directories specified here. Following directories are programming related. Some directories start with dot “.” that are hidden directories.

/XD sdk-tools-windows-4333796 ^
node_modules ^
eclipse ^
.* ^
dist ^
build ^^

The last line specifies how to make it work.

/E /XO /XC /XN /NP /LOG+:%LOGPATH%

The meaning of each option is following. It means that the command doesn’t copy files that exist in the destination. If you want to copy updated files you need to remove “/XC” and “/XN” options.

OptionWhat for
XFExclude files
XDExclude directories
ECopy sub directory including empty directory
XOExclude older files than the one that exist in destination
XCExclude changed files
XNExclude newer files than the one that exist in destination
NPNot show progress
LOG+Output log (Append)

Comments

Copied title and URL