Solved

How to get the dates of a folder?

Posted on 1998-12-16
6
601 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB 6.0 printer how to align 6 67
bit defender blocks good applications 2 101
Spell Check in VB6 13 126
MsgBox 4 68
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…

856 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