RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
SysUtils.ForceDirectories Function

Creates a new directory, including the creation of parent directories as needed.

Pascal
function ForceDirectories(Dir: string): Boolean;
C++
Boolean ForceDirectories(AnsiString Dir);

SysUtils

ForceDirectories creates a new directory as specified in Dir, which must be a fully-qualified path name. If the directories given in the path do not yet exist, ForceDirectories attempts to create them. 

ForceDirectories returns true if it successfully creates all necessary directories, false if it could not create a needed directory.

Warning: Important:Do not call ForceDirectories with an empty string. Doing so causes ForceDirectories to raise an exception.
Note: The FileCtrl unit (Windows only) also contains a ForceDirectories function. However, the FileCtrl version is deprecated, and the SysUtils version preferred, even if the code does not need to be cross-platform.
 

C++ Examples: 

 

/*
The following sample code sets the directory of
DirectoryListBox1 to c:\\WINDOWS when the form is created.
When Button1 is pressed, a subdirectory called
c:\\WINDOWS\\mytemp is added, but note that it is not updated
in DirectoryListBox1 until Button2 is pressed and Update is
called.
Note: A EInOutError will occur if you add a directory that
already exists.  Also, you must add the new directory to
your current directory to see the magic happen.  Also,
capitalization matters in C++ and the file spearators must
be "\\".
*/
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  ForceDirectories(Edit1->Text);
}

void __fastcall TForm1::Button2Click(TObject *Sender)
{
  DirectoryListBox1->Update();
}

void __fastcall TForm1::FormCreate(TObject *Sender)
{
  DirectoryListBox1->Directory = "c:\\WINDOWS";
}
/*
This example uses a label and a button on a form. When the
user clicks the button, all the directories along the
specified path that don't exist are created. The results are
reported in the caption of the label:
*/
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  AnsiString Dir = "C:\\Apps\\Sales\\Local";
  if (ForceDirectories(Dir))
    Label1->Caption = Dir + " was created";
}

 

Delphi Examples: 

{
This example uses a label and a button on a form. When the
user clicks the button, all the directories along the
specified path that don't exist are created. The results are
reported in the caption of the label:
} 
procedure TForm1.Button1Click(Sender: TObject);
var
  Dir: string;
begin
  Dir := 'C:\APPS\SALES\LOCAL';
  if SysUtils.ForceDirectories(Dir) then
    Label1.Caption := Dir + ' was created'
end;

 

Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!