Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
500 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi



Loremo - the 1.5 liter car coming in 2009




Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (2)


Using Relative Paths for FilesGo to Toby Allen's websiteFormat this article printer-friendly!Bookmark function is only available for registered users!
Product:
Delphi all versions
Category:
Files Operation
Skill Level:
Scoring:
Last Update:
03/13/2002
Search Keys:
delphi delphi3000 article borland vcl code-snippet String-Functions File-Paths ExtractRelativePath ExpandFileName
Times Scored:
4
Visits:
776
Uploader: Toby Allen
Company: Toflidium Software
Reference: http://www.toflidium.com
 
Question/Problem/Abstract:
I know how to extract a relative file name for a path, but how do I then use this relative file name to open a file.
Answer:



If you want to display relative paths in your application, you can use the ExtractRelativePath function.


This takes two parameters the base path, and the file path you wish to make relative, and returns you the relative path.


eg.

  • MyAppPath := 'c:\program files\toflidium\MyApp\';
  • MyFilePath := 'c:\program files\toflidium\MyApp\Bin\MyFile.bin';
  • RelPath := ExtractRelativePath(MyAppPath, MyFilePath);


    RelPath will be "Bin\MyFile.bin"


    Now if you want to expand this again to a fully qualified file name, and you use ExpandFileName you would think you would get the same filename back again, but not necessarily.  This function uses the current directory so you might get back a path like "c:\my documents\bin\myfile.bin"



    Solution



    While I was playing with this, I noticed that if you add the relative path to the application path (or other path you used to make it relative) you get the path your looking for.


    Thats Obvious I hear you cry!


    Yes! However it also works for paths that move up the tree so


  • RelPath := ExtractRelativePath('C:\program files\toflidium\MyApp\',
    'C:\program files\Microsoft Office\Office\WordFile.dot');


    RelPath will be '..\..\Microsoft Office\Office\WordFile.dot'


    You can add this to your Path and use it as a normal file name and it works perfectly.


    ExpRelFile := 'C:\program files\toflidium\MyApp\..\..\Microsoft Office\Office\WordFile.dot'


    eg.

  • if fileexists(ExpRelFile ) then ....


    I dont know if this works on all versions of windows (I guess it does, because I'm sure it is based on DOS).

    Hope someone finds this useful.

    Toby Allen.  






  • Please rate this article!
    Skill level:
    BeginnerExpert

    Useful:
    No!Very!

    Overall rating:
    PoorExcellent



    Comments to this article
    Write a new comment
    ExtractFilePath
        Michael Trim (Mar 14 2002 3:33PM)

    You can use:
    ExtractFilePath(Application.EXEName);
    to get the application path.
    Respond

    RE: ExtractFilePath
    Toby Allen (Mar 14 2002 4:03PM)

    The whole point of the article is that you can use it for files that are not on the application path.
    Extractfilepath is fine for something off a directory of the application, but not a document in a sub path.
    eg.
    Development\MyApp\Help\Helpfile.txt
    Development\Otherfiles\Help\Helpfile.txt

    Extractfilepath works for the first, but no the second.

    Respond














     
    Sign up to consume product discounts for Bronze memberships !

    read more


      Visit our Sponsor

     

      Community Ad of
    E. Irigoyen
     
       














     







         
      Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
    delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)