I don't have that, specifically, but here is some code I wrote to walk through a directory tree setting all the dates. You should be able to modify it to count directory sizes. One thing to watch out for is overflow. A 'long' only holds so much, and with everyone buying 30Gb drives nowadays, if someone were to run your app on c:\, it would blow up. Note in the WIN32_FIND_DATA structure there are two separate variables to hold the file size. You would have to check for overflow before adding each file's size to your totals.<br><FONT FACE=monospace><br>Option Explicit<br><br>Private Const OPEN_EXISTING = 3<br>Private Const GENERIC_WRITE = &H40000000<br>Private Const FILE_SHARE_READ = &H1<br>Private Const FILE_SHARE_WRITE = &H2<br><br>Private Const FILE_ATTRIBUTE_DIRECTORY = &H10<br>Private Const FILE_ATTRIBUTE_READONLY = &H1<br>Private Const FILE_ATTRIBUTE_HIDDEN = &H2<br><br>Private Const INVALID_HANDLE_VALUE = &HFFFFFFFF<br><br>Private Const MAX_PATH = 260<br><br>Private Type SYSTEMTIME<br> wYear As Integer<br> wMonth As Integer<br> wDayOfWeek As Integer<br> wDay As Integer<br> wHour As Integer<br> wMinute As Integer<br> wSecond As Integer<br> wMilliseconds As Integer<br>End Type<br>Private Type FILETIME<br> dwLowDateTime As Long<br> dwHighDateTime As Long<br>End Type<br>Private Type WIN32_FIND_DATA<br> dwFileAttributes As Long<br> ftCreationTime As FILETIME<br> ftLastAccessTime As FILETIME<br> ftLastWriteTime As FILETIME<br> nFileSizeHigh As Long<br> nFileSizeLow As Long<br> dwReserved0 As Long<br> dwReserved1 As Long<br> cFileName As String * MAX_PATH<br> cAlternate As String * 14<br>End Type<br><br>Private Declare Function SetFileTime Lib "kernel32" (ByVal hFile As Long, lpCreationTime As Long, lpLastAccessTime As Long, lpLastWriteTime As FILETIME) As Long<br>Private Declare Function SystemTimeToFileTime Lib "kernel32" (lpSystemTime As SYSTEMTIME, lpFileTime As FILETIME) As Long<br>Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long<br>Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long<br>Private Declare Function LocalFileTimeToFileTime Lib "kernel32" (lpLocalFileTime As FILETIME, lpFileTime As FILETIME) As Long<br><br>Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long<br>Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long<br>Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long<br><br>Private m_DirName As String<br>Private m_Recurse As Boolean<br>Private m_DoHidden As Boolean<br><br>Private m_File_Time As FILETIME<br><br>Private Sub cmdGo_Click()<br><br> Dim SysTime As SYSTEMTIME<br> Dim Local_Time As FILETIME<br> Dim File_Time As FILETIME<br> Dim lRC As Long<br> <br> Screen.MousePointer = vbHourglass<br> <br> ' User wants to go thru all child directories?<br> If chkRecurse = vbChecked Then<br> m_Recurse = True<br> Else<br> m_Recurse = False<br> End If<br> <br> 'See if we should skip hidden files<br> If chkHidden = vbChecked Then<br> m_DoHidden = True<br> Else<br> m_DoHidden = False<br> End If<br> <br> ' store values entered by user in systemtime structure<br> SysTime.wYear = dtpDate.Year<br> SysTime.wMonth = dtpDate.Month<br> SysTime.wDay = dtpDate.Day<br> SysTime.wDayOfWeek = dtpDate.DayOfWeek<br> SysTime.wHour = dtpTime.Hour<br> SysTime.wMinute = dtpTime.Minute<br> SysTime.wSecond = dtpTime.Second<br> SysTime.wMilliseconds = 0<br> <br> 'Convert to a filetime structure<br> lRC = SystemTimeToFileTime(SysTime, Local_Time)<br> If lRC = 0 Then<br> Screen.MousePointer = vbDefault<br> MsgBox "Error occurred changing system time to local time."<br> Exit Sub '<-------<br> End If<br> <br> 'Convert to UTC time<br> lRC = LocalFileTimeToFileTime(Local_Time, File_Time)<br> If lRC = 0 Then<br> Screen.MousePointer = vbDefault<br> MsgBox "Error occurred changing local time to a file time."<br> Exit Sub '<-------<br> End If<br> <br> m_File_Time = File_Time<br> <br> Call WalkFiles(m_DirName)<br> <br> Screen.MousePointer = vbDefault<br> MsgBox "Done"<br> <br>End Sub<br><br>Private Sub WalkFiles(Start As String)<br> <br> Static FindData As WIN32_FIND_DATA<br> Static szFilename As String<br> Dim Handle As Long<br> Dim lRC As Long<br> Dim hFiles As Long<br> Dim f As Boolean<br> Dim bDoIt As Boolean<br><br> If Right$(Start, 1) <> "/" Then<br> Start = Start & "\"<br> End If<br><br> hFiles = FindFirstFile(Start & "*.*", FindData)<br> f = (hFiles <> INVALID_HANDLE_VALUE)<br> <br> Do While f<br> szFilename = ByteZToStr(FindData.cFileName)<br> <br> If szFilename = "." Or szFilename = ".." Then<br> ' Don't do pseudo-directory entries for current and parent dirs<br> <br> Else<br> If FindData.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY Then<br> If m_Recurse Then<br> Call WalkFiles(Start & szFilename)<br> End If<br> ElseIf FindData.dwFileAttributes And FILE_ATTRIBUTE_READONLY Then<br> 'don't modify read-only files<br> <br> Else<br> bDoIt = True<br> If FindData.dwFileAttributes And FILE_ATTRIBUTE_HIDDEN Then<br> If Not m_DoHidden Then<br> bDoIt = False<br> End If<br> End If<br> <br> If bDoIt Then<br> 'Open the file to get a handle to it<br> Handle = CreateFile(Start & szFilename, _<br> GENERIC_WRITE, _<br> FILE_SHARE_READ Or FILE_SHARE_WRITE, _<br> ByVal 0&, _<br> OPEN_EXISTING, _<br> 0, _<br> 0)<br> <br> 'Now change the LastWrittenTo part of the file info<br> lRC = SetFileTime(Handle, 0&, 0&, m_File_Time)<br> If lRC = 0 Then<br> MsgBox "Unable to set date/time for file: " & szFilename<br> End If<br> <br> 'Close the filehandle<br> Call CloseHandle(Handle)<br> End If<br> End If<br> End If<br> f = FindNextFile(hFiles, FindData)<br> Loop<br> <br> f = FindClose(hFiles)<br> <br>End Sub<br><br>Private Function ByteZToStr(NullTermString) As String<br><br> Dim szTemp As String<br> <br> szTemp = Left$(NullTermString, InStr(NullTermString, Chr(0)) - 1)<br> ByteZToStr = szTemp<br> <br>End Function<br></font><br><br>Chip H.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.