Solved

How many days old is a file

Posted on 2013-12-20
3
258 Views
Last Modified: 2014-01-04
I am looking for a utility that will read the date stamp of a file and determine  how many days old that file is. I want to use the output in a Windows batch file that says:
if filename is more than %n% days old, do something.
Is anyone aware of a utility or some other way of doing this? Thanks in advance.
0
Comment
Question by:geistman
3 Comments
 
LVL 12

Expert Comment

by:Freya28
Comment Utility
a GUI based utility that I use is called Treesize.  lot of good stuff in that app.
0
 
LVL 12

Assisted Solution

by:ktaczala
ktaczala earned 200 total points
Comment Utility
This will do it. Copy this to a .bat file **NOTE: no error checking is done
rem ******************************************
rem * Here's the basics, converts todays date to Julian   *
rem * diff contains the number of days to trigger on    *
rem * gets the date of a file and converts to julian    *
rem * gets the difference of the two dates              *
rem * ************************************
@echo off
set datetoday=%date:~4,10%

rem if number of days older = 5 then do something, can be variable or static
set diff=5

rem get date of file
rem set filename from variable or static
set file1=c:\path\filename.xxx
for %%a in (%file1%) do set date1=%%~ta
rem echo %date1%

FOR /F "TOKENS=1-3 DELIMS=/" %%A IN ("%datetoday%") DO SET MM=%%A& SET DD=%%B& SET YY=%%C
REM ELIMINATE LEFT ZEROS
SET /A DD=10%DD% %% 100, MM=10%MM% %% 100
REM CALCULATE JULIAN DAY NUMBER
IF %MM% LSS 3 SET /A MM+=12, YY-=1
SET /A A=YY/100, B=A/4, C=2-A+B, E=36525*(YY+4716)/100, F=306*(MM+1)/10, juliandateoftoday=C+DD+E+F-1524

FOR /F "TOKENS=1-3 DELIMS=/" %%A IN ("%date1%") DO SET MM=%%A& SET DD=%%B& SET YY=%%C
rem ELIMINATE LEFT ZEROS
SET /A DD=10%DD% %% 100, MM=10%MM% %% 100
REM CALCULATE JULIAN DAY NUMBER
IF %MM% LSS 3 SET /A MM+=12, YY-=1
SET /A A=YY/100, B=A/4, C=2-A+B, E=36525*(YY+4716)/100, F=306*(MM+1)/10, juliandateoffile=C+DD+E+F-1524

set /A X=juliandateoftoday-juliandateoffile

rem *****************************
rem * Here you can do something *
rem *****************************


rem these lines below just show values

ECHO %x% = Difference

ECHO %juliandateoftoday% = Today's date

ECHO %juliandateoffile% = File date
0
 
LVL 68

Accepted Solution

by:
Qlemo earned 300 total points
Comment Utility
forfiles (http://technet.microsoft.com/en-us/library/cc753551.aspx) was used "in the old days", and many folks are still with it, to apply commands to files based on their age.

Doing date arithmetics in cmd.exe batches is possible as we can see, but cumbersome. Above tricky code could be made even simpler (by using subroutines), if that matters. And there might be an issue with the Datetoday line, as some locals do not have the abbreviated weekday in front, and of course the assumed format is mm/dd/yyyy.
Further, the code as shown does only work for a single file. Do you want to know the files matching a certain age, or test a specific file only?
rem ******************************************
rem * Here's the basics, converts todays date to Julian   *
rem * diff contains the number of days to trigger on    *
rem * gets the date of a file and converts to julian    *
rem * gets the difference of the two dates              *
rem * ************************************
@echo off
set datetoday=%date:~-10%

rem if number of days older = 5 then do something, can be variable or static
set diff=5

rem get date of file
rem set filename from variable or static
set file1=c:\path\filename.xxx
for %%a in (%file1%) do set date1=%%~ta
rem echo %date1%

call :julian %datetoday% & set juliandateoftoday=%julian%
call :julian %date1%     & set juliandateoffile=%julian%

set /A X=juliandateoftoday-juliandateoffile

rem *****************************
rem * Here you can do something *
rem *****************************

rem these lines below just show values

ECHO %x% = Difference

ECHO %juliandateoftoday% = Today's date

ECHO %juliandateoffile% = File date
exit /b

:julian %date%
set dt=%1
REM no leading zeros, those would force octal interpretation!
set /A DD=10%dt:~-7,2% %% 100
set /A MM=10%dt:~-10,2% %% 100
set yy=%dt:~-4%

IF %MM% LSS 3 SET /A MM+=12, YY-=1
SET /A A=YY/100, B=A/4, C=2-A+B, E=36525*(YY+4716)/100, F=306*(MM+1)/10, julian=C+DD+E+F-1524
exit /b

Open in new window

0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Today companies are subjected to more-and-more data, and it won't stop any time soon.  But there are obvious opportunities for reducing data, particularly data duplicated among companies.
Healthcare organizations in the United States must adhere to the guidance of both the HIPAA (Health Insurance Portability and Accountability Act) and HITECH (Health Information Technology for Economic and Clinical Health Act) for securing and protec…
This video will demonstrate how to find the puppet warp tool from the edit menu and where to put the points to edit.
Using Adobe Premiere Pro, the viewer will learn how to set up a sequence with proper settings, importing pictures, rendering, and exporting the finished product.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now