OA_Path.bas 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. Option Compare Database
  2. Option Private Module
  3. Option Explicit
  4. 'operations on directories and path
  5. Public Function norm_path(ByVal path As String) As String
  6. path = Replace(path, "/", "\")
  7. Do Until path = Replace(path, "\\", "\")
  8. path = Replace(path, "\\", "\")
  9. Loop
  10. norm_path = path
  11. End Function
  12. Public Function norm_dir_path(ByVal dir_path As String) As String
  13. dir_path = norm_path(dir_path)
  14. If Right(dir_path, 1) <> "\" Then dir_path = dir_path & "\"
  15. norm_dir_path = dir_path
  16. End Function
  17. Public Sub mktree(ByVal dirpath As String)
  18. 'recursively create the directory if it does not exist
  19. On Error GoTo err
  20. Dim path_part, current_path As String
  21. current_path = ""
  22. dirpath = norm_dir_path(dirpath)
  23. If dir(dirpath, vbDirectory) <> "" Then Exit Sub
  24. For Each path_part In Split(dirpath, "\")
  25. If Len(path_part) > 0 Then
  26. current_path = current_path & path_part & "\"
  27. If dir(current_path, vbDirectory) = "" Then
  28. MkDir current_path
  29. End If
  30. End If
  31. Next path_part
  32. logger "MkDirIfNotExist", "INFO", "New dir created: " & dirpath
  33. Exit Sub
  34. err:
  35. If err.Number = 75 Then
  36. 'dir already exist
  37. Else
  38. logger "MkDirIfNotExist", "ERROR", "Unable to create directory " & dirpath & " : " & err.Description
  39. End If
  40. End Sub
  41. Public Function parent_dir(path As String) As String
  42. 'parent_dir = Left(path, InStrRev(path, "\", Len(path) - 1))
  43. parent_dir = CreateObject("Scripting.FileSystemObject").GetParentFolderName(path)
  44. End Function
  45. Public Function joinpaths(ByVal path1 As String, ByVal path2 As String) As String
  46. path1 = norm_path(path1)
  47. path2 = norm_path(path2)
  48. If Not Right(path1, 1) = "\" Or Left(path2, 1) = "\" Then
  49. path1 = path1 & "\"
  50. End If
  51. joinpaths = path1 & path2
  52. End Function
  53. Public Function to_filename(ByVal object_name As String) As String
  54. ' return a file name for the object's name
  55. ' 1- access does not accept brackets for object's names
  56. ' 2- file's names can not contain those caracters:
  57. ' \ [92]
  58. ' / [47]
  59. ' : [58]
  60. ' * [42]
  61. ' ? [63]
  62. ' " [34]
  63. ' < [60]
  64. ' > [62]
  65. ' | [124]
  66. '
  67. ' this function replaces caracters which are not allowed for file names by [x],
  68. 'where x is the ascii code of the character
  69. ' test: "test_\_/_:_*_?_""_<_>_|" should become test_[92]_[47]_[58]_[42]_[63]_[34]_[60]_[62]_[124]
  70. ' to convert back the string, use to_accessname
  71. Dim result As String
  72. Dim ascii_code As Variant
  73. result = object_name
  74. For Each ascii_code In Split(ForbiddenCars, ",")
  75. result = Replace(result, Chr(CInt(ascii_code)), "[" & ascii_code & "]")
  76. Next
  77. If result <> object_name Then
  78. logger "to_filename", "DEBUG", "> Object's name " & object_name & " transformed to " & result
  79. End If
  80. to_filename = result
  81. Exit Function
  82. err:
  83. Call logger("to_filename", "ERROR", "Unable to convert object's name " & object_name & " to file's name")
  84. to_filename = object_name
  85. End Function
  86. Public Function to_accessname(ByVal file_name As String) As String
  87. On Error GoTo err
  88. ' return an object name from a file's name
  89. ' see function 'to_filename' for more informations
  90. ' test: "test_[92]_[47]_[58]_[42]_[63]_[34]_[60]_[62]_[124]" should become test_\_/_:_*_?_"_<_>_|
  91. Dim result As String
  92. Dim ascii_code As Variant
  93. result = file_name
  94. For Each ascii_code In Split(ForbiddenCars, ",")
  95. result = Replace(result, "[" & ascii_code & "]", Chr(CInt(ascii_code)))
  96. Next
  97. If result <> file_name Then
  98. logger "to_accessname", "DEBUG", "> File's name " & file_name & " transformed to " & result
  99. End If
  100. to_accessname = result
  101. Exit Function
  102. err:
  103. Call logger("to_accessname", "ERROR", "Unable to convert file's name " & file_name & " to access object's name")
  104. to_accessname = file_name
  105. End Function
  106. Public Sub del_if_exist(ByVal path As String)
  107. 'delete a file if it exists
  108. If dir(path) <> "" Then
  109. Kill path
  110. End If
  111. End Sub
  112. Public Function list_files_in(ByVal dirpath As String, Optional ByVal filter As String = "")
  113. Dim filename As String
  114. list_files_in = ""
  115. dirpath = norm_dir_path(dirpath)
  116. filename = dir$(dirpath & filter)
  117. Do Until Len(filename) = 0
  118. If Len(list_files_in) > 0 Then list_files_in = list_files_in & "|"
  119. list_files_in = list_files_in & filename
  120. filename = dir$()
  121. Loop
  122. End Function