Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to check to see if folder exists on network drive 2

Status
Not open for further replies.

Tarnish

Technical User
Nov 13, 2006
221
US
Hi,

I need to be able to, in vba, check a network drive for the existence of a folder. How can I do that?

I can make a folder, but if it's already been made then I get an error which I could handle in the error-handler. But, I'd much prefer being able to indicate on my form whether or not the folder exists before the attempt is made.

Thanks for any direction,
T
 
Use file system objects
Code:
    Dim FSO As New FileSystemObject
...

    If FSO.FolderExists(strDirectoryPath) = False Then
        FSO.CreateFolder (strDirectoryPath) ' Create subfolder
    End If
Make sure you have the proper references added - Microsoft Scripting Runtime

Greg
"Personally, I am always ready to learn, although I do not always like being taught." - Winston Churchill
 
Thanks traingamer! I'll give it a go.

T
 
Another way is to write your own function:
Code:
Sub myMkDir(strFolderName As String)
On Error Resume Next
Dim a, t As String, i As Integer
a = Split(strFolderName, "\")
t = a(0)
For i = 1 To UBound(a)
  t = t & "\" & a(i)
  MkDir t
Next
End Sub

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Another way is to use the Dir Function. Me.txtRunID is the new folder name at the end of the path and is in a text box on a form. User can change the RunID on the form before creating the new folder on the network.

Code will only create the new directory if it does not already exist.

Code:
Dim NewID As String

Dim newDirSpecial As String
Dim lastDirSpecial As String

NewID = Me.txtRunID

[COLOR=green]'Define the directories
'F:\special\13000\MyGroupOnly [/color]
newDirSpecial = "F:\special\13000\MyGroupOnly" & "\" & NewID
lastDirSpecial = Dir(newDirSpecial, vbDirectory)

[COLOR=green]'Create new directory if it does NOT already exist [/color]
    If lastDirSpecial <> NewID Then
        MkDir newDirSpecial
    End If
 
I love Access (and these forums)- there's never just one way to do things. [smile]
 
Thanks guys!

I'll play around with the other two suggestions as well. Traingamer's suggestion worked and it's all setup that way, but I'm always looking to learn and it might be nice to set it up to allow users to name the folder.

Right now, I've set it up using a formula for the folder path and name. That has some benefits in that users don't have to worry about naming folders, and there's a consistent naming convention for them all.

Thanks to all three of you for the help,
T
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top