Search code examples
delphidelphi-xe2delphi-7

How to check whether a directory exists or not in delphi XE2?


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?


Solution

  • 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');