Solved

How many days old is a file

Posted on 2013-12-20
3
271 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I previously wrote an article addressing the use of UBCD4WIN and SARDU. All are great, but I have always been an advocate of SARDU. Recently it was suggested that I go back and take a look at Easy2Boot in comparison.
A list of useful business intelligence software.
This video will demonstrate how to find the puppet warp tool from the edit menu and where to put the points to edit.
XMind Plus helps organize all details/aspects of any project from large to small in an orderly and concise manner. If you are working on a complex project, use this micro tutorial to show you how to make a basic flow chart. The software is free when…

733 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