0

. . . so I cut & pasted the code from https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/datecreated-property

and it works just FINE.

It gives me the creation date & time of any file.

BUT

I don't know what "format" it is in, because, having used that routine to acquire "FileDateStamp", this doesn't work :-

    FileAge = Application.Function.Now() - FileDateStamp

I have tried these :-

    Dim FileDateStamp 
    Dim FileDateStamp as Date
    Dim FileDateStamp as Text

Each format provides the datestamp, but none works in the formula. And neither does the "Date(Year, Month, Day)" function on FileDateStamp

7
  • FileAge = Now - FileDateStamp. Use Format$ afterwards to format into something readable. Commented Sep 3, 2021 at 18:09
  • TypeName(f.DateCreated) tells you it's a Date Commented Sep 3, 2021 at 18:38
  • 2
    "this doesn't work" - in what way does it not work? Application.Function.Now() is not a thing (at least in VBA), but just Now() works fine, so Now - FileDateStamp gives you file age in days. Commented Sep 3, 2021 at 18:41
  • @TimWilliams It is not clear if you are saying that it is a date without a time component, or a datetime, which it is. Commented Sep 3, 2021 at 18:46
  • 2
    The Date datatype includes time - there aren't two separate datatypes. There might not be any fractional component in an assigned value though. You can always Debug.Print 1 * FileDateStamp to see the underlying numeric value. Commented Sep 3, 2021 at 18:53

3 Answers 3

2

It isn't clear what you're asking for, but I think this is what you might be looking for.

At the time of writing, the UTC datetime is 2021-09-03 18:54:12. The UTC creation of the file in the code is 2020-06-12 14:04:50 (448 days ago).

Using the DateDiff function:

Function GetFileDateTime(filespec)
    Dim fs, f, s
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFile(filespec)
    GetFileDateTime = f.DateCreated
End Function

Sub ShowSampleDiff()
    Dim fname As String
    fname = "C:\temp\asp.txt"
    Dim diff As Long
    diff = DateDiff("d", GetFileDateTime(fname), Now())
    MsgBox (diff)
End Sub

The code shows a message box with the text "448".

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you ! But I don't have the code for the "DateDif" Function
@RobinClay In my answer I refer to the "DateDiff" function, not "DateDif" with one "f".
This will not return the age of the file, only the difference in calendar dates, as the timeparts will be ignored.
@ Andrew Morton Sticky keyboard, sorry. Yes, "DateDiff" with two Fs
@ Gustav - good enough for MY purpose - but for "completeness" how would you solve that ?
|
1

You are not clear in what you want.

This works to return the number of days (and fractional days)

    Dim FileDateStamp As Date    
    FileDateStamp = f.DateCreated
    s = "Created: " & FileDateStamp
    
    MsgBox s & vbLf & Now - FileDateStamp

For a particular file, the above returns:

enter image description here

Depending on what you want for output, you could format the result of:

FileAge = Now - FileDateStamp

3 Comments

Thank you - but the "challenge" is to find the age of the file, rather than being given the two dates. e.g. That could be followed by code along the lines of "If the file is more than a week old, delete it." i.e.. If FileAge > 7 then delete file
@RobinClay Huh? Silly me. I would have thought the age of the file in Days would be sufficient. What is the problem with you converting the days difference into an "age"?
That WAS exactly my problem ! But "all sorted now". Thanks for your help.
0

It returns a value of data type Date.

However, you can use VBA to reduce to a minimum:

Dim FileSpec As String
Dim FileAge  As Double
Dim TextAge  As String

FileSpec = "d:\path\filename.ext"
FileAge = VBA.Now - VBA.FileDateTime(FileSpec)

' Optionally, format as days.hours:minutes:seconds
TextAge = VBA.Fix(FileAge) & "." & VBA.Format(FileAge - VBA.Fix(FileAge), "hh:nn:ss")

Debug.Print FileAge
' for example: 349.739988425928
Debug.Print TextAge
' for example: 349.17:45:35

6 Comments

Thank you ! But I don't have the Functions "CLng" nor "Format"
@RobinClay - you might find following one or two tutorials on VBA useful - both CLng and Format are built-in.
I don't have the Functions "CLng" nor "Format" ... yes you do. They belong to VBA.
As you seem to require only a numeric age of the file, the solution can be reduced further. See edited answer, please.
Well . . I did suspect that I did have those two functions, so I tried typing them in - but they just produced an error.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.