Skip to content

Instantly share code, notes, and snippets.

@brhoades
Last active August 29, 2015 14:26
Show Gist options
  • Save brhoades/654bf806680cfea34b96 to your computer and use it in GitHub Desktop.
Save brhoades/654bf806680cfea34b96 to your computer and use it in GitHub Desktop.
File Backup to Dropbox
@echo off
REM Billy Rhoades
REM 8/3/15
REM Backup script meant for rotations. Please change the variable gamename and run this with the windows scheduling program.
REM This file manually looks for the SavedGames folder in its current directory--- this will fail for anything except lego harry potter.
REM Enables md (mkdir -p equivalent)
setlocal enableextensions
REM Don't ask...
Setlocal EnableDelayedExpansion
REM Change this for other games, obviously--- NO QUOTES
set gamename=Lego Harry Potter Years 1-4
REM Where dropbox will be located
set userdir=C:\Users\YOURUSENAMEHEREFIXME
REM Where we'll drop our backups
set backupfolder=%userdir%\Dropbox\Saved Games\%gamename%
REM Relative to this script, what folder are we taking files from inside of?(
set backuptargetdir=di
REM %~dps0 here stands for the batch file's directory
set backuptarget=%~dps0%backuptargetdir%\*
REM Path to our 7-zip binary
REM Uncomment the one that works for your machine.
REM set sevenza="C:\Program Files (x86)\7-Zip\7z.exe"
REM set sevenza="C:\Program Files\7-Zip\7z.exe"
REM FIX THIS ^
REM
REM Configuration ends here
REM
IF NOT EXIST "%backupfolder%" (echo Backup folder doesn't exist, creating one and doing first backup... && GOTO :UPDATEBACKUP)
echo Determining last backup timestamp...
REM Determine last backup timestamp
for /f "tokens=*" %%f in ('dir /b /O:D /A:-D "%backupfolder%"') do (
set latestbackuplong=%backupfolder%\%%f
set latestbackup=%%f
)
echo Searching for newer files...
REM Go through every file in the backuptarget and compare timestamps versus the last backup
REM More or less, this lists all files in order of last modified date, oldest first, the last onek
COPY "%latestbackuplong%" "%TEMP%" >nul
for /f "tokens=*" %%f in ('dir /b /s /A:-D "%backuptarget%"') do (
IF "%DONE%"=="1" (GOTO :EOF)
FOR %%i IN ("%%f") do (SET DATE1="%%~ti")
FOR %%i IN ("%latestbackuplong%") do (SET DATE2="%%~ti")
REM Create a copy that we'll move to the directory we're testing.
set lastdir=%TEMP%\
IF "%%~nxf" NEQ "%latestbackup%" (
IF "!DATE1!" NEQ "!DATE2!" (
REM Move the backup to the same folder that we're checking.
SET currentfile=%%~dpf%latestbackup%
MOVE /Y "!lastdir!%latestbackup%" "!currentfile!" >nul
FOR /F %%i IN ('DIR /B /O:D "%%f" "!currentfile!"') do (SET NEWEST=%%i)
if "!NEWEST!" NEQ "%latestbackup%" (ECHO !NEWEST! has been modified, updating backup... && GOTO :UPDATEBACKUP)
MOVE /Y "!currentfile!" "%TEMP%" >nul
)
)
)
DEL /Q %TEMP%\%latestbackup%
echo DONE - no new files to update
GOTO :EOF
:UPDATEBACKUP
DEL /Q !currentfile!
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set dt=%%a
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%
REM Timestamp format for our backup file
set timestampformat=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%
REM The backup file name
set backupfilename=%backupfolder%\back-%timestampformat%.7z
IF EXIST "%backupfolder%" GOTO :DONT_CREATE_FOLDER
echo Creating folder...
md "%backupfolder%"
:DONT_CREATE_FOLDER
REM You can drop several more files/folders at the end of this, the script will perform as expected
echo Creating zip file...
%sevenza% a -t7z "%backupfilename%" "%backuptarget%"
REM Just in case we're in a sub call.
set DONE=1
exit /b
:checkdate
exit /b
:EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment