kenspencer
asked on
Get File's date/timestamp with VB6
Hi,
I want to know how to use VB6 to get a file's date/timestamp. When an application opens, I want to check to see if I can delete a backup file (should occur 90 days after last use). Probably something straightforward, but I can't think of it right now.
Thanks.
Ken
I want to know how to use VB6 to get a file's date/timestamp. When an application opens, I want to check to see if I can delete a backup file (should occur 90 days after last use). Probably something straightforward, but I can't think of it right now.
Thanks.
Ken
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
This code fragment compares current date/time to the file's "last access date" instead of its "last updated date". Uncomment the "kill" command to make it fully functional after testing.
'Add a reference to the Microsoft Scripting Runtime
'Private Sub Form_Load()
Sub Main()
Dim sFileName As String
sFileName = "C:\SomeFile.bak"
If DateDiff("d", GetLastAccess(sFileName), Now) > 90 Then
MsgBox "File is older than 90 days"
'' Kill (sFileName)
End If
End Sub
Function GetLastAccess(sFile As String)
Dim oFSO As Object
Dim oFile As File
Dim sMsg As String
'Instantiate the FSO and get the oFile
Set oFSO = CreateObject("Scripting.Fi leSystemOb ject")
Set oFile = oFSO.GetFile(sFile)
GetLastAccess = oFile.DateLastAccessed
'Clean up
Set oFile = Nothing
Set oFSO = Nothing
End Function
HTH,
Lynn
'Add a reference to the Microsoft Scripting Runtime
'Private Sub Form_Load()
Sub Main()
Dim sFileName As String
sFileName = "C:\SomeFile.bak"
If DateDiff("d", GetLastAccess(sFileName), Now) > 90 Then
MsgBox "File is older than 90 days"
'' Kill (sFileName)
End If
End Sub
Function GetLastAccess(sFile As String)
Dim oFSO As Object
Dim oFile As File
Dim sMsg As String
'Instantiate the FSO and get the oFile
Set oFSO = CreateObject("Scripting.Fi
Set oFile = oFSO.GetFile(sFile)
GetLastAccess = oFile.DateLastAccessed
'Clean up
Set oFile = Nothing
Set oFSO = Nothing
End Function
HTH,
Lynn
Note: I actually used the project reference for "Windows Script Host Object Model" instead of " Microsoft Scripting Runtime".
ASKER
Everyone,
Thanks for the help. I will check it out either late today or tomorrow.
Ken
Thanks for the help. I will check it out either late today or tomorrow.
Ken
ASKER
Thanks for everyone's assistance. The first answer was what I needed.
Ken
Ken
Ken, are you aware that FileDateTime() returns the LAST MODIFIED date/time? That's not the same as "LAST USED" (unless every usage is also an update) which is what the script I posted uses.
-- Lynn
-- Lynn
ASKER
Lynn,
Backup file will be an Access database with 'Compact on Close' option turned on. I may not have worded the question accurately, so your point is well taken.
Ken
Backup file will be an Access database with 'Compact on Close' option turned on. I may not have worded the question accurately, so your point is well taken.
Ken
This function will get the dateTime Stamp of a file and will automaticly return how many days old is the file
Private Function GetDiffDays(sFile As String)
Dim fDate As Date
Dim tmpNum As Long
fDate = FileDateTime(sFile)
tmpNum = DateDiff("d", fDate, Date)
GetDiffDays = tmpNum
End Function