OA_Path.bas 4.2 KB

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