Check if Directory Exits

rnutts

Registered User.
Local time
Today, 16:54
Joined
Jun 26, 2007
Messages
110
Hi

I have some code that creates a directory on a server based upon fields from a form. These fields are represented in the code by strEnq, strJob, strSubJob.
I also wish to check that the directory exists before performing the mkdirectory code. I have searched forums and looked at the vba help and have come up with the following code but it is not working. Could someone please look at it and point me in the right direction.

retvalue = Dir("\\nwaps-sbs\e\SurveyorsPhotosDrawings\ & strEnq & " - " & strJob & " - " & strSubJob")
If retvalue = True Then
MsgBox "Exists"
Else
MsgBox "help"
End If

I know that the directory exists but I just get the message box "help"

Thanks

Richard
 
dir("C:\Temp",vbdirectory)
returns
Temp

So your retvalue would return the name of the folder you are looking for...

If revalue = & strEnq & " - " & strJob & " - " & strSubJob" then

would make it work....

Even better would be to create a string variable
Dim strFolder as string
strFolder = strEnq & " - " & strJob & " - " & strSubJob"
retvalue = Dir("\\nwaps-sbs\e\SurveyorsPhotosDrawings\ & strFolder)
If retvalue = strFolder Then

Because you are using the same thing twice, this way if it ever changes you only need to change it once.

Yet another alternative is to change it to:
If retvalue = "" then
MsgBox "help"
Else
MsgBox "Exists"
End If
Effectively turning your equation around... but still using the folder only once.

Good Luck !
 
Still struggling!!!
Have altered code as follows

strFolder = " & strEnq & " - " & strJob & " - " & strSubJob"
retvalue = Dir("\\nwaps-sbs\e\SurveyorsPhotosDrawings\strFolder")
If retvalue = strFolder Then
MsgBox "Exists"
Else
MsgBox "help"
End If

I am now getting a type mismatch error on the strFolder line of code!!!

thanks

Richard
 
Try below

strFolder = strEnq & " - " & strJob & " - " & strSubJob
retvalue = Dir("\\nwaps-sbs\e\SurveyorsPhotosDrawings\" & strFolder)
If retvalue = strFolder Then
MsgBox "Exists"
Else
MsgBox "help"
End If
 
Right the strFolder code works now, I also has some spaces in the "-" bit of the code which wasn't helping. However I still have the basic problem in that I don't think the code is actually checking the directory exists. Could you confirm or deny this and if not what function should I be using

Thanks

Richard
 
I confirm that it works!

Try this:
retvalue = Dir("C:\Windows")
If retvalue = strFolder Then
MsgBox "Exists"
Else
MsgBox "help"
End If

That will give the message "Exists"

Now try
retvalue = Dir("C:\RnuttsIsNuts")
If retvalue = strFolder Then
MsgBox "Exists"
Else
MsgBox "help"
End If
This will (probably/hopefully) return "help"
(Hope you dont mind the pun ;), it probably isnt the first time you seen it... )
 
P.S.

There is a "better way" to check this, using the "file system object" but that is FAR more complicated and involved, while this works just as well.
 
the clincher was the ,vbdirectory after strFolder

thanks

Richard
 
Yes offcourse... Otherwize it will also pick up a file... Sorry for my bad :(
 

Users who are viewing this thread

Back
Top Bottom