?
Solved

How many days old is a file

Posted on 2013-12-20
3
Medium Priority
?
275 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 800 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 70

Accepted Solution

by:
Qlemo earned 1200 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
If you're a modern-day technology professional, you may be wondering if certifications are really necessary. They are. Here's why.
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.
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…
Suggested Courses

752 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