Operations with dates (delete file with date in name and earlier than 6 months) without using Forfiles

Asked

Viewed 1,929 times

4

It’s been a long time since I’ve dealt with VBS, so I thought I’d just do it on the command line I need to delete files earlier than 6 months from the current date, as the files I am managing have date-based names that were generated in that layout:

bkp_201605.xml
log_201601.tmp
bkp_201704.xml
log_201702.tmp
bkp_201803.xml
log_201802.tmp

So I did so:

set /a lastyear=%date:~-4% - 1
del *_%lastyear%*.xml

The problem is that if you are in January this medium will delete last month’s file together (December/previous year), and I can’t risk losing recent histories.

To solve it I would have to make a huge code to calculate the difference six months before.

I was gonna do it using the remote forfiles to delete by actual file modification date, however it does not works (nay available) in the Windows XP.

If anyone has an idea how to fix this.

  • you are using windows XP?

  • these last two digits are the month?

  • 1

    @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.

3 answers

3

Suppose your files are all in this format as you reported and that are in a folder called "ARCHIVES", just run the script in the same directory as this folder.

Script will delete all files older than 6 months.

@echo off
cd ARQUIVOS
dir /b>%tmp%\fff.txt

setlocal enabledelayedexpansion
for /f "tokens=2 delims=_" %%R in (%tmp%\fff.txt) do (
set v=%%R
set fano=!v:~0,4!
set fmes=!v:~4,2!
set ano=!date:~-4!
set mes=!date:~,5! &set mes=!mes: =!&set mes=!mes:~-2!


if !ano! equ !fano! (set /a data=!mes!-!fmes!) Else (
if !mes! geq 07 (del /q /f ***%%R) Else (
if !mes! equ 06 (if !fmes! leq 11 (del /q /f ***%%R)) Else (
if !mes! equ 05 (if !fmes! leq 10 (del /q /f ***%%R)) Else (
if !mes! equ 04 (if !fmes! leq 09 (del /q /f ***%%R)) Else (
if !mes! equ 03 (if !fmes! leq 08 (del /q /f ***%%R)) Else (
if !mes! equ 02 (if !fmes! leq 07 (del /q /f ***%%R)) Else (
if !mes! equ 01 (if !fmes! leq 06 (del /q /f ***%%R)))))))))
if !data! geq 07 (del /q /f ***%%R)
set /a arq=!ano!-!fano! &if !arq! gtr 1 (del /q /f ***%%R))
  • I found the code interesting, but the for /f "tokens=2 delims=_" %R in (%tmp% fff.txt) do ( goes straight through for some reason, the system even deleted 1/3 of the files, but those of long before continued in the folder. Of course I made a copy of the data to test without modifying your code. I’m analyzing what I might have done wrong.

  • Bro, I tested with multiple files in the same format as your files... deleted files from 2012 to 2017. if you want to do a video tonight showing. tested in windows 7

  • also check if the Pc date is correct.

  • Is yes, actually I tested on a virtual machine and previously tested on Windows 8, but I solved the problem by making on the basis of the name of the files, making an algorithm that returns the month number by subtracting by 6 and if it was 0 or less this number subtracts from 12 giving the previous year’s extended month.

  • what may have happened to not have deleted all the files, is that long be in another format, different from the ones you mentioned in the question, if that is will have to make another script for this old format.

2


Finally I ended up finishing the code, as I imagined it got big, but not as much as I thought it would (This SCRIPT got a level above the FILES folder):

Comments show how I did each step

SET YNOW=%date:~-4%
SET YSIX=%date:~-4%
SET MNOW=%date:~-7,2%
SET /A MSIX=%mnow% - 6

REM ############### VERIFICAR ANO
IF %msix% LEQ 0 (SET /A YSIX=YNOW - 1)

REM ############### VERIFICAR MESES
REM ### igual a 0 equivale mes 12 do ano anterior
IF %msix% EQU 0 (SET MSIX=12)

REM ### NUMEROS NEGATIVOS
IF %msix% LSS 0 (SET /A MSIX=12%msix%)

REM ### MENOR QUE 10
IF %msix% LSS 10 (SET MSIX=0%msix%)

SET XMLS=bkp_%ysix%%msix%.xml
SET LOGS=log_%ysix%%msix%.tmp

echo ############## DELETAR XMLS ANTES DE %ysix%%msix%
dir FILES\*.xml /b>xml.log
FOR /F %%X IN (xml.log) DO (
    IF %%X LSS %xmls% (del FILES\%%X)
)
echo ####### END XMLS
echo ############## DELETAR LOGS ANTES DE %ysix%%msix%
dir FILES\*.tmp /b>tmp.log
FOR /F %%L IN (tmp.log) DO (
    IF %%L LSS %logs% (del FILES\%%L)
)
echo ####### END LOGS
pause

Batch Script - Old delete


As I was not at work I did in my personal Windows 8, to make sure that it would work I installed a virtual machine on my laptop, and I did the tests on it and it worked, to be honest I was getting in a single detail, but for amazing that it seems the command line understood the names of the file containing numbers, for example:

REM A Verficar       Data 6 meses
IF bkp_201706.xml LSS bkp_201709.xml (
echo ESTE ARQUIVO TEM MAIS DE 6 MESES
) ELSE (
echo ESTE ARQUIVO TEM MENOS DE 6 MESES
)

Returned ESTE ARQUIVO TEM MAIS DE 6 MESES


REM A Verficar       Data 6 meses
IF bkp_201803.xml LSS bkp_201709.xml (
echo ESTE ARQUIVO TEM MAIS DE 6 MESES
) ELSE (
echo ESTE ARQUIVO TEM MENOS DE 6 MESES
)

Returned ESTE ARQUIVO TEM MENOS DE 6 MESES

  • have to join the 3 parts?

  • 1

    No, the last two are examples of the very explanation that was written above, to understand how I got to the result.

  • I tested your code here, but did not delete any file, only created two empty text files... mine is still working properly.

  • 1

    I put the code example on Google Drive and shared the link. The ORIGINALS folder is just to keep the example files, the FILES folder is where the Batch Script will manage what should or should not be deleted.

1

Option without being extensive, without using the command forfiles and compatible with the Windows XP®:


  • Code commented below:

@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


inserir a descrição da imagem aqui


  • Commented code:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.