Search code examples
stringdelphipathdelphi-7

Determine file/folder path based on root + web link?


Possible Duplicate:
Conversion between absolute and relative paths in Delphi

I'm trying to figure out how to get a file path based on an original web link. In my application, I have two values:

fRootDir: String = C:\Some Directory\My Web Site\ (Application directory)

fImgPath: String = ../Some Other Web Site/SomeImage.jpg (From a web page)

Result needs to be: C:\Some Directory\Some Other Web Site\SomeImage.jpg

Notice the ../ in front of the image path. This could be many in a row like ../../../ which each ../ means go up a folder. The image SomeImage.jpg is in fact a location at C:\Some Directory\Some Other Web Site\.

Also note that web links use / - which is not a problem to convert to \. The problem is with noticing .. and actually looking in the above directory for each.

Now I need to combine the two properties to result in a final path of where to find SomeImage.jpg on the computer, based on the root and the image paths.


Solution

  • Even simpler:

    const
      RootFolder = 'D:\PerforceThuis\Marjan\Probeersels\StackOverflow';
      RelativeFolder = '..\..\General\Plugins';
    begin
      WriteLn(ExpandFileName(IncludeTrailingBackSlash(RootFolder)+RelativeFolder));
    

    Tested in D6. Outputs:

    D:\PerforceThuis\Marjan\General\Plugins