VCS_Dir.bas 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. Option Compare Database
  2. Option Private Module
  3. Option Explicit
  4. Private Declare Function PathIsRelative Lib "Shlwapi" _
  5. Alias "PathIsRelativeA" (ByVal Path As String) As Long
  6. Public Enum EMakeDirStatus
  7. ErrSuccess = 0
  8. ErrRelativePath
  9. ErrInvalidPathSpecification
  10. ErrDirectoryCreateError
  11. ErrSpecIsFileName
  12. ErrInvalidCharactersInPath
  13. End Enum
  14. Const MAX_PATH = 260
  15. ' Path/Directory of the current database file.
  16. Public Function ProjectPath() As String
  17. ProjectPath = CurrentProject.Path
  18. If Right$(ProjectPath, 1) <> "\" Then ProjectPath = ProjectPath & "\"
  19. End Function
  20. ' Path/Directory for source files
  21. Public Function SourcePath() As String
  22. SourcePath = ProjectPath & CurrentProject.name & ".src\"
  23. End Function
  24. ' Create folder `Path`. Silently do nothing if it already exists.
  25. Public Sub MkDirIfNotExist(ByVal Path As String)
  26. On Error GoTo MkDirIfNotexist_noop
  27. MkDir Path
  28. logger "MkDirIfNotExist", "INFO", "New dir created: " & Path
  29. MkDirIfNotexist_noop:
  30. On Error GoTo 0
  31. End Sub
  32. ' Delete a file if it exists.
  33. Public Sub DelIfExist(ByVal Path As String)
  34. On Error GoTo DelIfNotExist_Noop
  35. Kill Path
  36. logger "DelIfExist", "DEBUG", "Killed: " & Path
  37. DelIfNotExist_Noop:
  38. On Error GoTo 0
  39. End Sub
  40. ' Erase all *.`ext` files in `Path`.
  41. Public Sub ClearTextFilesFromDir(ByVal Path As String, ByVal Ext As String, Optional ByVal force As Boolean = False)
  42. '### 13/10/2016: add optimizer
  43. ' we don't want to clear the text files of the objects which will not be exported
  44. 'BUT we still want to clear obsolete files: see CleanDirs in optimizer
  45. If optimizer_activated() And Not force Then
  46. logger "ClearTextFilesFromDir", "INFO", "Optimizer on: sub aborted"
  47. Exit Sub
  48. End If
  49. '###
  50. logger "ClearTextFilesFromDir", "DEBUG", "Clear dir: " & Path & "*." & Ext
  51. Dim FSO As Object
  52. Set FSO = CreateObject("Scripting.FileSystemObject")
  53. If Not FSO.FolderExists(Path) Then Exit Sub
  54. On Error GoTo ClearTextFilesFromDir_noop
  55. If dir$(Path & "*." & Ext) <> vbNullString Then
  56. FSO.DeleteFile Path & "*." & Ext
  57. End If
  58. ClearTextFilesFromDir_noop:
  59. On Error GoTo 0
  60. End Sub
  61. Public Function DirExists(ByVal strPath As String) As Boolean
  62. On Error Resume Next
  63. DirExists = False
  64. DirExists = ((GetAttr(strPath) And vbDirectory) = vbDirectory)
  65. End Function
  66. Public Function FileExists(ByVal strPath As String) As Boolean
  67. On Error Resume Next
  68. FileExists = False
  69. FileExists = ((GetAttr(strPath) And vbDirectory) <> vbDirectory)
  70. End Function
  71. Function RecursiveMkDir(ByVal PathSpec As String) As EMakeDirStatus
  72. ' This function creates a series of nested directories. The parent of
  73. ' every directory is create before a subdirectory is created, allowing a
  74. ' folder path specification of any number of directories (as long as the
  75. ' total length is less than MAX_PATH.
  76. Dim FSO As Scripting.FileSystemObject
  77. Dim DD As Scripting.Drive
  78. Dim B As Boolean
  79. Dim Root As String
  80. Dim DirSpec As String
  81. Dim N As Long
  82. Dim M As Long
  83. Dim S As String
  84. Dim Directories() As String
  85. Set FSO = New Scripting.FileSystemObject
  86. ' ensure there are no invalid characters in spec.
  87. On Error Resume Next
  88. err.Clear
  89. S = dir(PathSpec, vbNormal)
  90. If err.Number <> 0 Then
  91. RecursiveMkDir = ErrInvalidCharactersInPath
  92. Exit Function
  93. End If
  94. On Error GoTo 0
  95. ' ensure we have an absolute path
  96. B = CBool(PathIsRelative(PathSpec))
  97. If B = True Then
  98. RecursiveMkDir = ErrRelativePath
  99. Exit Function
  100. End If
  101. ' if the directory already exists, get out with success.
  102. If FSO.FolderExists(PathSpec) = True Then
  103. RecursiveMkDir = ErrSuccess
  104. Exit Function
  105. End If
  106. ' get rid of trailing slash
  107. If Right(PathSpec, 1) = "\" Then
  108. PathSpec = Left(PathSpec, Len(PathSpec) - 1)
  109. End If
  110. ' ensure we don't have a filename
  111. N = InStrRev(PathSpec, "\")
  112. M = InStrRev(PathSpec, ".")
  113. If (N > 0) And (M > 0) Then
  114. If M > N Then
  115. ' period found after last slash
  116. RecursiveMkDir = ErrSpecIsFileName
  117. Exit Function
  118. End If
  119. End If
  120. If Left(PathSpec, 2) = "\\" Then
  121. ' UNC -> \\Server\Share\Folder...
  122. N = InStr(3, PathSpec, "\")
  123. N = InStr(N + 1, PathSpec, "\")
  124. Root = Left(PathSpec, N - 1)
  125. DirSpec = Mid(PathSpec, N + 1)
  126. Else
  127. ' Local or mapped -> C:\Folder....
  128. N = InStr(1, PathSpec, ":", vbBinaryCompare)
  129. If N = 0 Then
  130. RecursiveMkDir = ErrInvalidPathSpecification
  131. Exit Function
  132. End If
  133. Root = Left(PathSpec, N)
  134. DirSpec = Mid(PathSpec, N + 2)
  135. End If
  136. Set DD = FSO.GetDrive(Root)
  137. Directories = Split(DirSpec, "\")
  138. DirSpec = DD.Path
  139. For N = LBound(Directories) To UBound(Directories)
  140. DirSpec = DirSpec & "\" & Directories(N)
  141. If FSO.FolderExists(DirSpec) = False Then
  142. On Error Resume Next
  143. err.Clear
  144. FSO.CreateFolder (DirSpec)
  145. If err.Number <> 0 Then
  146. RecursiveMkDir = ErrDirectoryCreateError
  147. Exit Function
  148. End If
  149. End If
  150. Next N
  151. RecursiveMkDir = ErrSuccess
  152. End Function