Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Correct Relative Path in Delphi

I am writing with Delphi 2009 a little app to edit HTML files. With

HypRef := '../../photos/myjpg.jpg'   
If FileExists(ExpandFileName(HypRef)) then ... 

I can find out, whether the file exists or not. Is there a function to find out the correct relative path, if FileExists gives a negative answer?

like image 334
GerhardS Avatar asked Nov 20 '25 02:11

GerhardS


1 Answers

I'm presuming you mean relative to the directory of the main HTML document. You can call SetCurrentDir() to the directory containing the main HTML document, or you can simply prepend that path to the relative one.

if FileExists(ExtractFilePath(MainDocument) + HypRef) then...

You really don't need to call ExpandFileName() since the OS will properly resolve the '..' and '.' pieces. However, if you intend to use the path for identity then they should all be cannonicalized using ExpandFileName().

like image 67
Allen Bauer Avatar answered Nov 22 '25 16:11

Allen Bauer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!