Option Explicit
Function MakeFolder(ByVal aPath As String) As Boolean
On Error GoTo Err
Dim fso As Object: Set fso = CreateObject("Scripting.FileSystemObject")
If InStr(aPath, "\") = 0 Then aPath = CurDir & "\" & aPath
If ExistsFolder(aPath) Then GoTo Warn
Dim ary As Variant
Dim arg As Variant: arg = Split(aPath, "\")
Dim i As Long
For i = LBound(arg) To UBound(arg)
ary = arg
ReDim Preserve ary(i)
If Not fso.FolderExists(Join(ary, "\")) Then
fso.CreateFolder Join(ary, "\")
End If
Next
MakeFolder = True
If logging Then AddLog "FolderUtil.MakeFolder", "INFO ", "対象:" & aPath
GoTo Finally
Warn:
MakeFolder = True
If logging Then AddLog "FolderUtil.MakeFolder", "WARN ", "対象:" & aPath & " / 警告内容:すでに存在しています"
GoTo Finally
Err:
If logging Then AddLog "FolderUtil.MakeFolder", "ERROR", "対象:" & aPath & " / エラー内容:" & Err.Description
Finally:
Set fso = Nothing
End Function
Public Function DeleteFolder(ByVal aPath As String, Optional aForce As Boolean) As Boolean
On Error GoTo Err
Dim fso As Object: Set fso = CreateObject("Scripting.FileSystemObject")
If InStr(aPath, "\") = 0 Then aPath = CurDir & "\" & aPath
If Not ExistsFolder(aPath) Then GoTo Warn
fso.DeleteFolder aPath, aForce
DeleteFolder = True
If logging Then AddLog "FolderUtil.DeleteFolder", "INFO ", "対象:" & aPath & " / 読み取り専用削除:" & aForce
GoTo Finally
Warn:
DeleteFolder = True
If logging Then AddLog "FolderUtil.DeleteFolder", "WARN ", "対象:" & aPath & " / 警告内容:対象が存在しません"
GoTo Finally
Err:
If logging Then AddLog "FolderUtil.DeleteFolder", "ERROR", "対象:" & aPath & " / 読み取り専用削除:" & aForce & " / エラー内容:" & Err.Description
Finally:
Set fso = Nothing
End Function
Public Function CopyFolder(ByVal aOrigin As String, ByVal aDestination As String, Optional aIsOverWrite As Boolean) As Boolean
On Error GoTo Err
Dim fso As Object: Set fso = CreateObject("Scripting.FileSystemObject")
If InStr(aOrigin, "\") = 0 Then aOrigin = CurDir & "\" & aOrigin
If InStr(aDestination, "\") = 0 Then aDestination = CurDir & "\" & aDestination
fso.CopyFolder aOrigin, aDestination, aIsOverWrite
CopyFolder = True
If logging Then AddLog "FolderUtil.CopyFolder", "INFO ", "コピー元:" & aOrigin & " / コピー先:" & aDestination & " / 上書き:" & aIsOverWrite
GoTo Finally
Err:
If logging Then AddLog "FolderUtil.CopyFolder", "ERROR", "コピー元:" & aOrigin & " / コピー先:" & aDestination & " / 上書き:" & aIsOverWrite & " / エラー内容:" & Err.Description
Finally:
Set fso = Nothing
End Function
Public Function MoveFolder(ByVal aSource As String, ByVal aDestination As String) As Boolean
On Error GoTo Err
Dim fso As Object: Set fso = CreateObject("Scripting.FileSystemObject")
If InStr(aSource, "\") = 0 Then aSource = CurDir & "\" & aSource
If Not Right(aDestination, 1) = "\" Then aDestination = aDestination & "\"
fso.MoveFolder aSource, aDestination
MoveFolder = True
If logging Then AddLog "FolderUtil.MoveFolder", "INFO ", "移動元:" & aSource & " / 移動先:" & aDestination
GoTo Finally
Err:
If logging Then AddLog "FolderUtil.MoveFolder", "ERROR", "移動元:" & aSource & " / 移動先:" & aDestination & " / エラー内容:" & Err.Description
Finally:
Set fso = Nothing
End Function
Public Function ChangeFolderName(ByVal aPath As String, aFolderName As String) As Boolean
On Error GoTo Err
Dim fso As Object: Set fso = CreateObject("Scripting.FileSystemObject")
If InStr(aPath, "\") = 0 Then aPath = CurDir & "\" & aPath
fso.GetFolder(aPath).Name = aFolderName
ChangeFolderName = True
If logging Then AddLog "FolderUtil.ChangeFolderName", "INFO ", "対象:" & aPath & " / フォルダ名:" & aFolderName
GoTo Finally
Err:
If logging Then AddLog "FolderUtil.ChangeFolderName", "ERROR", "対象:" & aPath & " / フォルダ名:" & aFolderName & " / エラー内容:" & Err.Description
Finally:
Set fso = Nothing
End Function
Public Function GetFolderList(Optional ByVal aPath As String, Optional aLikeValue As String, Optional aIsFullPath As Boolean) As Collection
Dim fso As Object: Set fso = CreateObject("Scripting.FileSystemObject")
Dim f As Variant
Set GetFolderList = New Collection
If aPath = "" Then aPath = CurDir
For Each f In fso.GetFolder(aPath).subFolders
If aLikeValue = "" Or LCase(f.Name) Like aLikeValue Then
If aIsFullPath Then GetFolderList.Add f.Path
If Not aIsFullPath Then GetFolderList.Add f.Name
End If
Next
End Function
Public Function ExistsFolder(aPath As String) As Boolean
If (Dir(aPath, vbDirectory) <> "") Then ExistsFolder = True
End Function