Solved

How many days old is a file

Posted on 2013-12-20
3
269 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
ID: 39732552
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
ID: 39733280
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 69

Accepted Solution

by:
Qlemo earned 300 total points
ID: 39735187
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Internet Connection Util 8 102
CD-RW/DVD-ROM DRIVE 3 57
How do I paste an image of a signature into Nuance Power PDF 5 37
getting movie maker for windows 10 5 42
Workplace bullying has increased with the use of email and social media. Retain evidence of this with email archiving to protect your employees.
Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
Video by: Tony
This video teaches viewers how to export a project from Adobe Premiere Pro and the various file types involved.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

860 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