@echo off
SETLOCAL EnableDelayedExpansion

set sourcedir=%LOCALAPPDATA%\Microsoft\Windows Sidebar\
set targetdir=%LOCALAPPDATA%\Microsoft\Windows Sidebar\
rem change sourcedir if you have the backup files copied from somewhere else

set sort=%DIRCMD%
if /I not "%sort%"=="%sort:D=%" set sorttxt= (oldest first)
if /I not "%sort%"=="%sort:-D=%" set sorttxt= (newest first)
if "%sort%"=="" (set sort=/O) && set sorttxt=

if "%1"=="" goto help
if "%1"=="/?" goto help
if /I "%2"=="/auto" (set num=%1) && goto auto
if /I "%1"=="/auto" (set num=%2) && goto auto

set txt= Settings_%1.ini
goto start

:help
color 0A
echo.
echo Syntax: restoreSettings [1-9 ^| name] [/auto]
echo.
echo Use "/auto" to restore backup 9 or the stated number/name, no questions asked.
echo.
echo If no parameter is entered you will be prompted for a backup number/name.
echo.
echo                            _______________________
echo.

:start
echo.
echo Saved Settings files%sorttxt% in:
echo.
echo   %sourcedir%
echo.
dir %sort%N "%sourcedir%Settings.ini" "%sourcedir%Settings_*.ini" | find /I "settings"
echo.
echo.
echo Restore backup%txt% to the Desktop Gadgets Settings file:
echo.
echo   %targetdir%Settings.ini

if "%1"=="" goto choice
if "%1"=="/?" goto choice

set num=%1
goto exec

:choice
echo.
echo.
CHOICE /C 123456789NC /N /M "Choose a number between 1 and 9 for the backup file (N = Name, C = Cancel):"
set num=%ERRORLEVEL%
if %num%==10 goto name
if %num%==11 goto end
goto exec

:name
echo.
echo Enter the name that is appended to Settings_ for the backup file name:
echo.
set /p num=

:exec
echo.
echo.
echo Copy:
echo   %sourcedir%Settings_%num%.ini
echo To:
echo   %targetdir%Settings.ini
echo.
CHOICE /C YN /N /M "Do you wish to proceed [Y/N]?"
if not %ERRORLEVEL%==1 goto end

if exist "%sourcedir%Settings_%num%.ini" goto exec2

echo.
echo The file Settings_%num%.ini does not exist.

echo.
CHOICE /C YN /N /M "Do you want to choose another number/name [Y/N]?"
if %ERRORLEVEL%==1 goto choice
if not %ERRORLEVEL%==1 goto end

:exec2
echo.
echo.
echo Closing Desktop Gadgets
taskkill /IM sidebar.exe /F > nul
echo.
copy "%sourcedir%Settings_%num%.ini" "%targetdir%Settings.ini"
echo.
echo.
echo Saved Settings files%sorttxt% in:
echo.
echo   %sourcedir%
echo.
dir %sort%N "%sourcedir%Settings.ini" "%sourcedir%Settings_*.ini" | find /I "settings"
echo.
echo Press any key to restart Desktop Gadgets and continue. . .
pause > nul
start "" "%ProgramFiles%\Windows Sidebar\sidebar.exe"
goto end

:auto
if "%num%"=="" set num=9

if not exist "%sourcedir%Settings_%num%.ini" goto err

taskkill /IM sidebar.exe /F > nul
copy "%sourcedir%Settings_%num%.ini" "%targetdir%Settings.ini"
start "" "%ProgramFiles%\Windows Sidebar\sidebar.exe"
goto end

:err
echo.
echo The stated file Settings_%num%.ini does not exist.
echo.
echo Press any key to continue...
pause > nul

:end
