0

I now have a seemingly innocuous requirement to be put in a .bat file ,I am dealing with Batch code for the first time ...found a page here that deals with something similar but it takes the modified date as reference.

In a particular folder X , Delete all files older than N days by parsing through their file name. where file name format is Name_YYYYMMDD.log

But note : 1. Do not want the last modified date as reference (log might have been accessed/modified by other programs/apps) 2.No permission to install other utilities.

EDIT :

  • The FORFILES command worked perfectly well for this job (but only drawback is it takes'modified date' as reference)
  • The script below by Aacini works fine after tweaking it to specifications.
  • Due to additional conditions which further complicates the entire scenario ,we have decided to move away from batch and do it in Powershell or AutoIT.

Thanks!.

Community
  • 1
  • 1
spyky_42
  • 111
  • 1
  • 6

2 Answers2

5

The Batch file below convert file date to Julian Day Number, that is a sequential number of days, and use it to know how many days old is each one. The number of days to delete files is given in the parameter.

@echo off
setlocal EnableDelayedExpansion

rem Get Julian Day Number of today's date
rem The assumed format is MM/DD/YYYY, change C-A-B order in accordance with your locale
for /F "tokens=1-3 delims=/" %%a in ("%date%") do (
   set todayDate=%%c%%a%%b
)
call :DateToJDN %todayDate% todayJDN=

for /F "tokens=1-3 delims=_." %%a in ('dir /B /A-D *.*') do (
   call :DateToJDN %%b fileJDN=
   set /A daysOld=todayJDN - fileJDN
   if !daysOld! gtr %1 (
      echo File "%%a_%%b.%%c" is !daysOld! days old
   )
)
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

Test this program and change the echo File "%%a_%%b.%%c" ... command by the desired del "%%a_%%b.%%c" one.

Reference: http://www.hermetic.ch/cal_stud/jdn.htm#comp

Aacini
  • 65,180
  • 12
  • 72
  • 108
  • It worked fine after some tweaking but the requirements changed again :( and we are moving away from batch to Powershell/AutoIT. Thanks!. – spyky_42 May 18 '13 at 07:14
  • @spyky_42: May I ask you to give me some points for my answer? Perhaps an upvote? Thanks... – Aacini May 18 '13 at 14:52
  • 1
    I'll give you an upvote for him :) I can't believe how close the code I was writing for this is compared to yours. Nearly the same with a few minor differences that you handled better anyway. – Matt Williamson May 20 '13 at 10:33
  • @Aacini Could you explain your answer and add where i can add the directory or the target folder path. TIA – Sakthivel Feb 17 '17 at 14:41
0

here you have, it will delete file e.g. 20180105.log on January 5, it's working with date format YYYY-MM-DD, for date format MM/DD/YYYY change this delims=/ and check "echo todayDate !todayDate!" for manipulating a b c

cls
@echo off
setlocal EnableDelayedExpansion

for /F "tokens=1-3 delims=-" %%a in ("%date%") do (
   set todayDate=%%a%%b%%c
)

rem echo todayDate !todayDate!

for /F "tokens=1-3 delims=_." %%a in ('dir /B /A-D *.*') do (

   set /A daysOld=todayDate - "%%a"

   if !daysOld! EQU 0 (
      echo deleting.. %%a.%%b
      echo del %%a.%%b
   )
)

Test it and change the echo del %%a.%%b to del %%a.%%b