Re: Relative path to absolute
From: Ken Halter (Ken_Halter_at_Use_Sparingly_Hotmail.com)
Date: 04/02/04
- Next message: gsdgsfgfgsf: "Re: Need HELP QUICK!"
- Previous message: Tom Esh: "Re: Form question"
- In reply to: Mike Kanski: "Relative path to absolute"
- Next in thread: Larry Serflaten: "Re: Relative path to absolute"
- Messages sorted by: [ date ] [ thread ]
Date: Fri, 2 Apr 2004 11:05:59 -0800
This seems to do the job just fine. Doesn't actually test for the existance
of the file. It just returns the theoretical path to a file.
'===================
Option Explicit
Private Sub Command1_Click()
Debug.Print _
AbsFromRelativePath(App.Path, "..\SomeFile.txt")
End Sub
Private Function AbsFromRelativePath(StartPath As String _
, RelativePath As String) As String
Dim sReturnPath As String
Dim sRelativePath As String
Dim lDirPos As Long
Dim lPathPos As Long
sReturnPath = StartPath
If Right$(sReturnPath, 1) = "\" Then 'dump the back slash if there is one
sReturnPath = Left$(sReturnPath, Len(sReturnPath) - 1)
End If
sRelativePath = RelativePath
If Left$(sRelativePath, 2) = ".\" Then 'Current folder
sRelativePath = Mid$(sRelativePath, 3)
Else
Do
lDirPos = InStr(1, sRelativePath, "..\")
If lDirPos > 0 Then
sRelativePath = Mid$(sRelativePath, lDirPos + 3)
lPathPos = InStrRev(sReturnPath, "\")
If lPathPos > 0 Then
sReturnPath = Left$(sReturnPath, lPathPos - 1)
Else
Err.Raise 76 'Path not found
End If
End If
Loop While lDirPos > 0
End If
AbsFromRelativePath = sReturnPath & "\" & sRelativePath
End Function
'===================
-- Ken Halter - MS-MVP-VB - http://www.vbsight.com Please keep all discussions in the groups.. "Mike Kanski" <mkanski@asi-solutions.com> wrote in message news:upnLpTNGEHA.1396@TK2MSFTNGP11.phx.gbl... > Is there an easy way to convert relative path to the absolute? > > I have a VB application that reads INI file for all the objects that it > needs to run. > INI file example: > > [Run] > RunBlah=../../Blah.exe > > The app knows the base address: > c:\programs\company\myapp\ > > Now i need to read the RunBlah entry and run: > c:\programs\blah.exe > > Is there an easy way or maybe a function i can call to get the absolute > path? > > i.e. > Function GetAbsolutePath(BasePath,RelativePath) as string > ... > returns absolute path > end function > > >
- Next message: gsdgsfgfgsf: "Re: Need HELP QUICK!"
- Previous message: Tom Esh: "Re: Form question"
- In reply to: Mike Kanski: "Relative path to absolute"
- Next in thread: Larry Serflaten: "Re: Relative path to absolute"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|