Solved

How to get the dates of a folder?

Posted on 1998-12-16
6
600 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

831 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