Skip to content

Instantly share code, notes, and snippets.

@andygock
Created April 14, 2017 08:09
Show Gist options
  • Save andygock/2a46d786783aeb41c215d5acf32ec61f to your computer and use it in GitHub Desktop.
Save andygock/2a46d786783aeb41c215d5acf32ec61f to your computer and use it in GitHub Desktop.
Script to sync one directory to another using robocopy (suitable as a form of backup)
@echo off
setlocal
:: set target and source directory, do not use a trailing slash
:: recommended: sync to a subdir on target drive
set source_drive=z:
set target_drive=p:\drive_z
:: exclude these dirs from sync
set excluded_dirs=exclude1 exclude2 "exclude 3"
:: confirm with user to continue
echo "Sync %source_drive% to %target_drive% - Are you sure? Ctrl+C to cancel"
pause
:: check target drive and dir exists (this is why subdir is recommended on target drive)
if not exist "%target_drive%\" goto no_target_dir
:: Sync the drive, minus exclusions
robocopy "%source_drive%\\" "%target_drive%" /mir /ndl /r:99 /w:1 /xd %excluded_dirs%
echo Finished!
pause
exit /b
:no_target_dir
:: Can't find the target drive, maybe not plugged in?
@echo "%target_drive%" does not exist!
pause
exit /b
@hexadezi
Copy link

Exclude the following folders to skip two system folders

%source_drive%"$RECYCLE.BIN" %source_drive%"\System Volume Information"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment