This does not fit your answer exactly but I think you if work through the logic you will find many ways to work through your problem.
This is based on me wanting to check to see if a backup was made within the last 14 days based on the filename format "%username%.Backup.YEAR.MONTH.DAY.time.HOURS.MINUTES"
I believe in its current state it will fail to produce the correct result if you go above 28 days due to not looping through the if statement inside setlocal until days is no longer less than 1.
set year=%date:~10%
set month=%date:~4,2%
set day=%date:~7,2%
set /a day-=14
set /a leapyear=!(%year%%%4)
set numdays=31 28 31 30 31 30 31 31 30 31 30 31
if %leapyear%==1 set numdays=31 29 31 30 31 30 31 31 30 31 30 31
setlocal enabledelayedexpansion
if %day% lss 1 (
set /a month-=1
if !month! lss 1 (
set /a year-=1
set /a month+=12
)
set mcount=0
for %%A in (%numdays%) do (
set /a mcount+=1
if !mcount!==!month! set /a day+=%%A
)
)
set day=0%day%
set month=0%month%
endlocal & (set day=%day:~-2%)& (set month=%month:~-2%)& (set year=%year%)
for /f "tokens=3,4,5 delims=." %%A in ('dir /b /a:d /o:-n "%backuppath%" ^| findstr /r "%username%.Backup.*"') do (
if %year%%month%%day% lss %%A%%B%%C (
goto endofscript
)
)
find . -iname 20??_??_??.txt -type f -mtime +30 -exec rm -f {} \;