View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Kevin Stecyk Kevin Stecyk is offline
external usenet poster
 
Posts: 74
Default Test if a folder exists

Jeff,

I went to Google. You should find this post by Stephen Bullen to be very
helpful.
http://groups.google.com/groups?q=ex...%40mars&rnum=1

I will save you the trouble and simply paste it here.

Best regards,
Kevin



Hi Rajko,

I have a problem if I want to write a file to a folder which not exist.
How can I check if folder exists, then create it and write a file.


There are actually four outcomes to consider:

1. The folder exists - we're OK
2. The folder doesn't exist and there is no file of the same name - we
can create the folder and be OK
3. The folder doesn't exist and there IS a file of the same name - we
can't create the folder
4. A disk error ocurred during 1-3.

I might be tempted <g to write a routine for it like:

Sub Test()

MsgBox CheckFolder("C:\Test")

End Sub

Function CheckFolder(sPath As String) As Boolean

On Error GoTo ERR_DISKERROR

'Dir checks files as well as directories
If Dir(sPath, vbDirectory) < "" Then

'Is it a directorythat we found, or a file?
If GetAttr(sPath) And vbDirectory Then

'It's a directory, so we're OK
CheckFolder = True
Else
'What to do if you can't create the directory
End If
Else
'The directory doesn't exist, nor does a file of the same name
MkDir sPath
CheckFolder = True
End If

Exit Function

ERR_DISKERROR:

'Handle the error how you like, and terminate the function
'examples a
' Drive doesn't exist/not accessible (e.g. A:/Test without a disk in)
' Invalid path string (e.g. contains invalid characters)
' Disk full when creating folder
' Disk not writable
' Path too long
' etc.

End Function


Regards

Stephen Bullen
Microsoft MVP - Excel
http://www.BMSLtd.co.uk

"Jeff Marshall" wrote in message
...
Hi,

I am making a folder using VBA called "SeptInv" in C:\Billing\Invoices\ .

However before the folder is made I don't know how to;

1. Test if the SeptInv folder has already been made and
2. Once it has been made that my VBA macro doesn't try to make it again.

Thanks in advance for any help.
Jeff