@echo off
setlocal

rem Removes every "Sync Data" directory of the portable Cent Browser that lives next to this
rem script and of the installed Cent Browser under %LOCALAPPDATA%.
rem Profiles covered in both locations: "Default" and "Profile 1" through "Profile 100".
rem Nothing is printed for directories that do not exist or that were removed;
rem only deletions that failed are reported, followed by a one-line summary.

rem ".\User Data" is resolved against the folder this script is in (not the current directory),
rem so the script also works when started "as administrator" or from another directory.
set "PORTABLE_USER_DATA=%~dp0User Data"
set "INSTALLED_USER_DATA=%LOCALAPPDATA%\CentBrowser\User Data"

set /a REMOVED=0
set /a FAILED=0

call :RemoveSyncData "%PORTABLE_USER_DATA%\Default\Sync Data"
for /L %%i in (1,1,100) do call :RemoveSyncData "%PORTABLE_USER_DATA%\Profile %%i\Sync Data"

call :RemoveSyncData "%INSTALLED_USER_DATA%\Default\Sync Data"
for /L %%i in (1,1,100) do call :RemoveSyncData "%INSTALLED_USER_DATA%\Profile %%i\Sync Data"

if %FAILED% GTR 0 (
  echo.
  echo Some directories could not be removed - make sure Cent Browser is not running.
)
echo Done. Removed: %REMOVED%, failed: %FAILED%
echo.
pause
exit /b

rem Removes the directory given as the first argument and updates the counters.
rem A directory that does not exist is skipped without any output (the trailing backslash in
rem the "if exist" tests makes them match directories only). A directory that still exists
rem after "rd" is reported right after rd's own error output, which names the locked file.
:RemoveSyncData
if not exist "%~1\" exit /b
rd /s /q "%~1"
if exist "%~1\" (
  echo FAILED: "%~1"
  set /a FAILED+=1
) else (
  set /a REMOVED+=1
)
exit /b
