Situatie
A scheduled job that copies a folder to another drive or a network share, writes a dated log, deletes logs older than 30 days, and writes an Event Log entry if the copy fails.
Prerequisites
- Windows 10/11 (Robocopy is built in)
- Admin rights
- A destination with enough free space (a second drive, USB disk or NAS share).
Solutie
Step 1: Create the script
Create C:\Scripts\backup.bat:
@echo off
set "SRC=C:\Documents"
set "DST=D:\Backup\Documents"
set "LOGDIR=D:\Backup\logs"
if not exist "%SRC%" exit /b 16
if not exist "%LOGDIR%" mkdir "%LOGDIR%"
for /f %%i in ('powershell -NoProfile -Command "Get-Date -Format yyyy-MM-dd"') do set "STAMP=%%i"
robocopy "%SRC%" "%DST%" /E /XO /R:2 /W:5 /NP /LOG:"%LOGDIR%\backup_%STAMP%.log"
set "RC=%ERRORLEVEL%"
if %RC% GEQ 8 eventcreate /T ERROR /ID 100 /L APPLICATION /SO BackupScript /D "Robocopy failed with exit code %RC%"
forfiles /p "%LOGDIR%" /m backup_*.log /d -30 /c "cmd /c del @path" 2>nul
exit /b %RC%
The date comes from PowerShell because %date% formatting depends on the regional settings.
Step 2: Understand the Robocopy flags
/Ecopies all subfolders, including empty ones./XOskips files that are older than the copy already in the destination./R:2 /W:5retries twice, waiting 5 seconds, instead of the default of one million retries./NPremoves the progress percentage to keep logs readable./LOGwrites output to a file.
Step 3: Dry run
Add /L (list only) to the robocopy line temporarily and run the script. Check the log to confirm that the right files are selected. Remove /L afterwards.
Step 4: Schedule it with PowerShell (Admin)
$action = New-ScheduledTaskAction -Execute "C:\Scripts\backup.bat"
$trigger = New-ScheduledTaskTrigger -Daily -At 9pm
$settings = New-ScheduledTaskSettingsSet -StartWhenAvailable -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries
$principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -RunLevel Highest
Register-ScheduledTask -TaskName "Backup Documents" -Action $action -Trigger $trigger -Settings $settings -Principal $principal
-StartWhenAvailable runs the job as soon as possible if the PC was off at 9 PM. Running as SYSTEM means it works while nobody is logged in, but SYSTEM can’t see mapped drive letters. Use UNC paths (\\NAS\Backup\Documents) for network destinations.
Step 5: Test
- Run:
Start-ScheduledTask -TaskName "Backup Documents" - Check
D:\Backup\logs\backup_<date>.log. - Check Task Scheduler → Task Scheduler Library → Last Run Result.
0x0or0x1is success.
Robocopy exit codes
0: nothing to copy1: files copied2: extra files in the destination4: mismatches- Combinations of these (3, 5, 6, 7) are still success
8or higher: at least one failure, and the script logs it to the Event Log
Verification
- Open a few files in the destination and confirm they open.
- Simulate a failure by making the destination unavailable, then check Event Viewer → Windows Logs → Application for source
BackupScript.
Caveats
/XOnever propagates deletions, so files you delete from the source remain in the backup./MIRmirrors deletions too, which is risky if you delete something by accident.- This is a copy, not versioned backup. For history, add a dated destination folder or use File History or a dedicated backup tool.
- A backup on the same physical disk isn’t a backup. Follow the 3-2-1 rule: three copies, two media, one offsite.
Leave A Comment?