Skip to content

Instantly share code, notes, and snippets.

@MohammadRaziei
Last active August 13, 2026 22:45
Show Gist options
  • Select an option

  • Save MohammadRaziei/0af7bad2dbfbd693de4c5c951436ae4e to your computer and use it in GitHub Desktop.

Select an option

Save MohammadRaziei/0af7bad2dbfbd693de4c5c951436ae4e to your computer and use it in GitHub Desktop.
exclude_from_defender.bat
@echo off
:: exclude_from_defender.bat
::
:: Adds THIS folder (and every subfolder under it, e.g.
:: results\reconstructor\otf_queue) to Windows Defender's real-time
:: scanning exclusion list.
::
:: Why: on-the-fly training writes and deletes thousands of small PNG/JSON
:: files per minute in results\reconstructor\otf_queue. Windows Defender's
:: real-time protection scans each one by default, which can slow training
:: down a lot -- this is the suspected cause of an observed epoch-time
:: climb from ~1000s to ~3500s over the first ~19 epochs of a real run,
:: before dropping back to ~800s. Excluding the folder stops Defender from
:: touching files inside it at all.
::
:: HOW TO USE:
:: 1. Put this .bat file directly inside the folder you want excluded
:: (e.g. the mermaidpix\training folder itself).
:: 2. Right-click it -> "Run as administrator". (Required -- Defender
:: settings can't be changed without admin rights.)
:: 3. Check the printed exclusion list at the end to confirm it's there.
setlocal
:: Add-MpPreference requires admin rights -- "net session" fails without them.
net session >nul 2>&1
if %errorlevel% neq 0 (
echo This script needs to run as Administrator.
echo Right-click this file and choose "Run as administrator", then try again.
pause
exit /b 1
)
:: %~dp0 = the drive+path this .bat file lives in, with a trailing backslash.
set "TARGET_DIR=%~dp0"
if "%TARGET_DIR:~-1%"=="\" set "TARGET_DIR=%TARGET_DIR:~0,-1%"
echo Adding Windows Defender exclusion for:
echo %TARGET_DIR%
echo (this covers all subfolders too, e.g. results\reconstructor\otf_queue)
echo.
powershell -NoProfile -Command "Add-MpPreference -ExclusionPath '%TARGET_DIR%'"
if %errorlevel% neq 0 (
echo.
echo Something went wrong -- see the error above.
pause
exit /b 1
)
echo.
echo Done. Current Defender path exclusions:
powershell -NoProfile -Command "Get-MpPreference | Select-Object -ExpandProperty ExclusionPath"
echo.
echo If the path above is listed, the exclusion is active.
echo To undo this later, run in an admin PowerShell:
echo Remove-MpPreference -ExclusionPath '%TARGET_DIR%'
echo.
pause
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment