@echo off
setlocal enableextensions

set IdxPath=index\
set DrvPath=
if exist "%~dp0index" goto :DelIdx
call :GetCmdDir "%~dp0."
if "%IdxPath%"=="" if exist "%~dp0%DrvPath%drivers" goto :DelIdx
echo This file must be in root of SamDrivers or in "index" subfolder!
pause
goto :eof

:DelIdx
:: delete old DIA indexes
for /r "%~dp0%IdxPath%DIA" %%f in (*.hwid *.txt) do if not exist "%~dp0%DrvPath%drivers\%%~nf.7z" del /f /q "%%f"

:: delete old DPS indexes
for /r "%~dp0%IdxPath%DPS" %%f in (*.hash *.txt) do call :CheckDPSIdx "%%f"

:: delete old SDI indexes
for /r "%~dp0%IdxPath%SDI" %%f in (*.bin) do if not exist "%~dp0%DrvPath%drivers\%%~nf.7z" del /f /q "%%f"

goto :eof

:CheckDPSIdx
set DPName=%~n1
if not "%DPName:~-8%"=="_Drivers" goto :eof
if not exist "%~dp0%DrvPath%drivers\%DPName:~0,-8%.7z" del /f /q %1
goto :eof

:GetCmdDir
if /i "%~n1"=="index" set IdxPath=
set DrvPath=..\
goto :eof