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 Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check if Directory Exists 1

Status
Not open for further replies.

Guest_imported

New member
Joined
Jan 1, 1970
Messages
0
Hi,

I am new to VB!

I have a small project that lists all diectories on a drive. (combo box, dir list box, text box, and 2 command buttons - create and remove)

I would like to create a new directory, name will come from a text box. I am trying to check if the name that i type into the text box(name of directory) exists. If exists display an error message, then place focus back to the text box. If does not exist then create that directory.

Dim strStartDir As String, strCurDir As String ' current dir


If txtNewDir.Text = "Enter name for new directory" Or txtNewDir.Text = "" Then
MsgBox "Plese ENTER a name of NEW Directory", vbOKOnly, "Data Entry Error!"
txtNewDir.SetFocus
Exit Sub

Else

strNewDir = Dir(txtNewDir.Text, vbReadOnly + vbHidden + vbSystem + vbDirectory)

'// extract start directory from file path
strStartDir = Left$(strStartDir, InStrRev(strStartDir, "\"))
'// just get filename
strCurDir = Right$(strStartDir, Len(strStartDir) - InStrRev(strStartDir, "\"))



'If we get nothing in the strDirectory string then the directory doesn't exists.

' strNewDir = Dir$(strCurDir, vbDirectory)
If (Len(strCurDir) > 0) And (Err = 0) Then

MsgBox "Directory already exists..." & vbCrLf & vbCrLf & _
"Please enter a Different Directory Name!", vbCritical, "Directory Already Exists..."
txtNewDir.SetFocus
Exit Sub

Else

MkDir (strCurDir) & "\" & strNewDir ' Make new directory or folder
txtNewDir.Text = "Enter name for new directory"
dirCurDir.Refresh
End If
End If

Thanks

Newbie_999
 
You can test whether a directory exists by checking for a "." file with Dir$(). "." is a special entry in every directory except the root, and it refers to the directory itself. So, to test whether a non-root directory exists, use:
DirExists = Dir$(DirPath & &quot;\.&quot;) <> &quot;&quot;
This assumes that DirPath doesn't already have a trailing blackslash. Remember, too, that it doesn't work for a root directory; you'll have to check for that specially, anyway, since you can't create it with MkDir. Rick Sprague
 
The FileSystemObject, which is part of the Scripting Runtime library scrrun.dll provides all needed methods. This object is included into your project through the Project Menu -> References -> Check Scripting Runtime Library.

A skeleton program may look like this:


Dim strFolderName as String

Dim objFso as Scripting.FileSystemObject
Set objFso = New Scripting.FileSystemObject

With objFso
Select Case .FolderExists(strFolderName)
Case True
... Error Message
Case False
.CreateFolder(strFolderName)
End Select
End With

Set objFso = Nothing


strFolderName should be somewhere initialize with the Name of the Folder you want to check.


_________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top