Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
497 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 (7)


How to create a directory treeFormat this article printer-friendly!Bookmark function is only available for registered users!
Product:
Delphi 3.x (or higher)
Category:
Files Operation
Skill Level:
Scoring:
Last Update:
11/10/2000
Search Keys:
delphi delphi3000 article borland vcl code-snippet Create Directory Path
Times Scored:
5
Visits:
8414
Uploader: Marcelo Torres
Company: DBNet Consultoria Ltda
Reference: Marcelo Torres
 
Question/Problem/Abstract:
How to create all directories from a tree ?   Example: How to
create the path 'c:\data\year\2000', but the directory 'c:\data\year'
don't exist....
Answer:




procedure MakeDir(Dir: String);
  function Last(What: String; Where: String): Integer;
  var
    Ind : Integer;

  begin
    Result := 0;

    for Ind := (Length(Where)-Length(What)+1) downto 1 do
        if Copy(Where, Ind, Length(What)) = What then begin
           Result := Ind;
           Break;
        end;
  end;

var
  PrevDir : String;
  Ind     : Integer;

begin
  if Copy(Dir,2,1) <> ':' then
     if Copy(Dir,3,1) <> '\' then
        if Copy(Dir,1,1) = '\' then
           Dir := 'C:'+Dir
        else
           Dir := 'C:\'+Dir
     else
        Dir := 'C:'+Dir;

  if not DirectoryExists(Dir) then begin
     // if directory don't exist, get name of the previous directory

     Ind     := Last('\', Dir);         //  Position of the last '\'
     PrevDir := Copy(Dir, 1, Ind-1);    //  Previous directory

     // if previous directoy don't exist,
     // it's passed to this procedure - this is recursively...
     if not DirectoryExists(PrevDir) then
        MakeDir(PrevDir);

     // In thats point, the previous directory must be exist.
     // So, the actual directory (in "Dir" variable) will be created.
     CreateDir(Dir);
  end;
end;


Usage:
MakeDir('\data\year\2000');

or

MakeDir('D:\TEST\PROGRAMS\NOVEMBER\');





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
ForceDirectories
    Poul Lund (Nov 16 2000 7:07AM)

In Delphi 5 it is possible to use ForceDirectories and it works just fine
Respond

API
    Jérôme Tremblay (Nov 10 2000 1:31PM)

MakeSureDirectoryPathExists('c:\a\directory\that\might\not\exists');
Respond

RE: API
Marcelo Torres (Nov 16 2000 4:51AM)

Where is "MakeSureDirectoryPathExists" function ???

Respond

ForceDirectories
    Andreas Schmidt (Nov 10 2000 6:33AM)

function ForceDirectories(Dir: string): Boolean;
begin
  Result := True;
  if Length(Dir) = 0 then
    raise Exception.CreateRes(@SCannotCreateDir);
  Dir := ExcludeTrailingBackslash(Dir);
  if (Length(Dir) < 3) or DirectoryExists(Dir)
    or (ExtractFilePath(Dir) = Dir) then Exit; // avoid 'xyz:\' problem.
  Result := ForceDirectories(ExtractFilePath(Dir)) and CreateDir(Dir);
end;


Respond

RE: ForceDirectories
Marcelo Torres (Nov 16 2000 4:40AM)

I tested this function, but "ExcludeTrailingBackslash" function not
found.....  :-(

Respond

RE: RE: ForceDirectories
Joe White (Nov 16 2000 11:02AM)

ExcludeTrailingBackslash is new in Delphi 5. Its name is self-explanatory, though, and if you're not concerned about double-byte characters it's easy to code:

function ExcludeTrailingBackslash(Path: String): String;
begin
  if (Path <> '') and (Path[Length(Path)] = '\') then
    Result := Copy(Path, 1, Length(Path) - 1)
  else
    Result := Path;
end;
Respond

RE: RE: RE: ForceDirectories
Marcelo Torres (Nov 17 2000 12:15PM)

Yes. In Delphi 5, but this article was maded for Delphi 3.x

Thanks for your help....

Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
M. Maes
 
   














 







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