Skip to content

Instantly share code, notes, and snippets.

@jokimaki
Last active December 10, 2021 17:08
Show Gist options
  • Save jokimaki/c457fdcb7b9db2ecd3a25309cdd2439f to your computer and use it in GitHub Desktop.
Save jokimaki/c457fdcb7b9db2ecd3a25309cdd2439f to your computer and use it in GitHub Desktop.
Create unique .7z file by using current date and time. Copy-paste to notepad to get correct file encoding.
@echo off
setlocal enabledelayedexpansion
REM Assume this file is Windows-1252 encoded
REM (default when editing with Notepad)
chcp 1252>nul
REM Set 7zip to PATH
set _7ZIP_INSTALLATION=C:\Program Files\7-Zip
set PATH=%_7ZIP_INSTALLATION%;%PATH%
REM Example 1: store backups under temp
REM set BACKUP_DESTINATION=%TEMP%\backups
REM Example 2: store backups in Google Drive:
set BACKUP_DESTINATION=%USERPROFILE%\Google Drive\kirja-backups
mkdir "%BACKUP_DESTINATION%" 2>nul
REM Configure backup source directories
set BACKUP_BASE_DIR=%USERPROFILE%\Desktop
set BACKUP_SUB_DIRS=book
set BACKUP_SOURCES=
FOR %%D IN (%BACKUP_SUB_DIRS%) DO (set BACKUP_SOURCES=!BACKUP_SOURCES!"%BACKUP_BASE_DIR%\%%D" )
REM Create log file in %BACKUP_DESTINATION%
set LOG="%BACKUP_DESTINATION%\log.txt"
REM Create unique .7z file by using current date and time
set CURRENTTIME=%TIME:~0,2%%TIME:~3,2%%TIME:~6,2%
REM Note: C-B-A order and "delims" depends on locale. Here the day/month/year is separeted by ".".
FOR /F "tokens=2-4 delims=. " %%A IN ('DATE /T') DO (set TIMESTAMP=%%C-%%B-%%A_%CURRENTTIME%)
set ARCHIVE=%BACKUP_DESTINATION%\%TIMESTAMP%.7z
echo Start backup at %DATE% %TIME% >> %LOG%
echo Directories to back up: >> %LOG%
echo %BACKUP_SOURCES% >> %LOG%
REM -ssw = Compresses files open for writing by another applications
7z a -ssw "%ARCHIVE%" %BACKUP_SOURCES% >> %LOG%
echo End backup at %DATE% %TIME% >> %LOG%
echo. >> %LOG%
chcp 850>nul
echo.
echo Created backup archive: & echo.
dir "%ARCHIVE%"
endlocal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment