Getting path of selected folder in VB6
I want to get the selected folder path
dlgBrowse.ShowOpen
fname = dlgBrowse.FileName
dlgBrowse.Filter = "Text File (*.txt)|*.txt|Log File (*.log)|*.log||All Files (*.*)|*.*"
dlgBrowse.DialogTitle = "Open Log File"
dlgBrowse.ShowOpen
If dlgBrowse.FileName <> "" Then
txtLogFile.Text = dlgBrowse.FileName
End If
MsgBox fname
This shows the output "C:\MRMS\Report\xyz.txt"
, but I want only the selected folder path, ie if t开发者_如何转开发he user selects only root(MRMS) folder i.e. "C:\MRMS"
or any other folder only up to user selected folder.
The shortest way:
Dim FullPath as string, ParentFolder as string, l() as string
FullPath = "" '... Write here the path from ComDlg
l = Split(FullPath, "\")
l(UBound(l)) = ""
ParentFolder = Join(l, "\")
Try this
Private Function GetRootDir(ByVal inputString As String) As Integer
'min real path is c:\. We need a len of at least 2
If Len(inputString) < 2 Then
GetRootDir = ""
End If
Dim t As Integer, s As Integer
t = InStr(1, inputString, "\")
If t < 1 Then
GetRootDir = ""
Exit Function
End If
s = InStr(t + 1, inputString, "\")
'If this is the root folder that was selected
If s < 1 Then
GetRootDir = Mid(inputString, t + 1)
Exit Function
End If
GetRootDir = Mid(inputString, t + 1, s - t - 1)
End Function
Then, in your code, reference the function like this...
txtLogFile.Text = GetRootDir(dlgBrowse.FileName)
An alternative way to find the folder of selected file by Common Dialogue is this:
FilePath = Replace(CommonDialog1.FileName, "\" & CommonDialog1.FileTitle, "")
Here's a simple bare-bones solution for VB5 which doesn't have the Split function:
For Num = Len(CommonDialog1.filename) To 4 Step -1
Charac = Mid$(CommonDialog1.filename, Num, 1)
If Charac = "\" Then Exit For
Next
LastSlashPos = Num
LastPath = Left$(CommonDialog1.filename, LastSlashPos)
精彩评论