Solved

How to get the dates of a folder?

Posted on 1998-12-16
6
598 Views
Last Modified: 2012-08-13
I'm using the following code to get the dates
from files, but it does not work on folders...
(I get the "Could not open file!" MsgBox,
so the problem must be with the OpenFile...)
What is wrong?

Code:

Const OFS_MAXPATHNAME As Integer = 128

Declare Function OpenFile& Lib "kernel32" (ByVal lpFileName As String, lpReOpenBuff As OFSTRUCT, ByVal wStyle As Long)
Declare Function GetFileTime Lib "kernel32" (ByVal hFile As Long, lpCreationTime As FILETIME, lpLastAccessTime As FILETIME, lpLastWriteTime As FILETIME) As Long

Public Type FILETIME
  dwLowDateTime As Long
  dwHighDateTime As Long
End Type

Public Type OFSTRUCT
  cBytes As Byte
  fFixedDisk As Byte
  nErrCode As Integer
  Reserved1 As Integer
  Reserved2 As Integer
  szPathName As String * OFS_MAXPATHNAME
End Type


Private Sub MySub()
  Dim hFile As Long
  Dim tCreateTime As FILETIME
  Dim tAccessTime As FILETIME
  Dim tWriteTime As FILETIME

  hFile = OpenFile("C:\Windows", 0)
  If hFile = -1 Then
      MsgBox "Could not open file!"
  End If

  If GetFileTime(hFile, tCreateTime, tAccessTime, tWriteTime) = 0 Then
    Call CloseHandle(hFile)
    MsgBox "Could not get file time!"
  End If

  Call CloseHandle(hFile)
End Sub
0
Comment
Question by:Olli083097
  • 3
  • 2
6 Comments
 
LVL 12

Expert Comment

by:mark2150
ID: 1450606
When I need something like this I use a "Q&D" (quick & dirty) method. I simply ask DOS for the info!

If you do:

SHELL( environ("comspec") & " /c dir \ /s /ad /-p /on > X", 1)

This will give you an ASCII text file containing all of the directories on the current disk into a little file called 'X'. It's simple enough to scan this file for the data you need.

While this is not particularly elegant, it does work very well even on networks. In my book a Q&D answer that is *right* and easy to code is worth more than an "elegant" answer that takes a week of programmer time to write & debug.

M

0
 
LVL 1

Expert Comment

by:muffinthedog
ID: 1450607
Or you could just use FindFirstFile and use the FILETIME returned in the ftCreationTime member of the WIN32_FIND_DATA structure. If you want an example...
0
 

Author Comment

by:Olli083097
ID: 1450608
mark2150: I don't want to do it the "Q&D" in this case...
muffinthedog: What is FindFirstFile???
If this can help me to find the Creation, Last Accessed and Changed time for folders. Then I guess I'm interested. So, how do I use it?

BTW: I'm not sure it is possible to get the Change time for folders... but if it is...
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 1

Expert Comment

by:muffinthedog
ID: 1450609
Olli,
    LastAccess time and LastWrite time for a folder will return same as creation time... sorry

crummy little example though...

Dim FD As WIN32_FIND_DATA
Dim stCreationTime As SYSTEMTIME
Dim stLastAccessTime As SYSTEMTIME
Dim stLastWriteTime As SYSTEMTIME
FindFirstFile "C:\Download", FD
FileTimeToSystemTime FD.ftCreationTime, stCreationTime
FileTimeToSystemTime FD.ftLastAccessTime, stLastAccessTime
FileTimeToSystemTime FD.ftLastWriteTime, stLastWriteTime
MsgBox stCreationTime.wDay & "/" & stCreationTime.wMonth & "/" & stCreationTime.wYear
MsgBox stLastAccessTime.wDay & "/" & stLastAccessTime.wMonth & "/" & stLastAccessTime.wYear
MsgBox stLastWriteTime.wDay & "/" & stLastWriteTime.wMonth & "/" & stLastWriteTime.wYear

Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Declare Function FileTimeToSystemTime Lib "kernel32" (lpFileTime As FILETIME, lpSystemTime As SYSTEMTIME) As Long

Public Const MAX_PATH = 260

Type FILETIME
        dwLowDateTime As Long
        dwHighDateTime As Long
End Type

Type WIN32_FIND_DATA
        dwFileAttributes As Long
        ftCreationTime As FILETIME
        ftLastAccessTime As FILETIME
        ftLastWriteTime As FILETIME
        nFileSizeHigh As Long
        nFileSizeLow As Long
        dwReserved0 As Long
        dwReserved1 As Long
        cFileName As String * MAX_PATH
        cAlternate As String * 14
End Type

Type SYSTEMTIME
        wYear As Integer
        wMonth As Integer
        wDayOfWeek As Integer
        wDay As Integer
        wHour As Integer
        wMinute As Integer
        wSecond As Integer
        wMilliseconds As Integer
End Type



0
 

Author Comment

by:Olli083097
ID: 1450610
muffinthedog:
Thanx!!! The code you gave me worked fine.

BTW: I don't know what it means, but I get dates on LastAccess and on LastWrite too. And they are different from creation time.

You can get the points if you answer the question!!!
0
 
LVL 1

Accepted Solution

by:
muffinthedog earned 50 total points
ID: 1450611
Olli,
    see comments. thanx...


0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now