Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 608
  • Last Modified:

How to get the dates of a folder?

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
Olli083097
Asked:
Olli083097
  • 3
  • 2
1 Solution
 
mark2150Commented:
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
 
muffinthedogCommented:
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
 
Olli083097Author Commented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
muffinthedogCommented:
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
 
Olli083097Author Commented:
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
 
muffinthedogCommented:
Olli,
    see comments. thanx...


0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now