Main Menu

Make Relative Path

Started by kevin, July 14, 2004, 01:29:10 PM

Previous topic - Next topic

kevin

This code was developed using PlayBasic (AlPHA) 2.12.  


PlayBASIC Code: [Select]
/*
--------------------------------------------------------------------- Make Relative Path
---------------------------------------------------------------------
This Function attempts to convert an absolute path, so that's relative
to the path which you supply.. As long as they are both on the same
device it should be able to handle it. Be warned though it's only
had a little testing..

Inputs:
CurrentPath$ = The Current Work folder.. or the folder
that you wish to make a path relative to.
Destpath$ = The Absolute path, including filename of

---------------------------------------------------------------------
*/



Function MakeRelativePath(Currentpath$,DestPath$)
; remove any unwanted "\" characters from the Current path$
CurrentPath$=TrimRight$(CurrentPath$,"\")

; Grab the Filename fromt he Dest apth
Filename$=GetFileName$(destpath$)
; remove the file from the destpath
DestPath$=CutRight$(DestPath$,Len(destPath$)-Len(filename$))
DestPath$=TrimRight$(destPath$,"\")
; Create the two temp arrays to hold, the split paths
Dim CurrentPath$(1)
Dim DestPath$(1)
; Split the paths to these arrays/ Split if resize them in need be.
CurrentTotal =SplitToArray(CurrentPath$,"/\",CurrentPath$(),0)
DestTotal =SplitToArray(DestPath$,"/\",Destpath$(),0); Print "Source path"; ShowStringArray(CurrentPAth$()); Print "Dest path"; ShowStringArray(DestPAth$())
; Compare Devices. if the devices are not the same.. We can't ; make this absolute path, relative to the current path.
If Lower$(CurrentPAth$(0))= Lower$(DestPAth$(0))

; Find shortest path.
If CurrentTotal<=DestTotal
TestFields=CurrentTotal
Else
TestFields=DestTotal
EndIf

; Locate Matching Folders
For MatchingFolders=0 To TestFields
If Lower$(CurrentPAth$( MatchingFolders))<> Lower$(DestPAth$( MatchingFolders))
Exit
EndIf
Next

; Make Back Step
For lp=MatchingFolders To CurrentTotal-1
NewPAth$=Newpath$+"..\"
Next
newpath$=TrimRight$(newpath$,"\")

; Add remaining dest folders
For lp=MatchingFolders To DestTotal
NewPAth$=Newpath$+"\"+DestPAth$(lp)
Next

; trim up the result and append the filename$
DestPath$=Trim$(newpath$,"\")+"\"+Filename$
EndIf ; Clip possible case of "\filename.type"
destPath$=Trim$(Destpath$,"\")

EndFunction DestPath$




kevin



   GetAbsolutePathToFile


     This function takes a base path and then computes the absolute path from this base from an assumed relative path in the file name





PlayBASIC Code: [Select]
   BasePath$="D:\MyProjectFolder\Gfx\_examples"
ink $ff0000
print BasePath$
ink -1

print ""
print GetAbsolutePathToFile(BasePath$, "Stuff.txt")
print GetAbsolutePathToFile(BasePath$, "subfolder/Stuff.txt")
print GetAbsolutePathToFile(BasePath$, "../Stuff.txt")
print GetAbsolutePathToFile(BasePath$, "../../Stuff.txt")
print GetAbsolutePathToFile(BasePath$, "subfolder/../../Stuff.txt")



Sync
waitkey


Function GetAbsolutePathToFile(BasePath$, Filename$)

BasePath$=replace$(BasePath$,"/","\")
Filename$=replace$(Filename$,"/","\")

Filename$=trim$(Filename$)
BasePath$=trimright$(BasePath$,"\")+"\"

local Device$=GetDevicename$(Filename$)
if len(Device$)=0

// if the Filename is relative, we'll try and
// build an absolute path from BASE to this Filename
local Path$=GetFolderName$(Filename$)

FileName$=BasePath$+Filename$

// Check for "..\" in path
if instring(Path$,"..\")

Dim FolderChunks$(50)
local Count=splittoarray(filename$,"\",FolderChunks$())

local lp,s$
local Output=0


for lp =0 to count-1
s$=FolderChunks$(lp)
if s$<>".."
if output>=0 then FolderChunks$(Output)=s$
Output++
else
output--
endif
next

// Put it back together
Filename$=""
for lp =0 to output-1
Filename$+=FolderChunks$(lp)
if lp<output-1 then Filename$+="\"
next

endif

endif


EndFunction Filename$











    Example output:


D:\MyProjectFolder\Gfx\_examples

D:\MyProjectFolder\Gfx\_examples\Stuff.txt
D:\MyProjectFolder\Gfx\_examples\subfolder\Stuff.txt
D:\MyProjectFolder\Gfx\Stuff.txt
D:\MyProjectFolder\Stuff.txt
D:\MyProjectFolder\Gfx\Stuff.txt