@echo off
:: substitute.bat OLD NEW FILE
setlocal
set "OLD=%~1"
set "NEW=%~2"
set "FILE=%~3"

:: Validate arguments
if "%OLD%"=="" (
    echo ERROR: OLD argument missing
    exit /b 1
)
if "%NEW%"=="" (
    echo ERROR: NEW argument missing
    exit /b 1
)
if not exist "%FILE%" (
    echo ERROR: File not found: %FILE%
    exit /b 1
)

:: Replace text in place with PowerShell (UTF-8 without BOM, works on PS 5.1)
powershell -NoProfile -Command ^
    "$content = Get-Content -LiteralPath '%FILE%' -Raw;" ^
    "$content = $content -replace [regex]::Escape('%OLD%'), '%NEW%';" ^
    "[System.IO.File]::WriteAllText('%FILE%', $content, (New-Object System.Text.UTF8Encoding($false)))"

endlocal
exit /b


