Convert from Absolute to Relative Paths

Jim Ault JimAultWins at yahoo.com
Thu Jun 7 12:01:40 EDT 2007


I look at this task differently and offer it as an example to study to see
various ways Rev let's you accomplish the same thing.  David's solution will
make more sense to some and be easier to program, so they should follow that
direction.  My approach is to use the Rev chunk expressions to get to the
same result rather than repeat loops.  In this case, speed is not an issue
unless you are operating on thousands of paths, and then you should probably
take another sip of coffee to fill the time difference, if any.

My style appears below, and David's original appears below that

Jim Ault
Las Vegas

---start copy code
--correct for word wrap
function file_GetRelativePath someFileOrFolder, baseFolder, @commonPath
  if baseFolder is not in someFileOrFolder then return "Oooops, not going to
work"
  if char -1 of baseFolder is not "/" then put "/" after baseFolder --add
  if char -1 of someFileOrFolder is "/" then put "" after  baseFolder
--subtract
  
  replace baseFolder with empty in someFileOrFolder
  set the itemdelimiter to "/"
  put item 1 to -2 of someFileOrFolder & "/" into commonPath
  put the number of items in someFileOrFolder into relativeLevels
  
  get item -1 of someFileOrFolder
  set the itemDelimiter to tab
  put it into item relativeLevels of relativePath
  replace tab with "../" in relativePath

  return (someFileOrFolder &cr& baseFolder &cr& relativeLevels &cr&
relativePath)
end file_GetRelativePath
------ end of function

-----start of handler that calls the function
--using literal strings, so correct for word wrap
on testt
  put 
"/Volumes/mainHDG5/Users/jault/Documents/wbArea/wbSubDomains/laurabydesign/v
er01/page01/swfLoop01" into someFileOrFolder
  put "/Volumes/mainHDG5/Users/jault/Documents/wbArea/wbSubDomains/" into
baseFolder
  put empty into commonPath
  get file_GetRelativePath(someFileOrFolder, baseFolder, commonPath)
  put it & cr & commonPath
end testt
------end copy code

====== result of my version appears in message box ======
laurabydesign/ver01/page01/swfLoop01   <someFileOrFolder>
/Volumes/mainHDG5/Users/jault/Documents/wbArea/wbSubDomains/
4  <relativeLevels>
../../../swfLoop01   <relativePath>
laurabydesign/ver01/page01/   <commonPath>

---------------------------------------------


On 6/7/07 7:42 AM, "David Bovill" <david at openpartnership.net> wrote:

> function file_GetRelativePath someFileOrFolder, baseFolder, @commonPath
>     put the itemdelimiter into originalDelim
>     set the itemdelimiter to "/"
>     put empty into commonPath
>     put someFileOrFolder into relativePath
>     put 0 into itemNum
>     repeat with itemNum = 1 to the number of items of baseFolder
>         put item itemNum of baseFolder into basePathComponent
>         put item itemNum of someFileOrFolder into somePathComponent
> 
>         if somePathComponent is basePathComponent then
>             next repeat
>         else
>             subtract 1 from itemNum
>             exit repeat
>         end if
>     end repeat
> 
>     put item 1 to itemNum of someFileOrFolder into commonPath
>     delete item 1 to itemNum of relativePath
> 
>     delete item 1 to itemNum of baseFolder
>     put the number of items of baseFolder into upBits
>     repeat upBits
>         put "../" before relativePath
>     end repeat
> 
>     set the itemdelimiter to originalDelim
>     put "/" after commonpath
>     return relativePath
> end file_GetRelativePath
> _______________________________________________
> use-revolution mailing list
> use-revolution at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription
> preferences:
> http://lists.runrev.com/mailman/listinfo/use-revolution





More information about the use-livecode mailing list