I need to retrieve a relative pathname but the CommonDialog Box just doesn't seem to allow this option.
The FileName property just gives you an absolute pathname.
Could someone help me please ?
Printable View
I need to retrieve a relative pathname but the CommonDialog Box just doesn't seem to allow this option.
The FileName property just gives you an absolute pathname.
Could someone help me please ?
Do you mean that you want a dialog box (like the common dialog that comes with VB) which allows the user to select a path name relative to the current directory?
If so, the following might be a starting point (you will have to write your own dialog box)..
The two controls used are a command button and a dir list box. The dir list box is handy but makes you select an absolute path. If this is not possible at all (or if you want to hide the absolute path) then you have to dig deeper and make your own "relative path list box"
Hope it helps you get startedCode:Private Sub Command1_Click()
Debug.Print GetRelativePath(Dir1.List(Dir1.ListIndex), "c:")
End Sub
Public Function GetRelativePath(path As String, relativeTo As String) As String
' strips out the relativeTo prefix
' returrns empty string if relativeTo is not found
' does not take into account . or .. directories
Dim pos As Integer
Dim res As String
res = ""
pos = InStr(1, path, relativeTo)
If pos = 1 Then
res = Mid(path, Len(relativeTo) + 1) '
If res = "" Then res = "\"
End If
GetRelativePath = res
End Function
Private Sub Form_Load()
Dir1.path = "c:\"
End Sub
Paul Lewis
hi,
i didn't really understand your question.
if you mean that if you select a file in the commondialoge lets say: "c:\app\app.txt" then you want the return : "c:\app\"?
if i am correct then all you have to do is:
lets say the commondialoge is called: cmd.
dim lenPath
dim CmdPath
lenPath =len(cmd.FileTitle)
lenCmd = len(cmd.FileName)
CmdPath = mid(cmd.FileName,1,lenCmd-lenPath)
now if the file selected is : "c:\app\app.txt" then the return is "c:\app\"
hope it helped,
bye,
yair.
ysa1441, the problem here is that if you were a user of the program, you would be "forced" to select a file.
Also, the common dialog box lets you change disks which I precieved to be a part of the problem (whats the relative path from c:\apps to d:\test ?)
With Gillles second message though, it is clear that he wants to have the ".." in the relative path name.
This new example although alot more complex is still only using simple non-API based ideas. It might help I hope.
Even if it doesn't it provides one method of breaking a path down into components which you can then manipulate any way you like.
Code:
Private Sub Command1_Click()
Dim startPath As String
startPath = "c:\" 'put your full path here
Debug.Print "Relative path from " & startPath & " to " & Dir1.List(Dir1.ListIndex)
Debug.Print GetRelativePath(Dir1.List(Dir1.ListIndex), startPath)
End Sub
Public Function GetRelativePath(path As String, relativeTo As String) As String
' fast exit for special case
If path = relativeTo Then
GetRelativePath = ".\"
Exit Function
End If
Dim res As String
Dim myPath() As String
Dim relPath() As String
myPath = GetPathFromString(path)
relPath = GetPathFromString(relativeTo)
Dim c, startPos, pathLen, relLen As Integer
pathLen = UBound(myPath)
relLen = UBound(relPath)
If InStr(1, path, relativeTo) > 0 Then
' the entire relative to is within the path
res = ".\"
startPos = relLen + 1
Else
res = ""
startPos = 0
End If
' get the rest of the path
For c = startPos To pathLen
If c <= relLen Then
If relPath(c) = myPath(c) Then
res = res & "..\"
Else
res = res & myPath(c) & "\"
End If
Else
res = res & myPath(c) & "\"
End If
Next
GetRelativePath = res
End Function
Function GetPathFromString(path As String) As String()
' returns an array of strings representing the "path" to a file
' based on the common notation of A:\path1\path2
ReDim myPath(0) As String
Dim lastpos, pos, c As Integer
c = 0
pos = 0
Do
lastpos = pos
pos = InStr(pos + 1, path, "\")
If pos > 0 Then
myPath(c) = Mid(path, lastpos + 1, pos - lastpos - 1)
If pos < Len(path) Then
c = c + 1
ReDim Preserve myPath(c)
End If
Else
myPath(c) = Mid(path, lastpos + 1, Len(path) - lastpos)
End If
Loop Until pos = 0
GetPathFromString = myPath
End Function
Private Sub Form_Load()
Dir1.path = "c:\"
End Sub
I've checked your code and it nearly works...
For example if startpath is c:\thing\stuff
and you want a relative path to c:\thing
the answer is ..\.. instead of ..\
or if you want a relative to c:\stuff
the answer is ..\ instead of ..\..
I don't have time now to really think about it, but here is an idea
you have two string : path and relativeTo
with the split function you split both
then you compare the results words by words (I mean the first occurence of split(path) with the first occurence of split(relativeTo) ...)
You watch for the first <> tokens and you mark the position : i
(if there's any i = Total Tokens of relativeTo)
then result = "..\" * ((Total Tokens of relativeTo) - i)
& "path" (without the common part between path and relativeTo)
I don't know if you see what I mean, I'll try to write the code as soon as possible.
bye,
Gilles
Sorry about any bugs... That happens when you code something quickly :(
As long as it has given you a starting point where you can get your own final solution I will feel like I've helped...
Cheers
PAul Lewis
You really helped Paul
Thanks
Gilles DEVAUX