Relative Path Function <BR><SMALL>Gets the relative path of a file w.r.t. a directory</SMALL>
A reusable function that computes the relative path of a file with respect to a given directory Examples will make the point clear, so here goes: GetRelativePath ("C:\VB\", "C:\VB\File.ext") returns "File.ext" GetRelativePath ("C:\VB\", "C:\VB\Program\File.ext") returns "Program\File.ext" GetRelativePath ("C:\VB\", "C:\File.ext") returns "..\File.ext" It is useful to insert images and hyperlinks into webpages, given the filenames of the images and the HTML file.
AI
AI 요약: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.
소스 코드
Public Function GetRelativePath(sBase As String, sFile As String) '------------------------------------------------------------ ' Accepts : sBase= Fully Qualified Path of the Base Directory ' sFile= Fully Qualified Path of the File of which ' the relative path is to be computed. ' Returns : Relative Path of sFile with respect to sBase. ' Modifies: Nothing. '------------------------------------------------------------ ' Author : Manas Tungare (www.manastungare.com) '------------------------------------------------------------ Dim Base() As String, File() As String Dim I As Integer, NewTreeStart As Long, sRel As String If Left(sBase, 3) <> Left(sFile, 3) Then 'Since the files lie on different drives, the relative 'filename is same as the Absolute Filename GetRelativePath = sFile Exit Function End If Base = Split(sBase, "\") File = Split(sFile, "\") While Base(I) = File(I) I = I + 1 Wend If I = UBound(Base) Then 'Then the Base Path is over, and the file lies 'in a subdirectory of the base directory. 'So simply append the rest of the path. While I <= UBound(File) sRel = sRel + File(I) + "\" I = I + 1 Wend 'Now remove the extra trailing "\" we put earlier. GetRelativePath = Left(sRel, Len(sRel) - 1) Exit Function End If NewTreeStart = I 'The base path is not yet over, and we need to step 'back using the "..\" While I < UBound(Base) sRel = sRel & "..\" I = I + 1 Wend While NewTreeStart <= UBound(File) sRel = sRel & File(NewTreeStart) + "\" NewTreeStart = NewTreeStart + 1 Wend 'Now remove the extra trailing "\" we put earlier. GetRelativePath = Left(sRel, Len(sRel) - 1) End Function
원본 댓글 (3)
Wayback Machine에서 복구됨