home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / CODE_UPLOAD88928122000.psc / CFileInfo.cls < prev    next >
Encoding:
Visual Basic class definition  |  2000-05-17  |  16.9 KB  |  536 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "CFileInfo"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = False
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. ' *********************************************************************
  15. '  Copyright (C)1995-98 Karl E. Peterson, All Rights Reserved
  16. '  http://www.mvps.org/vb
  17. ' *********************************************************************
  18. '  Warning: This computer program is protected by copyright law and
  19. '  international treaties. Unauthorized reproduction or distribution
  20. '  of this program, or any portion of it, may result in severe civil
  21. '  and criminal penalties, and will be prosecuted to the maximum
  22. '  extent possible under the law.
  23. ' *********************************************************************
  24. Option Explicit
  25. '
  26. ' API declarations
  27. '
  28. Private Declare Function GetFullPathName Lib "kernel32" Alias "GetFullPathNameA" (ByVal lpFileName As String, ByVal nBufferLength As Long, ByVal lpBuffer As String, lpFilePart As Long) As Long
  29. Private Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal nBufferLength As Long) As Long
  30. Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
  31. Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
  32. Private Declare Function FileTimeToLocalFileTime Lib "kernel32" (lpFileTime As FILETIME, lpLocalFileTime As FILETIME) As Long
  33. Private Declare Function FileTimeToSystemTime Lib "kernel32" (lpFileTime As FILETIME, lpSystemTime As SYSTEMTIME) As Long
  34. Private Declare Function GetCompressedFileSize Lib "kernel32" Alias "GetCompressedFileSizeA" (ByVal lpFileName As String, lpFileSizeHigh As Long) As Long
  35. Private Declare Function SHGetFileInfo Lib "shell32.dll" Alias "SHGetFileInfoA" (ByVal pszPath As String, ByVal dwFileAttributes As Long, psfi As SHFILEINFO, ByVal cbFileInfo As Long, ByVal uFlags As Long) As Long
  36. '
  37. ' API constants.
  38. '
  39. Private Const MAX_PATH = 260
  40. Private Const INVALID_HANDLE_VALUE = -1
  41. '
  42. ' File attribute constants.
  43. '
  44. Private Const FILE_ATTRIBUTE_READONLY = &H1
  45. Private Const FILE_ATTRIBUTE_HIDDEN = &H2
  46. Private Const FILE_ATTRIBUTE_SYSTEM = &H4
  47. Private Const FILE_ATTRIBUTE_DIRECTORY = &H10
  48. Private Const FILE_ATTRIBUTE_ARCHIVE = &H20
  49. Private Const FILE_ATTRIBUTE_NORMAL = &H80
  50. Private Const FILE_ATTRIBUTE_TEMPORARY = &H100
  51. Private Const FILE_ATTRIBUTE_COMPRESSED = &H800
  52. '
  53. ' SHGetFileInfo constants.
  54. '
  55. Private Const SHGFI_ICON = &H100                         '  get icon
  56. Private Const SHGFI_DISPLAYNAME = &H200                  '  get display name
  57. Private Const SHGFI_TYPENAME = &H400                     '  get type name
  58. Private Const SHGFI_ATTRIBUTES = &H800                   '  get attributes
  59. Private Const SHGFI_ICONLOCATION = &H1000                '  get icon location
  60. Private Const SHGFI_EXETYPE = &H2000                     '  return exe type
  61. Private Const SHGFI_SYSICONINDEX = &H4000                '  get system icon index
  62. Private Const SHGFI_LINKOVERLAY = &H8000                 '  put a link overlay on icon
  63. Private Const SHGFI_SELECTED = &H10000                   '  show icon in selected state
  64. Private Const SHGFI_LARGEICON = &H0                      '  get large icon
  65. Private Const SHGFI_SMALLICON = &H1                      '  get small icon
  66. Private Const SHGFI_OPENICON = &H2                       '  get open icon
  67. Private Const SHGFI_SHELLICONSIZE = &H4                  '  get shell size icon
  68. Private Const SHGFI_PIDL = &H8                           '  pszPath is a pidl
  69. Private Const SHGFI_USEFILEATTRIBUTES = &H10             '  use passed dwFileAttribute
  70. '
  71. ' API structures.
  72. '
  73. Private Type FILETIME
  74.    dwLowDateTime As Long
  75.    dwHighDateTime As Long
  76. End Type
  77.  
  78. Private Type WIN32_FIND_DATA
  79.    dwFileAttributes As Long
  80.    ftCreationTime As FILETIME
  81.    ftLastAccessTime As FILETIME
  82.    ftLastWriteTime As FILETIME
  83.    nFileSizeHigh As Long
  84.    nFileSizeLow As Long
  85.    dwReserved0 As Long
  86.    dwReserved1 As Long
  87.    cFileName As String * MAX_PATH
  88.    cAlternate As String * 14
  89. End Type
  90.  
  91. Private Type SYSTEMTIME
  92.    wYear As Integer
  93.    wMonth As Integer
  94.    wDayOfWeek As Integer
  95.    wDay As Integer
  96.    wHour As Integer
  97.    wMinute As Integer
  98.    wSecond As Integer
  99.    wMilliseconds As Integer
  100. End Type
  101.  
  102. Private Type SHFILEINFO
  103.    hIcon As Long                       '  out: icon
  104.    iIcon As Long                       '  out: icon index
  105.    dwAttributes As Long                '  out: SFGAO_ flags
  106.    szDisplayName As String * MAX_PATH  '  out: display name (or path)
  107.    szTypeName As String * 80           '  out: type name
  108. End Type
  109. '
  110. ' Member variables.
  111. '
  112. Private m_PathName As String
  113. Private m_Name As String
  114. Private m_Path As String
  115. Private m_Extension As String
  116. Private m_DisplayName As String
  117. Private m_TypeName As String
  118. Private m_hIcon As Long
  119. Private m_PathNameShort As String
  120. Private m_NameShort As String
  121. Private m_PathShort As String
  122. Private m_FileExists As Boolean
  123. Private m_PathExists As Boolean
  124. Private m_FileSize As Long
  125. Private m_FileSizeHigh As Long
  126. Private m_CompFileSize As Long
  127. Private m_CompFileSizeHigh As Long
  128. Private m_Attributes As Long
  129. Private m_tmCreation As Double
  130. Private m_tmAccess As Double
  131. Private m_tmWrite As Double
  132.  
  133. ' ********************************************
  134. '  Initialize and Terminate
  135. ' ********************************************
  136. Private Sub Class_Initialize()
  137.    '
  138.    ' All member variables can be left to defaults.
  139.    '
  140. End Sub
  141.  
  142. Private Sub Class_Terminate()
  143.    '
  144.    ' No special cleanup required.
  145.    '
  146. End Sub
  147.  
  148. ' ********************************************
  149. '  Public Properties
  150. ' ********************************************
  151. Public Property Let FullPathName(ByVal NewVal As String)
  152.    Dim Buffer As String
  153.    Dim nFilePart As Long
  154.    Dim nRet As Long
  155.    '
  156.    ' Retrieve fully qualified path/name specs.
  157.    '
  158.    Buffer = Space(MAX_PATH)
  159.    nRet = GetFullPathName(NewVal, Len(Buffer), Buffer, nFilePart)
  160.    If nRet Then
  161.       m_PathName = Left(Buffer, nRet)
  162.       Refresh
  163.    End If
  164. End Property
  165.  
  166. Public Property Get FullPathName() As String
  167.    ' Returns fully-qualified path/name spec.
  168.    FullPathName = m_PathName
  169. End Property
  170.  
  171. Public Property Get FileName() As String
  172.    ' Returns filename only.
  173.    FileName = m_Name
  174. End Property
  175.  
  176. Public Property Get FilePath() As String
  177.    ' Returns fully-qualified pathname only.
  178.    FilePath = m_Path
  179. End Property
  180.  
  181. Public Property Get FileExtension() As String
  182.    ' Returns the file's extension only.
  183.    FileExtension = m_Extension
  184. End Property
  185.  
  186. Public Property Get ShortPathName() As String
  187.    ' Returns fully-qualified *short* path/name spec.
  188.    ShortPathName = m_PathNameShort
  189. End Property
  190.  
  191. Public Property Get ShortName() As String
  192.    ' Returns *short* filename only.
  193.    ShortName = m_NameShort
  194. End Property
  195.  
  196. Public Property Get ShortPath() As String
  197.    ' Returns *short* fully-qualified pathname only.
  198.    ShortPath = m_PathShort
  199. End Property
  200.  
  201. Public Property Get DisplayName() As String
  202.    ' Returns the "display" name for the file, not necessarily
  203.    ' proper-cased, but as Explorer shows it.
  204.    DisplayName = m_DisplayName
  205. End Property
  206.  
  207. Public Property Get TypeName() As String
  208.    ' Returns the string that describes the file's type.
  209.    TypeName = m_TypeName
  210. End Property
  211.  
  212. Public Property Get FileExists() As Boolean
  213.    ' Returns whether file exists.
  214.    FileExists = m_FileExists
  215. End Property
  216.  
  217. Public Property Get PathExists() As Boolean
  218.    ' Returns whether path exists.
  219.    PathExists = m_PathExists
  220. End Property
  221.  
  222. Public Property Get FileSize() As Long
  223.    ' Return size of file.
  224.    FileSize = m_FileSize
  225. End Property
  226.  
  227. Public Property Get FileSizeHigh() As Long
  228.    ' Returns high dword of filesize to support files > 2Gb.
  229.    FileSizeHigh = m_FileSizeHigh
  230. End Property
  231.  
  232. Public Property Get CompressedFileSize() As Long
  233.    ' Return actual size of file.
  234.    CompressedFileSize = m_CompFileSize
  235. End Property
  236.  
  237. Public Property Get CompressedFileSizeHigh() As Long
  238.    ' Returns high dword of actual filesize to support files > 2Gb.
  239.    CompressedFileSizeHigh = m_CompFileSizeHigh
  240. End Property
  241.  
  242. Public Property Get CreationTime() As Double
  243.    ' Returns date/time of file creation.
  244.    CreationTime = m_tmCreation
  245. End Property
  246.  
  247. Public Property Get LastAccessTime() As Double
  248.    ' Returns date/time of last access.
  249.    LastAccessTime = m_tmAccess
  250. End Property
  251.  
  252. Public Property Get ModifyTime() As Double
  253.    ' Returns date/time of last write.
  254.    ModifyTime = m_tmWrite
  255. End Property
  256.  
  257. Public Property Get Attributes() As Long
  258.    ' Returns entire set of attribute flags.
  259.    Attributes = m_Attributes
  260. End Property
  261.  
  262. Public Property Get attrReadOnly() As Boolean
  263.    ' Returns whether file has ReadOnly attribute.
  264.    attrReadOnly = (m_Attributes And FILE_ATTRIBUTE_READONLY)
  265. End Property
  266.  
  267. Public Property Get attrHidden() As Boolean
  268.    ' Returns whether file has Hidden attribute.
  269.    attrHidden = (m_Attributes And FILE_ATTRIBUTE_HIDDEN)
  270. End Property
  271.  
  272. Public Property Get attrSystem() As Boolean
  273.    ' Returns whether file has System attribute.
  274.    attrSystem = (m_Attributes And FILE_ATTRIBUTE_SYSTEM)
  275. End Property
  276.  
  277. Public Property Get attrArchive() As Boolean
  278.    ' Returns whether file has Archive attribute.
  279.    attrArchive = (m_Attributes And FILE_ATTRIBUTE_ARCHIVE)
  280. End Property
  281.  
  282. Public Property Get attrTemporary() As Boolean
  283.    ' Returns whether file has Temporary attribute.
  284.    attrTemporary = (m_Attributes And FILE_ATTRIBUTE_TEMPORARY)
  285. End Property
  286.  
  287. Public Property Get attrCompressed() As Boolean
  288.    ' Returns whether file has Compressed attribute.
  289.    attrCompressed = (m_Attributes And FILE_ATTRIBUTE_COMPRESSED)
  290. End Property
  291.  
  292. Public Property Get hIcon() As Long
  293.    ' Returns handle to display icon.
  294.    hIcon = m_hIcon
  295. End Property
  296.  
  297. ' ********************************************
  298. '  Public Methods
  299. ' ********************************************
  300. Public Sub Refresh()
  301.    Dim hSearch As Long
  302.    Dim wfd As WIN32_FIND_DATA
  303.    Dim Buffer As String
  304.    Dim nRet As Long
  305.    Dim i As Long
  306.    Dim sfi As SHFILEINFO
  307.    '
  308.    ' Check for existence of file.
  309.    '
  310.    hSearch = FindFirstFile(m_PathName, wfd)
  311.    If hSearch <> INVALID_HANDLE_VALUE Then
  312.       Call FindClose(hSearch)
  313.       '
  314.       ' Assign file data to member variables.
  315.       '
  316.       m_FileExists = True
  317.       m_PathExists = True
  318.       m_FileSize = wfd.nFileSizeLow
  319.       m_FileSizeHigh = wfd.nFileSizeHigh
  320.       m_Attributes = wfd.dwFileAttributes
  321.       m_tmCreation = FileTimeToDouble(wfd.ftCreationTime, True)
  322.       m_tmAccess = FileTimeToDouble(wfd.ftLastAccessTime, True)
  323.       m_tmWrite = FileTimeToDouble(wfd.ftLastWriteTime, True)
  324.       '
  325.       ' Assign file/path data to member variables.
  326.       '
  327.       m_Name = TrimNull(wfd.cFileName)
  328.       For i = Len(m_PathName) To 1 Step -1
  329.          If Mid(m_PathName, i, 1) = "\" Then
  330.             m_Path = ProperCasePath(Left(m_PathName, i))
  331.             If Right(m_Path, 1) <> "\" Then m_Path = m_Path & "\"
  332.             Exit For
  333.          End If
  334.       Next i
  335.       m_PathName = m_Path & m_Name
  336.       '
  337.       ' Extract extension from filename.
  338.       '
  339.       If InStr(m_Name, ".") Then
  340.          For i = Len(m_Name) To 1 Step -1
  341.             If Mid(m_Name, i, 1) = "." Then
  342.                m_Extension = Mid(m_Name, i + 1)
  343.                Exit For
  344.             End If
  345.          Next i
  346.       Else
  347.          m_Extension = ""
  348.       End If
  349.       '
  350.       ' Short name same as long, if cAlternate element empty.
  351.       '
  352.       If InStr(wfd.cAlternate, vbNullChar) = 1 Then
  353.          m_NameShort = UCase(m_Name)
  354.       Else
  355.          m_NameShort = TrimNull(wfd.cAlternate)
  356.       End If
  357.       '
  358.       ' Retrieve short path name.
  359.       '
  360.       Buffer = Space(MAX_PATH)
  361.       nRet = GetShortPathName(m_PathName, Buffer, Len(Buffer))
  362.       If nRet Then
  363.          m_PathNameShort = Left(Buffer, nRet)
  364.          m_PathShort = Left(m_PathNameShort, Len(m_PathNameShort) - Len(m_NameShort))
  365.       End If
  366.       '
  367.       ' Retrieve compressed size.
  368.       '
  369.       m_CompFileSize = GetCompressedFileSize(m_PathName, m_CompFileSizeHigh)
  370.       '
  371.       ' Get icon and descriptive text.
  372.       '
  373.       nRet = SHGetFileInfo(m_PathName, 0&, sfi, Len(sfi), _
  374.              SHGFI_ICON Or SHGFI_DISPLAYNAME Or SHGFI_TYPENAME)
  375.       m_DisplayName = TrimNull(sfi.szDisplayName)
  376.       m_TypeName = TrimNull(sfi.szTypeName)
  377.       m_hIcon = sfi.hIcon
  378.       '
  379.       ' Confirm displayable typename.
  380.       '
  381.       If Trim(m_TypeName) = "" Then
  382.          m_TypeName = Trim(UCase(m_Extension) & " File")
  383.       End If
  384.    Else
  385.       '
  386.       ' Assign applicable data to member variables.
  387.       '
  388.       m_FileExists = False
  389.    End If
  390. End Sub
  391.  
  392. Public Function FormatFileDate(ByVal dt As Double) As String
  393.    FormatFileDate = Format(dt, "long date") & " " & _
  394.                     Format(dt, "long time")
  395. End Function
  396.  
  397. Public Function FormatFileSize(ByVal Size As Long) As String
  398.    Dim sRet As String
  399.    Const KB& = 1024
  400.    Const MB& = KB * KB
  401.    ' Return size of file in kilobytes.
  402.    If Size < KB Then
  403.       sRet = Format(Size, "#,##0") & " bytes"
  404.    Else
  405.       Select Case Size \ KB
  406.          Case Is < 10
  407.             sRet = Format(Size / KB, "0.00") & " KB"
  408.          Case Is < 100
  409.             sRet = Format(Size / KB, "0.0") & " KB"
  410.          Case Is < 1000
  411.             sRet = Format(Size / KB, "0") & " KB"
  412.          Case Is < 10000
  413.             sRet = Format(Size / MB, "0.00") & " MB"
  414.          Case Is < 100000
  415.             sRet = Format(Size / MB, "0.0") & " MB"
  416.          Case Is < 1000000
  417.             sRet = Format(Size / MB, "0") & " MB"
  418.          Case Is < 10000000
  419.             sRet = Format(Size / MB / KB, "0.00") & " GB"
  420.       End Select
  421.       sRet = sRet
  422.    End If
  423.    FormatFileSize = sRet
  424. End Function
  425.  
  426. ' ********************************************
  427. '  Private Methods
  428. ' ********************************************
  429. Private Function FileTimeToDouble(ftUTC As FILETIME, Localize As Boolean) As Double
  430.    Dim ft As FILETIME
  431.    Dim st As SYSTEMTIME
  432.    Dim d As Double
  433.    Dim t As Double
  434.    '
  435.    ' Convert to local filetime, if necessary.
  436.    '
  437.    If Localize Then
  438.       Call FileTimeToLocalFileTime(ftUTC, ft)
  439.    Else
  440.       ft = ftUTC
  441.    End If
  442.    '
  443.    ' Convert to system time structure.
  444.    '
  445.    Call FileTimeToSystemTime(ft, st)
  446.    '
  447.    ' Convert to VB-style date (double).
  448.    '
  449.    FileTimeToDouble = DateSerial(st.wYear, st.wMonth, st.wDay) + _
  450.                       TimeSerial(st.wHour, st.wMinute, st.wSecond)
  451. End Function
  452.  
  453. Private Function ProperCasePath(ByVal PathIn As String) As String
  454.    Dim hSearch As Long
  455.    Dim wfd As WIN32_FIND_DATA
  456.    Dim PathOut As String
  457.    Dim i As Long
  458.    '
  459.    ' Trim trailing backslash, unless root dir.
  460.    '
  461.    If Right(PathIn, 1) = "\" Then
  462.       If Right(PathIn, 2) <> ":\" Then
  463.          PathIn = Left(PathIn, Len(PathIn) - 1)
  464.       Else
  465.          ProperCasePath = UCase(PathIn)
  466.          Exit Function
  467.       End If
  468.    End If
  469.    '
  470.    ' Check for UNC share and return just that,
  471.    ' if that's all that's left of PathIn.
  472.    '
  473.    If InStr(PathIn, "\\") = 1 Then
  474.       i = InStr(3, PathIn, "\")
  475.       If i > 0 Then
  476.          If InStr(i + 1, PathIn, "\") = 0 Then
  477.             ProperCasePath = PathIn
  478.             Exit Function
  479.          End If
  480.       End If
  481.    End If
  482.    '
  483.    ' Insure that path portion of string uses the
  484.    ' same case as the real pathname.
  485.    '
  486.    If InStr(PathIn, "\") Then
  487.       For i = Len(PathIn) To 1 Step -1
  488.          If Mid(PathIn, i, 1) = "\" Then
  489.             '
  490.             ' Found end of previous directory.
  491.             ' Recurse back up into path.
  492.             '
  493.             PathOut = ProperCasePath(Left(PathIn, i - 1)) & "\"
  494.             '
  495.             ' Use FFF to proper-case current directory.
  496.             '
  497.             hSearch = FindFirstFile(PathIn, wfd)
  498.             If hSearch <> INVALID_HANDLE_VALUE Then
  499.                Call FindClose(hSearch)
  500.                If wfd.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY Then
  501.                   ProperCasePath = PathOut & TrimNull(wfd.cFileName)
  502.                End If
  503.             End If
  504.             '
  505.             ' Bail out of loop.
  506.             '
  507.             Exit For
  508.          End If
  509.       Next i
  510.    Else
  511.       '
  512.       ' Just a drive letter and colon,
  513.       ' upper-case and return.
  514.       '
  515.       ProperCasePath = UCase(PathIn)
  516.    End If
  517. End Function
  518.  
  519. Private Function TrimNull(ByVal StrIn As String) As String
  520.    Dim nul As Long
  521.    '
  522.    ' Truncate input string at first null.
  523.    ' If no nulls, perform ordinary Trim.
  524.    '
  525.    nul = InStr(StrIn, vbNullChar)
  526.    Select Case nul
  527.       Case Is > 1
  528.          TrimNull = Left(StrIn, nul - 1)
  529.       Case 1
  530.          TrimNull = ""
  531.       Case 0
  532.          TrimNull = Trim(StrIn)
  533.    End Select
  534. End Function
  535.  
  536.