You are not logged in.

#1 07 Jan 2015 01:46

eurotool
New Member
Registered: 07 Jan 2015
Posts: 2

Age of file / Time difference

Hello everyone,

I'm in the need of a script to calculate, as title says, the age of a file or the difference between two times and dates. After fruitlessly searching for hours on different forums, I decided to build my own from little snippets found on different places.

Unfortunately I'm now stuck at having to calculate the number of days in each month of both ends, but also to check if both were leap years or not. And how about daylight savings now? This is getting way too much trouble for a simple task that has to be repeated quickly and often: typing in and scraping data from a website that does the job would even be better in that case. Do any of you know a way to simply get an accurate representation of the age of a file? As long as it's possible to read the value from cmd, it can be in any language.

Thanks in advance  smile

Offline

#2 07 Jan 2015 07:35

foxidrive
Member
Registered: 04 Apr 2013
Posts: 339

Re: Age of file / Time difference

How do you want the file age?  In seconds from 1/1/1970?  That's the Unix way.

Give examples of what you are trying to do.

Offline

#3 07 Jan 2015 11:48

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,129

Re: Age of file / Time difference

The heavy lifting has already been done:

http://ss64.com/nt/syntax-datemath.html
http://ss64.org/viewtopic.php?id=1079

Shouldn't be too tricky to mash these two together.


cmd | *sh | ruby | chef

Offline

#4 07 Jan 2015 19:49

eurotool
New Member
Registered: 07 Jan 2015
Posts: 2

Re: Age of file / Time difference

Can't believe I actually skipped that one. Thanks for the help man, it works like a charm!

I'll paste my code here for future reference. A function to calculate the difference in hours has been added; it could be easily rewritten to catch minutes and seconds. I'm also using a slightly modified version of SS64's DateMath.cmd, which outputs the number of days on a single line.

:: Old date
for /f %%G in ('cscript /nologo external\getdate.vbs') do set _dtm=%%G
set _yyyy=%_dtm:~0,4%
set _mm=%_dtm:~4,2%
set _dd=%_dtm:~6,2%
set _hh=%_dtm:~8,2%
set _nn=%_dtm:~10,2%
set OldDate=%_yyyy% %_mm% %_dd%
set OldHour=%_hh%

:: New date
for /f %%G in ('cscript /nologo external\getdate.vbs') do set _dtm=%%G
set _yyyy=%_dtm:~0,4%
set _mm=%_dtm:~4,2%
set _dd=%_dtm:~6,2%
set _hh=%_dtm:~8,2%
set _nn=%_dtm:~10,2%
set NewDate=%_yyyy% %_mm% %_dd%
set NewHour=%_hh%

for /f %%G in ('call external\DateMath.cmd %NewDate% - %OldDate%') do set NbDays=%%G
::call external\DateMath.cmd %NewDate% - %OldDate%

set /a NbHours=%NewHour% - %OldHour%
if %NbHours% lss 0 set /a "NbDays-=1"&set /a "NbHours*=-1"

echo %NbDays% days, %NbHours%h

Offline

Board footer

Powered by