'********************************************************************************************************** ' Name: deleteFolder ' Author: mielk | 2014-08-16 ' ' Comment: Function to delete the specified folder from the file system. ' ' Parameters: ' filepath The path of a folder to be deleted. ' ' Returns: ' Boolean True - if the given folder has been deleted or the given folder doesn't exist. ' False - if it was impossible to delete the given folder (i.e. it is being used ' at this moment). ' ' ' --- Changes log ----------------------------------------------------------------------------------------- ' 2014-08-16 mielk Function created. '********************************************************************************************************** Public Function deleteFolder(folderPath As String) As Boolean Const METHOD_NAME As String = "deleteFolder" Const ERR_NUM_PATH_NOT_FOUND As Long = 76 '------------------------------------------------------------------------------------------------------ Static objFSO As Object '(Late binding that allows to use the function, even if 'Microsoft Scripting Runtime library is not loaded) Dim path As String '------------------------------------------------------------------------------------------------------ 'Create FileSystemObject instance if it hasn't been created yet. ------------------------------------| If objFSO Is Nothing Then '| Set objFSO = VBA.CreateObject("Scripting.FileSystemObject") '| End If '| '----------------------------------------------------------------------------------------------------| 'Convert the given folder path to format without the last slash. path = VBA.IIf(VBA.right$(folderPath, 1) = "\", _ cutChars(folderPath, 1), folderPath) 'Try to remove the given file. If it is not possible code moves to the DeleteFileException label. ---| On Error GoTo DeleteFolderException '| Call objFSO.deleteFolder(path) '| deleteFolder = True '| '----------------------------------------------------------------------------------------------------| '========================================================================================================== ExitPoint: Exit Function '---------------------------------------------------------------------------------------------------------- DeleteFolderException: If VBA.Err.number = ERR_NUM_PATH_NOT_FOUND Then 'Foldercannot be deleted because it doesn't exist. 'In this case True should be returned. deleteFolder = True Else 'Folder cannot be deleted for any other reason. deleteFolder = False End If End Function '********************************************************************************************************** ' Name: cutChars ' Author: mielk | 2013-05-15 ' ' Comment: Function cuts the specified number of characters at the end of the source string. ' ' Parameters: ' text String to be shortened. ' chars Number of characters to be cutted. It should be positive number. ' If this argument is 0 or negative number, the original string is returned without ' any changes. ' ' Returns: ' String The original string without the specified number of characters at the end of this ' string. ' ' Example: ' ?cutChars("String", 3) = "Str" <--- 3 characters have been cut from the original ' string "String") ' ' ' --- Changes log ----------------------------------------------------------------------------------------- ' 2013-05-15 mielk Method created. '********************************************************************************************************** Public Function cutChars(text As String, chars As Integer) As String Const METHOD_NAME As String = "cutChars" '------------------------------------------------------------------------------------------------------ 'Check if the number of characters to be cutted is not greater than the length of the base string. 'If it is, return empty String. If chars > VBA.Len(text) Then cutChars = "" Else cutChars = VBA.Left$(text, VBA.Len(text) - chars) End If End Function