Guest_imported
New member
- Jan 1, 1970
- 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
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