Option without being extensive, without using the command forfiles
and compatible with the Windows XP®
:
@echo off & setlocal enabledelayedexpansion && cd /d d:\files
for /f "tokens=2delims==." %%i in ('wmic OS Get localdatetime /value ^|findstr /r [0-9]')do set "_data=%%i"
set "_dt=!_data:~0,4!!_data:~4,2!" && if 1!_dt:~-2! leq 106 (set /a "_dt=!_dt!-94") else set /a "_dt=!_dt!-6"
set "_msg=Arquivo tem mais de 6 meses" & for /f "tokens=1*delims=." %%a in ('where .:"bkp_*.*"')do set "_f=%%a.%%b" && call :-[
goto :eof
:-[
set "_f_dt=!_f:~-10!" & call set "_f_dt=!_f_dt:~0,6!" & if !_f_dt! leq !_dt_! (
call echo/"!_f!": !_msg! & echo/del /q /f "!_f!") else call echo/"!_f!": !_msg:mais=menos!
timeout /t 1 >nul & echo/ & exit /b
Take the current date in number format concatenating year+month with and month:
for /f "tokens=2delims==." %%i in ('wmic OS Get localdatetime /value ^|findstr /r [0-9]')do set "_data=%%i"
Results in a variable (!_data!
) with the value 20190630
Date is obtained with a substring/variable (!_dt!
) 6-digit order year month:
set "_dt=!_data:~0,4!!_data:~4,2!"
If the month is less/equal to 107 where positive/negative, subtract 94 or 6 for date/current - 6 months:
Obs.: month file is compared with current month - 6 months, this way: if 1!_dt:~-2! leq 106
set "_dt=!_data:~0,4!!_data:~4,2!" && if 1!_dt:~-2! leq 106 (set /a _dt=!_dt!-94) else (set /a _dt=!_dt!-6)
A variable is created to display messages according to the date/age of the file on the screen:
set "_msg=Arquivo tem mais de 6 meses"
Implement a for loop to get: date in the file name/full name + call to the :label
: call :-[
for /f "tokens=1*delims=." %%a in ('where .:"bkp_*.*"')do set "_f=%%a.%%b" && call :-[
Within the label
, filter the file date via substring/variable (!_f_dt!
):
set "_f_dt=!_f:~-10!" & call set "_f_dt=!_f_dt:~0,6!"
One uses a if
to verify that the file is 6 months old and takes action for the applicable case:
if !_f_dt! leq !_dt! (
call echo/"!_f!": !_msg! & echo/del /q /f "!_f!") else call echo/"!_f!": !_msg:mais=menos!
timeout /t 1 >nul & echo/ & exit /b
you are using windows XP?
– user82136
these last two digits are the month?
– user82136
@Cristianmota Yes, I’m using Winxp 32-Bits and yes, the two digits are the month, which will serve as a criterion for when to delete. I was about to do it by command line, but the code is getting big.
– OldSchool