Re: Relative path to absolute

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Ken Halter (Ken_Halter_at_Use_Sparingly_Hotmail.com)
Date: 04/02/04


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
>
>
>


Relevant Pages

  • Search pattern
    ... Dim strfile As String ... Dim bAddressFound As Boolean ... Dim strCurrentChar As String ...
    (comp.databases.ms-access)
  • Auto Write Name and Merge across
    ... Dim Sheetname01 As String ... Dim WeekName01 As String ...
    (microsoft.public.excel.misc)
  • Re: multiplatform (pocketPC & desktopPC) (Daniel !!)
    ... Friend Versione As String ... Public Sub GetMyConnectionPalmare() ... Dim errorMessages As String ... Private Function GetDS_Desktop(ByVal SQL As String) As DataSet ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: multiplatform (pocketPC & desktopPC) (Daniel !!)
    ... Friend Versione As String ... Public Sub GetMyConnectionPalmare() ... Dim errorMessages As String ... Private Function GetDS_Desktop(ByVal SQL As String) As DataSet ...
    (microsoft.public.dotnet.framework.compactframework)
  • Help answer these 70-310 questions
    ... One argument is the string ... Dim output As New StringBuilder ... EmployeeLocations. ... You create a strongly named serviced component. ...
    (microsoft.public.cert.exam.mcad)