Operations with dates

2
It's been a long time since I moved with VBS , so I thought of doing only command line, I'm going to delete files from 6 months ago, as the files I'm managing have names on the basis of date that were generated:

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

So I did like this:

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

The problem is that if you are in JANUARY this medium will delete the file from last month together, I can not risk losing recent history.

To solve I would have to make a huge code to be able to calculate the difference from six months earlier. I was going to do it for forfiles to delete by the actual modification date of the file, but it does not work on Windows XP .

If someone has an idea how to solve it.

    
asked by anonymous 08.03.2018 / 01:05

2 answers

1

Finally I finished the code, as I imagined it was great, but not as much as I thought it would (This SCRIPT was one 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 on my Windows 8 personal, to make sure it would work, I installed a virtual machine in my notebook, and I ran the tests on it and it worked, to be frank I was getting in a single detail, but however incredible the command line looks like the names of the files 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

    
09.03.2018 / 20:39
2
  

Suppose your files are all in this format as reported and   that are in a folder called FILES ", 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))
    
09.03.2018 / 09:02