Solved

How many days old is a file

Posted on 2013-12-20
3
267 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

I use more than 1 computer in my office for various reasons. Multiple keyboards and mice take up more than just extra space, they make working a little more complicated. Using one mouse and keyboard for all of my computers makes life easier. This co…
If your app took Google’s lash recently, here are the 5 most likely reasons.
This video demonstrates how to use each tool, their shortcuts, where and when to use them, and how to use the keyboard to improve workflow.
The viewer will learn how to successfully download and install the SARDU utility on Windows 7, without downloading adware.

823 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