Have had a need to write this script for a while and finally took the time to do it today.
As a rule of thumb, I have an extreme distrust of the Windows file copy/paste functionality when it comes to deep directory structures.
It only gets worse when you add in network shares, so cue the paranoia.
Below is a script that has two examples:
- Copy a single file from a remote directory to a local directory using robocopy
- Copy an entire directory structure from remote to a local directory using robocopy
Both of these examples will only copy the delta between the two, so if the remote is newer or has a different file size the local representation will be purged of the differences and sync only the differences. In addition, it will log the entire transaction to a separate timestamped log file each time it is ran so we can analyze if there were transmission/permission errors.
This is helpful because network shares are prone to failure, especially over a VPN.
@echo off
REM #####################################################
REM #### Generate Timestamp String for Log Filename: ####
REM #####################################################
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set VARIABLE_DATETIME=%%a
set VARIABLE_YEAR=%VARIABLE_DATETIME:~0,4%
set VARIABLE_MONTH=%VARIABLE_DATETIME:~4,2%
set VARIABLE_DAY=%VARIABLE_DATETIME:~6,2%
set VARIABLE_HOUR=%VARIABLE_DATETIME:~8,2%
set VARIABLE_MINUTE=%VARIABLE_DATETIME:~10,2%
set VARIABLE_SECOND=%VARIABLE_DATETIME:~12,2%
set VARIABLE_TIMESTAMP=%VARIABLE_YEAR%-%VARIABLE_MONTH%-%VARIABLE_DAY%_%VARIABLE_HOUR%-%VARIABLE_MINUTE%-%VARIABLE_SECOND%
REM ##############################################################
REM #### Parameters used in this script for robocopy command: ####
REM ##############################################################
REM
REM e - Copies subdirectories. This option automatically includes empty directories.
REM
REM z - Copies files in restartable mode.
REM
REM purge - Deletes destination files and directories that no longer exist in the source. Using this option with the /e option and a destination directory, allows the destination directory security settings to not be overwritten.
REM
REM log+ - Writes the status output to the log file (appends the output to the existing log file).
REM
REM tee - Writes to console window and log file at the same time instead of just the log file when using the log option
REM SCRIPT_DIRECTORY is defined as the directory that this script currently exists in
SET "SCRIPT_DIRECTORY=%~dp0"
SET "SCRIPT_DIRECTORY=%SCRIPT_DIRECTORY:~0,-1%"
echo "Working Directory = %SCRIPT_DIRECTORY%"
SET "VARIABLE_RETRY_WAIT=1"
echo "Wait Time for Retry = %VARIABLE_RETRY_WAIT%"
SET "VARIABLE_RETRY_COUNT=1000"
echo "Retry Count Before Giving Up = %VARIABLE_RETRY_COUNT%"
SET "VARIABLE_LOG=%SCRIPT_DIRECTORY%\sync_%VARIABLE_TIMESTAMP%.log"
echo "Log File = %VARIABLE_LOG%"
REM ############################################################
REM #### FILE - Getting specific_file_that_i_want.extension ####
REM ############################################################
SET "VARIABLE_LOCAL_DIR=%SCRIPT_DIRECTORY%\network-share-directory"
echo "Local Directory = %VARIABLE_LOCAL_DIR%"
SET "VARIABLE_REMOTE_DIR=\\remote-file-share\path\to\directory\to\sync\network-share-directory"
echo "Remote Directory = %VARIABLE_REMOTE_DIR%"
REM Example of copying a single file with robocopy
robocopy /e /Z /purge /W:%VARIABLE_RETRY_WAIT% ^
/r:%VARIABLE_RETRY_COUNT% ^
/tee ^
"/log+:%VARIABLE_LOG%" ^
"%VARIABLE_REMOTE_DIR%" ^
"%VARIABLE_LOCAL_DIR%" ^
"specific_file_that_i_want.extension"
REM #####################################################
REM #### DIRECTORY - Getting network-share-directory ####
REM #####################################################
SET "VARIABLE_LOCAL_DIR=%SCRIPT_DIRECTORY%\network-share-directory"
echo "Local Directory = %VARIABLE_LOCAL_DIR%"
SET "VARIABLE_REMOTE_DIR=\\remote-file-share\path\to\directory\to\sync\network-share-directory"
echo "Remote Directory = %VARIABLE_REMOTE_DIR%"
REM Example of copying a single file with robocopy
robocopy /e /Z /purge /W:%VARIABLE_RETRY_WAIT% ^
/r:%VARIABLE_RETRY_COUNT% ^
/tee ^
"/log+:%VARIABLE_LOG%" ^
"%VARIABLE_REMOTE_DIR%" ^
"%VARIABLE_LOCAL_DIR%"
pause