'********************************************************************************************************** ' Name: isFolderWriteable ' Author: mielk | 2012-12-02 ' ' Comment: Function to check if the current user has the rights to add, delete and modify ' files in the specified folder ' ' Parameters: ' folderPath The path of a folder to be checked. ' ' Returns: ' Boolean True - if the given folder exists and the current user has the read-write ' access to this folder. ' False - if the given folder doesn't exist or the user has not read-write ' access to it. ' ' Exceptions: ' FolderNotExistException Thrown if a folder with the specified path doesn't exist. ' ' ' --- Changes log ----------------------------------------------------------------------------------------- ' 2012-12-02 mielk Function created. '********************************************************************************************************** Public Function isFolderWriteable(folderPath As String) As Boolean Const METHOD_NAME As String = "isFolderWriteable" Const TEMP_FILE_NAME As String = "TestFile.txt" '------------------------------------------------------------------------------------------------------ Static objFSO As Object '(Late binding that allows to use the function, even if 'Microsoft Scripting Runtime library is not loaded) Dim strTempFile 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 '| '----------------------------------------------------------------------------------------------------| 'Check if the given folder exists at all. -----------------------------------------------------------| If objFSO.FolderExists(folderPath) Then '| '| 'Create the full path of a temporary file. '| strTempFile = folderPath '| If Not VBA.right$(folderPath, 1) = "\" Then strTempFile = strTempFile & "\" '| strTempFile = uniqueFilePath(strTempFile & TEMP_FILE_NAME) '| '| '| 'Try to create a temporary file. --------------------------------------------------------| '| On Error Resume Next '| '| Call objFSO.CreateTextFile(strTempFile) '| '| On Error GoTo 0 '| '| '----------------------------------------------------------------------------------------| '| '| '| 'Check if temporary file has been created. If it exists, the function should ------------| '| 'return True and delete this file. '| '| If objFSO.fileExists(strTempFile) Then '| '| isFolderWriteable = True '| '| Call objFSO.deleteFile(strTempFile) '| '| End If '| '| '----------------------------------------------------------------------------------------| '| '| Else '| '| GoTo FolderNotExistException '| '| End If '| '-------- [If objFSO.FolderExists(folderPath) Then] -------------------------------------------------| '========================================================================================================== ExitPoint: Exit Function '---------------------------------------------------------------------------------------------------------- FolderNotExistException: '(...) 'Put your own error handling here for a case if the given folder does not exist. GoTo ExitPoint End Function '********************************************************************************************************** ' Name: uniqueFilePath ' Author: mielk | 2012-12-02 ' ' Comment: Function transforms the given filepath so that it is unique in the file system. ' ' Parameters: ' filepath The filepath to be transformed. ' ' Returns: ' String The specified filepath transformed into a unique one. ' If the specified filepath doesn't exist yet, the original name is returned. ' If the file with the specified path already exist, the path returned by the ' function consists of the original filepath and the number in brackets appended ' after the filename. ' ' Example: ' --------------------------------------------------------------------------------- ' If you run this function for filepath C:\test.txt and such file doesn't exist ' yet, the original filepath (C:\test.txt) is returned. ' ' However, if such file already exists, the filepath C:\test (1).txt is returned. ' In case this transformed filepath also exists already, the next number is appended: ' C:\test (2).txt and so on. ' ' ' Please note that the function doesn't check if the given filepath has been ' constructed properly. If the function is invoked with the improper filepath, it is ' just trying to find this file in the file system, but won't find it, since the ' filepath is improper, and will think about it as if it was a unique filepath. ' ' ' --- Changes log ----------------------------------------------------------------------------------------- ' 2012-12-02 mielk Function created. '********************************************************************************************************** Public Function uniqueFilePath(filepath As String) As String Const METHOD_NAME As String = "uniqueFilePath" '------------------------------------------------------------------------------------------------------ Static objFSO As Object '(Late binding that allows to use the function, even if 'Microsoft Scripting Runtime library is not loaded) Dim strFileExtension As String Dim strFileName As String Dim strParentFolder As String Dim strTempFilePath As String Dim intCounter As Integer '------------------------------------------------------------------------------------------------------ 'Create FileSystemObject instance if it hasn't been created yet. ------------------------------------| If objFSO Is Nothing Then '| Set objFSO = VBA.CreateObject("Scripting.FileSystemObject") '| End If '| '----------------------------------------------------------------------------------------------------| With objFSO 'Checks if the file already exists. -------------------------------------------------------------| If .fileExists(filepath) Then '| '| 'If the given filepath already exists, function transforms its name by '| 'appending the number in brackets. '| strParentFolder = .GetParentFolderName(filepath) '| If Not VBA.right$(strParentFolder, 1) = "\" Then strParentFolder = strParentFolder & "\" '| strFileName = .GetBaseName(filepath) '| strFileExtension = "." & .GetExtensionName(filepath) '| '| '------------------------------------------------------------------------------------| '| Do '| '| intCounter = intCounter + 1 '| '| strTempFilePath = strParentFolder & strFileName & _ " (" & intCounter & ")" & strFileExtension '| '| Loop While .fileExists(strTempFilePath) '| '| '------------------------------------------------------------------------------------| '| '| uniqueFilePath = strTempFilePath '| '| Else '| '| 'Specified filepath is unique in the file system and is returned in its original form. '| uniqueFilePath = filepath '| '| End If '| '-------- [If .FileExists(filepath) Then] -------------------------------------------------------| End With End Function