How can I Create folders recursively in Delphi?

Viewed 8558

Need some help in creating function which can create folders recursively with giving path:

C:\TestFolder\Another\AndAnother

Delphi function MkDir returning IOerror = 3.

MkDir('C:\TestFolder\Another\AndAnother');
2 Answers

SysUtils is obsolete now and ForceDirectories is not UNC aware!

There is a new library in since Delphi XE7 (or even earlyer?) called IOUtils.
IOUtils is cross-platform compatible and UNC aware.

function ForceDirectories(FullPath: string): Boolean;   // Works with UNC paths
begin
  TDirectory.CreateDirectory(FullPath);
  Result:= DirectoryExists(FullPath);
end;

Note: The function is from Delphi LightSaber library. There are several other similar I/O functions there (like ListFilesOf(Folder)).

Related