📄 directory.cls
字号:
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "Directory"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
' CopyRight (c) 2005 Kelly Ethridge
'
' This file is part of VBCorLib.
'
' VBCorLib is free software; you can redistribute it and/or modify
' it under the terms of the GNU Library General Public License as published by
' the Free Software Foundation; either version 2.1 of the License, or
' (at your option) any later version.
'
' VBCorLib is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' GNU Library General Public License for more details.
'
' You should have received a copy of the GNU Library General Public License
' along with Foobar; if not, write to the Free Software
' Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
'
' Module: Directory
'
''
' Provides a set of static methods for manipulating and retrieving directory information.
'
Option Explicit
Private Const ALT_DIRECTORY_SEPARATOR_CHAR As Integer = 47
Private Const DIRECTORY_SEPARATOR_CHAR As Integer = 92
Private Const FILE_FLAG_BACKUP_SEMANTICS As Long = &H2000000
Private Const ERROR_NO_MORE_FILES As Long = 18&
Private Enum SystemEntryTypes
FileEntry = 1
DirectoryEntry = 2
End Enum
' We want to use the variable name Path in the arguments like DotNet,
' so we will keep a reference to the one Path object for each access
' without having to type out the mStaticClasses portion.
Private mPath As Path
''
' Creates a directory and any subdirectories in the specified path.
'
' @param Path The directory path to be created.
' @return A DirectoryInfo object that represents the created directory.
' @remarks The path to be created can be a relative or absolute path.
'
Public Function CreateDirectory(ByVal Path As String) As DirectoryInfo
Path = Trim$(Path)
Call mPath.FixupPath(Path)
If Len(Path) = 0 Then _
Throw Cor.NewArgumentException(Environment.GetResourceString(Argument_EmptyPath), "Path")
Path = cString.TrimEnd(Path, mPath.DirectorySeparatorString)
If Len(Path) > mPath.MaxPathLength Then _
Throw New PathTooLongException
If InStr(3, Path, mPath.VolumeSeparatorString) > 0 Then _
Throw New NotSupportedException
Dim DirectoryNames() As String
DirectoryNames = Split(Path, mPath.DirectorySeparatorString)
Dim i As Long
i = cArray.GetLength(DirectoryNames)
Do
Dim TestDirectory As String
TestDirectory = cString.Join(DirectoryNames, mPath.DirectorySeparatorString, 0, i)
If Directory.Exists(TestDirectory) Then Exit Do
i = i - 1
Loop While i > 0
Do While i <= UBound(DirectoryNames)
i = i + 1
Dim NewDirectory As String
NewDirectory = cString.Join(DirectoryNames, mPath.DirectorySeparatorString, 0, i)
If API.CreateDirectory(NewDirectory) = BOOL_FALSE Then IOError Err.LastDllError
Loop
Set CreateDirectory = Cor.NewDirectoryInfo(Path)
End Function
''
' Checks if the path exists.
'
' @param Path The path to check for existence.
' @return Indication of the existence of the path.
' @remarks The path can be relative or absolute.
'
Public Function Exists(ByVal Path As String) As Boolean
Dim Data As WIN32_FILE_ATTRIBUTE_DATA
If File.GetFileData(Path, Data) = NO_ERROR Then
Exists = CBool(Data.dwFileAttributes And FileAttributes.DirectoryAttr)
End If
End Function
''
' Deletes directories and files within directories.
'
' @param Path The top directory to be deleted.
' @param Recursive If set to True, then all directories and files in the top
' directory will be deleted along with the top directory. Otherwise, the specified
' directory must be empty to be deleted.
'
Public Sub Delete(ByVal Path As String, Optional ByVal Recursive As Boolean = False)
If Len(Path) = 0 Then _
Throw Cor.NewArgumentException(Environment.GetResourceString(Argument_EmptyPath))
Path = cString.TrimEnd(mPath.InternalGetFullPath(Path), "/\")
If Recursive Then
Dim FileHandle As Long
Dim Data As WIN32_FIND_DATA
FileHandle = API.FindFirstFile(Path & "\*", Data)
If FileHandle <> INVALID_HANDLE Then
' Simply loop through any files or directories
' within this directory and delete them.
Do
Dim FileName As String
FileName = GetFileNameFromFindData(Data)
If Len(FileName) > 0 Then
Dim FullPath As String
FullPath = mPath.Combine(Path, FileName)
If Data.dwFileAttributes And FileAttributes.DirectoryAttr Then
Call Delete(FullPath, True)
Else
If API.DeleteFile(FullPath) = BOOL_FALSE Then IOError Err.LastDllError, FullPath
End If
End If
Loop While API.FindNextFile(FileHandle, Data)
Dim Result As Long
Result = Err.LastDllError ' get this before FindClose can change it.
Call FindClose(FileHandle)
If Result <> ERROR_NO_MORE_FILES Then IOError Result, FullPath
End If
End If
If API.RemoveDirectory(Path) = BOOL_FALSE Then
If Err.LastDllError <> ERROR_PATH_NOT_FOUND Then IOError Err.LastDllError, Path
End If
End Sub
''
' Returns the time of creation for the specified directory in local time.
'
' @param Path The directory to retrieve the creation time of.
' @return A cDateTime object containing the creation time of the directory.
' @remarks The path can be relative or absolute.
'
Public Function GetCreationTime(ByVal Path As String) As cDateTime
Set GetCreationTime = File.GetCreationTime(Path)
End Function
''
' Returns the time of creation for the specified directory in UTC time.
'
' @param Path The directory to retrieve the creation time of.
' @return A cDateTime object containing the creation time of the directory.
' @remarks The path can be relative or absolute.
'
Public Function GetCreationTimeUtc(ByVal Path As String) As cDateTime
Set GetCreationTimeUtc = File.GetCreationTimeUtc(Path)
End Function
''
' Returns the time the directory was last accessed in local time.
'
' @param Path The directory to retrieve the last time it was accessed.
' @return A cDateTime object containing the time the directory was last accessed.
' @remarks The path can be relative or absolute.
'
Public Function GetLastAccessTime(ByVal Path As String) As cDateTime
Set GetLastAccessTime = File.GetLastAccessTime(Path)
End Function
''
' Returns the time the directory was last accessed in UTC time.
'
' @param Path The directory to retrieve the last time it was accessed.
' @return A cDateTime object containing the time the directory was last accessed.
' @remarks The path can be relative or absolute.
'
Public Function GetLastAccessTimeUtc(ByVal Path As Stream) As cDateTime
Set GetLastAccessTimeUtc = File.GetLastAccessTimeUtc(Path)
End Function
''
' Returns the time the directory was last written to in local time.
'
' @param Path The directory to retrieve the last time it was written to.
' @return A cDateTime object containing the last time the directory was written to.
' @remarks The path can relative or absolute.
'
Public Function GetLastWriteTime(ByVal Path As String) As cDateTime
Set GetLastWriteTime = File.GetLastWriteTime(Path)
End Function
''
' Returns the time the directory was last written to in UTC time.
'
' @param Path The directory to retrieve the last time it was written to.
' @return A cDateTime object containing the last time the directory was written to.
' @remarks The path can relative or absolute.
'
Public Function GetLastWriteTimeUtc(ByVal Path As String) As cDateTime
Set GetLastWriteTimeUtc = File.GetLastWriteTimeUtc(Path)
End Function
''
' Returns the current directory the application is set to.
'
' @return The current set directory.
'
Public Function GetCurrentDirectory() As String
Dim Ret As String
Ret = String$(Path.MaxPathLength, 0)
Dim Size As Long
Size = API.GetCurrentDirectory(Len(Ret), Ret)
If Size = 0 Then IOError Err.LastDllError
GetCurrentDirectory = Left$(Ret, Size)
End Function
''
' Returns a list of all the directories found in the specified directory
' that matches the search pattern.
'
' @param Path The directory to find the requested directories.
' @param SearchPattern A pattern to compare all the directories against, returning
' those that match the pattern.
' @return An array of the matching directories. If no directories match the pattern, then
' an empty zero-length array.
' @remarks The path can be absolute or relative.
'
Public Function GetDirectories(ByVal Path As String, Optional ByVal SearchPattern As String = "*") As String()
GetDirectories = InternalGetFiles(Path, SearchPattern, DirectoryEntry)
End Function
''
' Returns the root directory of the specified path.
'
' @param Path The path to return the root of.
' @return The root of the specified directory.
' @remarks If the path is relative, then the current directory will
' be used as the root.
'
Public Function GetDirectoryRoot(ByVal Path As String) As String
If Len(Path) = 0 Then _
Throw Cor.NewArgumentException(Environment.GetResourceString(Argument_EmptyPath), "Path")
If Not mPath.IsPathRooted(Path) Then
Path = mPath.GetFullPath(Path)
End If
GetDirectoryRoot = mPath.GetPathRoot(Path)
End Function
''
' Returns a list of all the files that match a pattern within the specified directory.
'
' @param Path The directory to search for the matching files.
' @param SearchPattern A pattern to match the files against.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -