AT_FileOp.bas 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. Option Compare Database
  2. Option Explicit
  3. ' ** Access Toolbox Module **
  4. ' on 2017-02-28,
  5. ' @author: Olivier Massot
  6. ' V 1.0
  7. ' Various file operations
  8. Public Function norm_path(ByVal path As String) As String
  9. path = replace(path, "/", "\")
  10. Do Until path = replace(path, "\\", "\")
  11. path = replace(path, "\\", "\")
  12. Loop
  13. norm_path = path
  14. End Function
  15. Public Function norm_dir_path(ByVal dir_path As String) As String
  16. dir_path = norm_path(dir_path)
  17. If Right(dir_path, 1) <> "\" Then dir_path = dir_path & "\"
  18. norm_dir_path = dir_path
  19. End Function
  20. Public Function joinpaths(ByVal path1 As String, ByVal path2 As String) As String
  21. path1 = norm_path(path1)
  22. path2 = norm_path(path2)
  23. If Not Right(path1, 1) = "\" Or Left(path2, 1) = "\" Then
  24. path1 = path1 & "\"
  25. End If
  26. joinpaths = path1 & path2
  27. End Function
  28. Public Function dir_exists(ByVal dir_path As String) As Boolean
  29. dir_exists = Dir(norm_dir_path(dir_path), vbDirectory) <> ""
  30. End Function
  31. Public Function file_exists(ByVal file_path As String) As Boolean
  32. file_exists = Dir(norm_path(file_path)) <> ""
  33. End Function
  34. Public Sub mktree(ByVal dirpath As String)
  35. 'recursively create the directory if it does not exist
  36. On Error GoTo err
  37. Dim path_part, current_path As String
  38. current_path = ""
  39. dirpath = norm_dir_path(dirpath)
  40. If Dir(dirpath, vbDirectory) <> "" Then Exit Sub
  41. For Each path_part In Split(dirpath, "\")
  42. If Len(path_part) > 0 Then
  43. current_path = current_path & path_part & "\"
  44. If Dir(current_path, vbDirectory) = "" Then
  45. MkDir current_path
  46. End If
  47. End If
  48. Next path_part
  49. Exit Sub
  50. err:
  51. If err.number = 75 Then
  52. 'dir already exist
  53. Else
  54. logger "MkDirIfNotExist", "ERROR", "Unable to create directory " & dirpath & " : " & err.Description
  55. End If
  56. End Sub
  57. Public Function parent_dir(path As String) As String
  58. parent_dir = CreateObject("Scripting.FileSystemObject").GetParentFolderName(path)
  59. 'OR parent_dir = Left(path, InStrRev(path, "\", Len(path) - 1))
  60. End Function
  61. Public Function file_name(path As String) As String
  62. file_name = CreateObject("Scripting.FileSystemObject").GetBaseName(path)
  63. 'OR file_name = Split(cheminFichier, "\")(UBound(Split(cheminFichier, "\")))
  64. End Function
  65. Public Function abs_path(path As String) As String
  66. 'returns the absolute path
  67. abs_path = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(path)
  68. End Function
  69. Public Sub del_if_exist(ByVal path As String)
  70. 'delete a file if it exists
  71. If Dir(path) <> "" Then
  72. Kill path
  73. End If
  74. End Sub
  75. Public Function list_files_in(ByVal dirpath As String, Optional ByVal filter As String = "")
  76. Dim filename As String
  77. list_files_in = ""
  78. dirpath = norm_dir_path(dirpath)
  79. filename = Dir$(dirpath & filter)
  80. Do Until Len(filename) = 0
  81. If Len(list_files_in) > 0 Then list_files_in = list_files_in & "|"
  82. list_files_in = list_files_in & filename
  83. filename = Dir$()
  84. Loop
  85. End Function
  86. Public Function read_file(filePath As String, Optional encoding As String = "utf-8") As String
  87. Dim objStream As ADODB.Stream
  88. Set objStream = New ADODB.Stream
  89. objStream.Charset = encoding
  90. objStream.Open
  91. objStream.LoadFromFile (filePath)
  92. ReadFile = objStream.ReadText()
  93. objStream.Close
  94. Set objStream = Nothing
  95. End Function
  96. Public Sub make_file(filePath As String, content As String, Optional encoding As String = "utf-8")
  97. Dim objStream As ADODB.Stream
  98. Set objStream = CreateObject("ADODB.Stream")
  99. objStream.Open
  100. objStream.Type = 2 'Text
  101. objStream.Charset = encoding
  102. objStream.WriteText content
  103. objStream.SaveToFile (filePath)
  104. objStream.Close
  105. End Sub
  106. ' Generate Random / Unique tempprary file name.
  107. Public Function temp_filename(Optional ByVal sPrefix As String = "tmp") As String
  108. Dim sTmpPath As String * 512
  109. Dim sTmpName As String * 576
  110. Dim nRet As Long
  111. Dim sFileName As String
  112. nRet = getTempPath(512, sTmpPath)
  113. nRet = getTempFileName(sTmpPath, sPrefix, 0, sTmpName)
  114. If nRet <> 0 Then sFileName = Left$(sTmpName, InStr(sTmpName, vbNullChar) - 1)
  115. TempFile = sFileName
  116. End Function
  117. Public Function is_valid_filename(ByVal sName As String) As Boolean
  118. 'returns True if sName is a valid file's name
  119. IsValidFileName = (InStr(sName, "\") = 0 And InStr(sName, "/") = 0 And InStr(sName, "*") = 0 And InStr(sName, "?") = 0 And InStr(sName, Chr(34)) = 0 And InStr(sName, "|") = 0 And InStr(sName, ":") = 0 And InStr(sName, ">") = 0 And InStr(sName, "<") = 0)
  120. End Function
  121. Public Function run_file(filePath As String, Optional args As String = "")
  122. Shell "cmd.exe /r start " & filePath & " " & args, vbHide
  123. End Function
  124. Public Sub open_file(filePath As String)
  125. Application.FollowHyperlink filePath
  126. End Sub