Skip to content

Instantly share code, notes, and snippets.

@zydezu
Last active January 21, 2026 01:05
Show Gist options
  • Select an option

  • Save zydezu/830787a1367b37a5fd3a38118422675c to your computer and use it in GitHub Desktop.

Select an option

Save zydezu/830787a1367b37a5fd3a38118422675c to your computer and use it in GitHub Desktop.
Backup the windows user directory to where the directory this .bat file is run.
@echo off
setlocal EnableExtensions EnableDelayedExpansion
REM ===== Configuration =====
set BACKUP_DEST=.
for /f %%I in ('powershell -NoProfile -Command "Get-Date -Format yyyy-MM-dd_HH_mm_ss"') do set TIMESTAMP=%%I
set BACKUP_FILE=%BACKUP_DEST%\%TIMESTAMP% zydesktop.7z
set LOG_FILE=%BACKUP_DEST%\%TIMESTAMP%_backup.log
set FAIL_LOG=%BACKUP_DEST%\%TIMESTAMP%_backup_failed.log
echo ===== Starting =====
echo Starting backup at %date% %time%
echo Log: %LOG_FILE%
echo Failed files: %FAIL_LOG%
echo ====================
REM ===== Temp directory =====
set TEMP_BACKUP=%BACKUP_DEST%\%TIMESTAMP%_backup
if exist "%TEMP_BACKUP%" rmdir /s /q "%TEMP_BACKUP%"
mkdir "%TEMP_BACKUP%"
REM ===== User profile (exclude AppData + dot folders) =====
echo Backing up user directory...
set "EXCLUDE_DIRS=%USERPROFILE%\AppData"
for /d %%D in ("%USERPROFILE%\.*") do (
set "EXCLUDE_DIRS=!EXCLUDE_DIRS! "%%~fD""
)
REM Exclude known Windows shell folders
set "EXCLUDE_DIRS=!EXCLUDE_DIRS! %USERPROFILE%\Contacts %USERPROFILE%\Cookies %USERPROFILE%\Favorites %USERPROFILE%\Favourites %USERPROFILE%\Links %USERPROFILE%\NetHood %USERPROFILE%\PrintHood %USERPROFILE%\Recent %USERPROFILE%\Searches %USERPROFILE%\SendTo %USERPROFILE%\Templates"
robocopy "%USERPROFILE%" "%TEMP_BACKUP%\User" ^
/E /COPY:DAT /R:1 /W:1 ^
/XD !EXCLUDE_DIRS! ^
/XD "%USERPROFILE%\Application Data" ^
/XD "%USERPROFILE%\Local Settings" ^
/XD "%USERPROFILE%\My Documents" ^
/XD "%USERPROFILE%\Documents\My Music" ^
/XD "%USERPROFILE%\Documents\My Pictures" ^
/XD "%USERPROFILE%\Documents\My Videos" ^
/XD "%USERPROFILE%\Saved Games" ^
/XD "%USERPROFILE%\Start Menu" ^
/XF "NTUSER.*" ^
/TEE /LOG+:"%LOG_FILE%" /NFL /NDL
REM ===== AppData\Roaming (filtered) =====
echo Backing up AppData/Roaming...
robocopy "%USERPROFILE%\AppData\Roaming" "%TEMP_BACKUP%\User\AppData\Roaming" ^
/E /COPY:DAT /R:1 /W:1 ^
/XD "Code" "discord" "Microsoft" "nvm" ^
/TEE /LOG+:"%LOG_FILE%" /NFL /NDL
REM ===== C:\Apps =====
echo Backing up C:\Apps...
robocopy "C:\Apps" "%TEMP_BACKUP%\Apps" ^
/E /COPY:DAT /R:1 /W:1 ^
/TEE /LOG+:"%LOG_FILE%" /NFL /NDL
REM ===== Extract failures into separate log =====
findstr /I /C:"ERROR" /C:"FAILED" "%LOG_FILE%" > "%FAIL_LOG%"
REM ===== Create ZIP =====
echo Creating 7z archive...
7z a -t7z "%BACKUP_FILE%" "%TEMP_BACKUP%\*"
REM ===== Cleanup =====
rmdir /s /q "%TEMP_BACKUP%"
echo ===== Complete =====
echo Backup completed at %date% %time%
echo ZIP: %BACKUP_FILE%
echo ====================
if exist "%FAIL_LOG%" (
echo WARNING: Some files failed to copy.
echo See: %FAIL_LOG%
) else (
echo All files copied successfully.
)
pause
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment