Here is a modified version of Aacini's
post, from back in 2013. I only amended it to use powershell
to get today's date, instead of using the locale specific %date%
variable and some changes to accomodate your environment. So I take no credit for this whatsoever.
@echo off
setlocal EnableDelayedExpansion
pushd "K:\Bck"
for /f "tokens=*" %%i in ('PowerShell -Command "& {Get-Date -format 'yyyyMMdd'}"') do set todayDate=%%i
call :DateToJDN %todayDate% todayJDN=
for /F "tokens=1-4* delims=_-" %%a in ('dir /B /A-D "Backup_*-*-*.txt"') do (
call :DateToJDN %%b%%c%%~nd fileJDN=
set /A daysOld=todayJDN - fileJDN
if !daysOld! gtr 180 (
echo File "%%a_%%b-%%c-%%d" is !daysOld! days old
del "%%a_%%b-%%c-%%d" /Q
)
)
popd
goto :eof
:DateToJDN yyyymmdd jdn=
set yyyymmdd=%1
set /A yyyy=%yyyymmdd:~0,4%, mm=1%yyyymmdd:~4,2% %% 100, dd=1%yyyymmdd:~6% %% 100
set /A a=(mm-14)/12, %2=(1461*(yyyy+4800+a))/4+(367*(mm-2-12*a))/12-(3*((yyyy+4900+a)/100))/4+dd-32075
exit /B
This will NOT yet delete anything. It will simply echo the commands to the screen. If you are sure the echo
'd results resemble what you want, you can remove the echo
from the line echo del "%%a_%%b_%%c_%%d" /Q
You also need to set your days accordingly in the line if !daysOld! gtr 180 (