5

I want to create a directory say TestDir but only when that directory does not exist. I don't find the way the check the existence of that directory.

I am using following function to create the directory.

CreateDir('TestDir')

How should I make sure that I use this CreateDir function only when TestDir does not exist?

4

3 に答える 3

19

In Delphi XE2, you can use the IOUtils unit TDirectory record, like this:

uses IOUtils;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if not TDirectory.Exists('test') then
    TDirectory.CreateDirectory('test');

In Delphi7, you can use the DirectoryExists function from the SysUtils unit:

uses SysUtils, Windows;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if not DirectoryExists('test') then
    CreateDir('test');
于 2013-02-19T06:43:59.017 に答える
11

SysUtilsと呼ばれるルーチンがあり、DirectoryExists必要なことを正確に実行する必要があります...

于 2013-02-19T06:42:13.760 に答える
2

CreateDir can only create directories that are one level "higher" then an existing directory. E.g., CreateDir('C:\Folder1\Folder2') only works if C:\Folder1 already exists, and likewise CreateDir('C:\F1\F2\F3') only works if C:\F1\F2 exists. For creating the "in-between" folders in one step, you use Delphi's ForceDirectories.

procedure TForm1.Button2Click(Sender: TObject);
begin
  if DirectoryExists(Edit1.Text) then
    ShowMessage(Edit1.Text + ' exists already')
  else begin
    ForceDirectories(Edit1.Text);
    if DirectoryExists(Edit1.Text) then
      ShowMessage('Folder created: ' + Edit1.Text)
    else
      ShowMessage('Could not create ' + Edit1.Text);
  end;
end;
于 2016-07-30T01:55:34.683 に答える