Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I Create folders recursively in Delphi?

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');
like image 984
Andrew Rumm Avatar asked Jul 12 '10 18:07

Andrew Rumm


2 Answers

Use

ForceDirectories('C:\TestFolder\Another\AndAnother');

(This is a standard RTL function, found in SysUtils.pas. Hence you do not need to create your own function, even though that wouldn't have been difficult.)

like image 158
Andreas Rejbrand Avatar answered Sep 20 '22 16:09

Andreas Rejbrand


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)).

like image 29
Server Overflow Avatar answered Sep 20 '22 16:09

Server Overflow