ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Testing modification time of file (https://www.excelbanter.com/excel-programming/373412-testing-modification-time-file.html)

Peter Clancy

Testing modification time of file
 
I want to test the modification time of a file to see whether it is more than
20 hours old. So far I have:

dim dteFileMod as Date
dim dteNow as Date
dim dteDiff as Date

'Get the date/time properties of the file
dteFileMod = FileDateTime(strFilename)
'Get date/time now
dteNow = Now
'As long as it is less than 20 hours old allow it to be used
dteDiff = dteFileMod - dteNow

I thought that a simple subtraction would work, but it hasnt. If I do a
debug print after it has run I get the following values:

dteDiff = 24/12/1899 22:25:06
dteFileMod = 14/09/2006 15:55:19
dteNow = 21/09/2006 14:20:25

Will someone please explain date/time subtraction in VB; or suggest an
easier way?
Thanks

Die_Another_Day

Testing modification time of file
 
How about:
If (Now - 20) < FileDatetime(strFilename) Then
'Do Stuff
End If

Charles

Peter Clancy wrote:
I want to test the modification time of a file to see whether it is more than
20 hours old. So far I have:

dim dteFileMod as Date
dim dteNow as Date
dim dteDiff as Date

'Get the date/time properties of the file
dteFileMod = FileDateTime(strFilename)
'Get date/time now
dteNow = Now
'As long as it is less than 20 hours old allow it to be used
dteDiff = dteFileMod - dteNow

I thought that a simple subtraction would work, but it hasnt. If I do a
debug print after it has run I get the following values:

dteDiff = 24/12/1899 22:25:06
dteFileMod = 14/09/2006 15:55:19
dteNow = 21/09/2006 14:20:25

Will someone please explain date/time subtraction in VB; or suggest an
easier way?
Thanks



Die_Another_Day

Testing modification time of file
 
Whoops, that's 20 days. use this instead:
How about:
If (Now - (20/24)) < FileDatetime(strFilename) Then
'Do Stuff
End If

Charles

P.S. In VBA 1 in a date format = 1 day, you want 20hrs or 20/24ths of a
day.


Andrew Taylor

Testing modification time of file
 
You're on the right lines, but you shouldn't dim dteDiff
as a date - use

Dim dteDiff as Double ' (should change prefix too...)

and then:

dteDiff = dteNow - dteFileMod ' note change of order
' dteDiff contains days (and part) between the dates
if dteDiff * 24 20 then ' difference is more than 20 hours
' whatever you need to do
end if..

The dteDiff in your example is nearly 6 days before
00:00 on 1 Jan 1900, which is the basepoint for dates.

hth
Andrew


Peter Clancy wrote:
I want to test the modification time of a file to see whether it is more than
20 hours old. So far I have:

dim dteFileMod as Date
dim dteNow as Date
dim dteDiff as Date

'Get the date/time properties of the file
dteFileMod = FileDateTime(strFilename)
'Get date/time now
dteNow = Now
'As long as it is less than 20 hours old allow it to be used
dteDiff = dteFileMod - dteNow

I thought that a simple subtraction would work, but it hasnt. If I do a
debug print after it has run I get the following values:

dteDiff = 24/12/1899 22:25:06
dteFileMod = 14/09/2006 15:55:19
dteNow = 21/09/2006 14:20:25

Will someone please explain date/time subtraction in VB; or suggest an
easier way?
Thanks



Andrew Taylor

Testing modification time of file
 
You're on the right lines, but you shouldn't dim dteDiff
as a date - use

Dim dteDiff as Double ' (should change prefix too...)

and then:

dteDiff = dteNow - dteFileMod ' note change of order
' dteDiff contains days (and part) between the dates
if dteDiff * 24 20 then ' difference is more than 20 hours
' whatever you need to do
end if..

The dteDiff in your example is nearly 6 days before
00:00 on 1 Jan 1900, which is the basepoint for dates.

hth
Andrew


Peter Clancy wrote:
I want to test the modification time of a file to see whether it is more than
20 hours old. So far I have:

dim dteFileMod as Date
dim dteNow as Date
dim dteDiff as Date

'Get the date/time properties of the file
dteFileMod = FileDateTime(strFilename)
'Get date/time now
dteNow = Now
'As long as it is less than 20 hours old allow it to be used
dteDiff = dteFileMod - dteNow

I thought that a simple subtraction would work, but it hasnt. If I do a
debug print after it has run I get the following values:

dteDiff = 24/12/1899 22:25:06
dteFileMod = 14/09/2006 15:55:19
dteNow = 21/09/2006 14:20:25

Will someone please explain date/time subtraction in VB; or suggest an
easier way?
Thanks



Peter Clancy

Testing modification time of file
 
What can I say? Genius, it works like a charm.
Thanks

"Die_Another_Day" wrote:

Whoops, that's 20 days. use this instead:
How about:
If (Now - (20/24)) < FileDatetime(strFilename) Then
'Do Stuff
End If

Charles

P.S. In VBA 1 in a date format = 1 day, you want 20hrs or 20/24ths of a
day.




All times are GMT +1. The time now is 10:39 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com