home *** CD-ROM | disk | FTP | other *** search
Visual Basic class definition | 2005-03-08 | 14.7 KB | 415 lines |
- VERSION 1.0 CLASS
- BEGIN
- MultiUse = -1 'True
- Persistable = 0 'NotPersistable
- DataBindingBehavior = 0 'vbNone
- DataSourceBehavior = 0 'vbNone
- MTSTransactionMode = 0 'NotAnMTSObject
- END
- Attribute VB_Name = "clsFileUtilities"
- Attribute VB_GlobalNameSpace = False
- Attribute VB_Creatable = True
- Attribute VB_PredeclaredId = False
- Attribute VB_Exposed = False
- '//************************************
- '// Author: Morgan Haueisen
- '// morganh@hartcom.net
- '// Copyright (c) 1996-2004
- '//************************************
- 'Legal:
- ' This is intended for and was uploaded to www.planetsourcecode.com
- '
- ' Redistribution of this code, whole or in part, as source code or in binary form, alone or
- ' as part of a larger distribution or product, is forbidden for any commercial or for-profit
- ' use without the author's explicit written permission.
- '
- ' Redistribution of this code, as source code or in binary form, with or without
- ' modification, is permitted provided that the following conditions are met:
- '
- ' Redistributions of source code must include this list of conditions, and the following
- ' acknowledgment:
- '
- ' This code was developed by Morgan Haueisen. <morganh@hartcom.net>
- ' Source code, written in Visual Basic, is freely available for non-commercial,
- ' non-profit use at www.planetsourcecode.com.
- '
- ' Redistributions in binary form, as part of a larger project, must include the above
- ' acknowledgment in the end-user documentation. Alternatively, the above acknowledgment
- ' may appear in the software itself, if and wherever such third-party acknowledgments
- ' normally appear.
- '
- '
- '// Examples:
- '// VBGetOpenFileName(sFile, , True, , , , "All Icon Files
- ' (*.EXE;*.DLL;*.ICO)|*.EXE;*.DLL;*.ICO|Icon Files
- ' (*.ICO)|*.ICO|Executables (*.EXE;*.DLL)|*.EXE;*.DLL|All Files (*.*)|*.*", 1, , , "ICO", Me.hWnd)
-
- '// VBGetSaveFileName(sFile, , , "ICO Files (*.ICO)|*.ICO|All Files (*.*)|*.*", , , , "ICO",
- ' Me.hWnd)
-
- Option Explicit
-
- Private Type SHFILEOPSTRUCT
- HWND As Long
- wFunc As Long
- pFrom As String
- pTo As String
- fFlags As Integer
- fAnyOperationsAborted As Boolean
- hNameMappings As Long
- lpszProgressTitle As String '// only used if FOF_SIMPLEPROGRESS
- End Type
-
- Private Declare Function SHFileOperation Lib "shell32.dll" _
- Alias "SHFileOperationA" ( _
- ByRef lpFileOp As SHFILEOPSTRUCT) As Long
-
- Private Const FO_MOVE As Long = &H1
- Private Const FO_COPY As Long = &H2
- Private Const FO_DELETE As Long = &H3
- Private Const FO_RENAME As Long = &H4
-
- Private Const FOF_MULTIDESTFILES As Long = &H1
- Private Const FOF_SILENT As Long = &H4 '// don't create progress/report
- Private Const FOF_RENAMEONCOLLISION As Long = &H8
- Private Const FOF_NOCONFIRMATION As Long = &H10 '// Don't prompt the user.
- Private Const FOF_WANTMAPPINGHANDLE As Long = &H20 '// Fill in SHFILEOPSTRUCT.hNameMappings
- Private Const FOF_ALLOWUNDO As Long = &H40
- Private Const FOF_FILESONLY As Long = &H80 '// on *.*, do only files
- Private Const FOF_SIMPLEPROGRESS As Long = &H100 '// means don't show names of Files
- Private Const FOF_NOCONFIRMMKDIR As Long = &H200 '// don't confirm making any needed dirs
-
- '// This API returns The Attrbiutes of the File
- Private Declare Function GetFileAttributes Lib "kernel32.dll" _
- Alias "GetFileAttributesA" ( _
- ByVal lpFileName As String) As Long
-
- Private Const C_lngMAX_PATH As Long = 260&
- Private Const C_lngMAX_FILE As Long = 260&
-
- Private Type OPENFILENAME
- lStructSize As Long '// Filled with UDT size
- hWndOwner As Long '// Tied to Owner
- hInstance As Long '// Ignored (used only by templates)
- lpstrFilter As String '// Tied to Filter
- lpstrCustomFilter As String '// Ignored (exercise for reader)
- nMaxCustFilter As Long '// Ignored (exercise for reader)
- nFilterIndex As Long '// Tied to FilterIndex
- lpstrFile As String '// Tied to FileName
- nMaxFile As Long '// Handled internally
- lpstrFileTitle As String '// Tied to FileTitle
- nMaxFileTitle As Long '// Handled internally
- lpstrInitialDir As String '// Tied to InitDir
- lpstrTitle As String '// Tied to DlgTitle
- Flags As Long '// Tied to Flags
- nFileOffset As Integer '// Ignored (exercise for reader)
- nFileExtension As Integer '// Ignored (exercise for reader)
- lpstrDefExt As String '// Tied to DefaultExt
- lCustData As Long '// Ignored (needed for hooks)
- lpfnHook As Long '// Ignored (good luck with hooks)
- lpTemplateName As Long '// Ignored (good luck with templates)
- End Type
-
- Private Declare Function CommDlgExtendedError Lib "COMDLG32.DLL" () As Long
- Private Declare Function GetOpenFileName Lib "COMDLG32.DLL" _
- Alias "GetOpenFileNameA" ( _
- ByRef pOpenfilename As OPENFILENAME) As Long
- Private Declare Function GetSaveFileName Lib "COMDLG32.DLL" _
- Alias "GetSaveFileNameA" ( _
- ByRef pOpenfilename As OPENFILENAME) As Long
- Private Declare Function lstrlen Lib "kernel32.dll" _
- Alias "lstrlenA" ( _
- ByVal lpString As String) As Long
-
- Public Enum enuOpenFile
- OFN_ReadOnly = &H1
- OFN_OverWritePrompt = &H2
- OFN_HideReadOnly = &H4
- OFN_NOCHANGEDIR = &H8
- OFN_SHOWHELP = &H10
- OFN_ENABLEHOOK = &H20
- OFN_ENABLETEMPLATE = &H40
- OFN_ENABLETEMPLATEHANDLE = &H80
- OFN_NOVALIDATE = &H100
- OFN_ALLOWMULTISELECT = &H200
- OFN_EXTENSIONDIFFERENT = &H400
- OFN_PATHMUSTEXIST = &H800
- OFN_FileMustExist = &H1000
- OFN_CREATEPROMPT = &H2000
- OFN_SHAREAWARE = &H4000
- OFN_NOREADONLYRETURN = &H8000
- OFN_NOTESTFILECREATE = &H10000
- OFN_NONETWORKBUTTON = &H20000
- OFN_NOLONGNAMES = &H40000
- OFN_EXPLORER = &H80000
- OFN_NODEREFERENCELINKS = &H100000
- OFN_LONGNAMES = &H200000
- End Enum
-
- Private Type BrowseInfo
- hWndOwner As Long
- pIDLRoot As Long
- pszDisplayName As Long
- lpszTitle As Long
- ulFlags As Long
- lpfnCallback As Long
- lParam As Long
- iImage As Long
- End Type
-
- Private Declare Function lstrcat Lib "kernel32.dll" _
- Alias "lstrcatA" ( _
- ByVal lpString1 As String, _
- ByVal lpString2 As String) As Long
- Private Declare Function SHBrowseForFolder Lib "shell32.dll" ( _
- ByRef lpbi As BrowseInfo) As Long
- Private Declare Function SHGetPathFromIDList Lib "shell32" ( _
- ByVal pidList As Long, _
- ByVal lpBuffer As String) As Long
-
- Private Const BIF_STATUSTEXT As Long = &H4
- Private Const BIF_RETURNONLYFSDIRS As Long = &H1
- Private Const BIF_DONTGOBELOWDOMAIN As Long = &H2
- Private Const BIF_NEWDIALOGSTYLE As Long = &H40
- Private Const BIF_USENEWUI As Long = BIF_NEWDIALOGSTYLE
-
- Private Declare Function GetShortPathName Lib "kernel32.dll" _
- Alias "GetShortPathNameA" ( _
- ByVal lpszLongPath As String, _
- ByVal lpszShortPath As String, _
- ByVal cchBuffer As Long) As Long
-
- '// Required for changing a File's Date & Time
-
- Private Type FILETIME
- dwLowDateTime As Long
- dwHighDateTime As Long
- End Type
-
- Private 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
-
- Private Const GENERIC_WRITE As Long = &H40000000
- Private Const OPEN_EXISTING As Long = 3
- Private Const FILE_SHARE_READ As Long = &H1
- Private Const FILE_SHARE_WRITE As Long = &H2
-
- Private Declare Function CreateFile Lib "kernel32" _
- Alias "CreateFileA" ( _
- ByVal lpFileName As String, _
- ByVal dwDesiredAccess As Long, _
- ByVal dwShareMode As Long, _
- ByVal lpSecurityAttributes As Long, _
- ByVal dwCreationDisposition As Long, _
- ByVal dwFlagsAndAttributes As Long, _
- ByVal hTemplateFile As Long) As Long
- Private Declare Function SetFileTime Lib "kernel32" ( _
- ByVal hFile As Long, _
- ByRef lpCreationTime As FILETIME, _
- ByRef lpLastAccessTime As FILETIME, _
- ByRef lpLastWriteTime As FILETIME) As Long
- Private Declare Function SystemTimeToFileTime Lib "kernel32" ( _
- ByRef lpSystemTime As SYSTEMTIME, _
- ByRef lpFileTime As FILETIME) As Long
- Private Declare Function CloseHandle Lib "kernel32" ( _
- ByVal hObject As Long) As Long
- Private Declare Function LocalFileTimeToFileTime Lib "kernel32" ( _
- ByRef lpLocalFileTime As FILETIME, _
- ByRef lpFileTime As FILETIME) As Long
-
- '// CompactPath
- Private Declare Function PathCompactPath Lib "shlwapi.dll" _
- Alias "PathCompactPathA" ( _
- ByVal hDC As Long, _
- ByVal pszPath As String, _
- ByVal dx As Long) As Long
-
- '// create new folder
- Private 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 * C_lngMAX_PATH
- cAlternate As String * 14
- End Type
-
- Private Type SECURITY_ATTRIBUTES
- nLength As Long
- lpSecurityDescriptor As Long
- bInheritHandle As Long
- End Type
-
- Private Declare Function CreateDirectory Lib "kernel32" _
- Alias "CreateDirectoryA" ( _
- ByVal lpPathName As String, _
- ByRef lpSecurityAttributes As SECURITY_ATTRIBUTES) As Long
- 'Private Declare Function FindNextFile Lib "kernel32" _
- Alias "FindNextFileA" ( _
- ByVal hFindFile As Long, _
- ByRef lpFindFileData As WIN32_FIND_DATA) As Long
- Private Declare Function FindFirstFile Lib "kernel32" _
- Alias "FindFirstFileA" ( _
- ByVal lpFileName As String, _
- ByRef lpFindFileData As WIN32_FIND_DATA) As Long
-
- Private mlngApiReturn As Long
- Private mlngExtendedError As Long
- Private mlngNoConfirmation As Long
-
- Private Sub Class_Initialize()
-
- mlngNoConfirmation = FOF_NOCONFIRMATION
-
- End Sub
-
- Public Function CompactPath(ByVal vstrFullPath As String, _
- ByVal vlngWidth As Long, _
- Optional ByVal hDC As Long = 0&) As String
-
- Dim lngZeroPos As Long
-
- On Error GoTo Err_Proc
-
- '// Compact
- Call PathCompactPath(hDC, vstrFullPath, vlngWidth)
-
- '// Remove all trailing Chr$(0)'s
- lngZeroPos = InStr(1, vstrFullPath, vbNullChar)
-
- If lngZeroPos > 0 Then
- CompactPath = Left$(vstrFullPath, lngZeroPos - 1)
- Else
- CompactPath = vstrFullPath
- End If
-
- Exit_Proc:
- Exit Function
-
- Err_Proc:
- Err_Handler True, Err.Number, Err.Description, "CompactPath"
- Err.Clear
- Resume Exit_Proc
-
- End Function
-
- Public Function CopyDir(ByVal vstrSPath As String, _
- ByVal vstrDPath As String, _
- Optional ByVal vblnNoShow As Boolean = False, _
- Optional ByVal vblnNoConformation As Boolean = False) As Boolean
-
- Dim lngTemp As Long
-
- On Error GoTo Err_Proc
- lngTemp = mlngNoConfirmation
-
- If vblnNoConformation Then
- mlngNoConfirmation = FOF_NOCONFIRMATION
- Else
- mlngNoConfirmation = 0
- End If
-
- If Right$(vstrSPath, 1) = "\" Then vstrSPath = Left$(vstrSPath, Len(vstrSPath) - 1)
- If Right$(vstrDPath, 1) = "\" Then vstrDPath = Left$(vstrDPath, Len(vstrDPath) - 1)
-
- Call CreateDir(vstrDPath)
-
- If LCase$(Me.RetOnlyPath(vstrSPath, False)) = LCase$(vstrDPath) Then
- CopyDir = False
- Else
- CopyDir = CopyMoveSingle(vstrSPath, vstrDPath, FO_COPY, False, vblnNoShow)
- End If
-
- mlngNoConfirmation = lngTemp
-
- Exit_Proc:
- Exit Function
-
- Err_Proc:
- Err_Handler True, Err.Number, Err.Description, "CopyDir"
- Err.Clear
- Resume Exit_Proc
-
- End Function
-
- Public Function CopyFile(ByVal vstrSPathFile As String, _
- ByVal vstrDPathFile As String, _
- Optional ByVal vblnOnlyNewer As Boolean = False, _
- Optional ByVal vblnNoShow As Boolean = True) As Boolean
-
- On Error GoTo Err_Proc
-
- If LCase$(vstrSPathFile) = LCase$(vstrDPathFile) Then
- CopyFile = False
- Else
- CopyFile = CopyMoveSingle(vstrSPathFile, vstrDPathFile, FO_COPY, vblnOnlyNewer, vblnNoShow)
- End If
-
- Exit_Proc:
- Exit Function
-
- Err_Proc:
- Err_Handler True, Err.Number, Err.Description, "CopyFile"
- Err.Clear
- Resume Exit_Proc
-
- End Function
-
- Private Function CopyMoveMulti(ByVal vstrSPath As String, _
- ByVal vstrDPath As String, _
- ByRef vntFileName() As String, _
- ByVal vlngAction As Long, _
- Optional ByVal vblnOnlyNewer As Boolean = False, _
- Optional ByVal vblnNoShow As Boolean = False) As Boolean
-
- Dim udtOptions As SHFILEOPSTRUCT
- Dim lngI As Long
- Dim strSFileNames As String
- Dim strDFileNames As String
-
- On Local Error GoTo Function_Error
-
- If Right$(vstrSPath, 1) <> "\" Then vstrSPath = vstrSPath & "\"
- If Right$(vstrDPath, 1) <> "\" Then vstrDPath = vstrDPath & "\"
-
- For lngI = LBound(vntFileName) To UBound(vntFileName)
-
- If LenB(vntFileName(lngI)) Then
- If vblnOnlyNewer Then
- If NotTheSame(vstrSPath & vntFileName(lngI), vstrDPath & vntFileName(lngI)) Then
- strSFileNames = strSFileNames & vstrSPath & vntFileName(lngI) & vbNullChar
- End If
-
- Else
- strSFileNames = strSFileNames & vstrSPath & vntFileName(lngI) & vbNullChar
- End If
-
- End If
- Next lngI
-
- If LenB(strSFileNames) Then
-
- strSFileNames = strSFileNames & vbNullChar
- strDFileNames = Left$(vstrDPath, Len(vstrDPath) - 1) & vbNullChar
-
- With udtOptions
- .HWND = 0
- .wFunc = vlngAction
- .pFrom = strSFileNames
- .pTo = strDFileNames
- .fFlags = FOF_ALLOWUNDO
- If vblnNoShow Then .fFlags = .fFlags Or FOF_SILENT
- If vblnOnlyNewer Then
- .fFlags = .fFlags Or FOF_ If NotTheSalags blnOnlyNewlf Ng) & vbNuh Optional ByValCir_ProcklChenf Ng) & vbNuh