AccessBlog.net

News, links, downloads, tips and tricks on Microsoft Access and related

About Me Search
Alex
Name:Alex Dybenko

Location:Moscow, Russia

Wednesday, December 12, 2007

Is file or folder writable?

I did not find such VBA function on internet, so wrote my own, just pass a folder or file name:

Public Function IsWritable(ByVal filePath As String) _
As Boolean
If Right(filePath, 1) = "\" Then _
filePath = Left(filePath, Len(filePath) - 1)

If Len(Dir(filePath)) > 0 Then
IsWritable = IsFileWritable(filePath)
ElseIf Len(Dir(filePath, vbDirectory)) > 0 Then
IsWritable = IsFileWritable(filePath & "\" & "~.txt")
If Len(Dir(filePath & "\" & "~.txt")) > 0 Then _
Kill filePath & "\" & "~.txt"
Else
IsWritable = IsFileWritable(filePath)
If Len(Dir(filePath)) > 0 Then _
Kill filePath
End If
End Function
Public Function IsFileWritable(ByVal filePath As String) _
As Boolean
On Error Resume Next
Err.Clear
Dim nFileNum As Integer
nFileNum = FreeFile
Open filePath For Append As nFileNum
Print #nFileNum, " "
Close nFileNum
IsFileWritable = (Err.Number = 0)
End Function


Technorati tags:

0 Comments:

Post a Comment

<< Home