VBA retrieve text file creation date

Fat Cat

Active Member
Joined
Nov 5, 2004
Messages
336
Is there a simple way to retrieve the date a text file was created ?

I'm using the following code to read the contents of the file, but need to know the date it was created.

many thanks

Code:
Set fso = CreateObject("Scripting.FileSystemObject")
Set fil = fso.GetFile(fName)                            'the file we want to open
Set txt = fil.OpenAsTextStream(1)                       'open file for reading as text
strtxt = txt.ReadAll                                    'read entire contents into strtxt variable
txt.Close                                               'close the file
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
For a text file, maybe this, at least it seems to work for me:


Function test()
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
test = fso.GetFile("C:\Your\File\Path\YourFileNameCopy.txt").DateCreated
End Function


Then in a macro:

Sub Test1()
MsgBox test
End Sub


or as a simple udf
=test()
 
Upvote 0
Just out of curiousity, Tom:

Why make that sub a function, if it's not returning a value?
 
Upvote 0
Sorry, I don't understand the question. It could have been a sub or a function, I created a function as the base answer just out of interest for flexibility with how it might be applied, either calling it from a macro, or as a UDF in my two examples for application. Either way, it does return a numeric value though, at least in 2003, which when formatted in a date and time format yields a date and time-looking creation date.
 
Upvote 0
I see what you mean. It is returning a value, just Variant, right? I must have missed where this line:
Code:
test = fso.GetFile("C:\Your\File\Path\YourFileNameCopy.txt").DateCreated
is set equal to the function.

I tend to ask stupid questions at 3 o'clock in the morning. :oops:
 
Upvote 0

Forum statistics

Threads
1,215,700
Messages
6,126,305
Members
449,308
Latest member
VerifiedBleachersAttendee

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top