Home > General > How to Create Multiple Directories

How to Create Multiple Directories

December 15th, 1999 m3Rlin Leave a comment Go to comments

The standard MkDir() function can create only one directory, it can not create subdirectories at one time. This function allows you to create multiple directories (directories inside directories). Delphi 4+ has the ForceDirectories() routine which does the same thing. It is declared in the FileCtrl unit.

uses
  SysUtils, FileCtrl;
...
procedure MkDirMulti(sPath: string);
begin
  if sPath[Length(sPath)] = '' then
    sPath := Copy(sPath, 1, Length(sPath - 1));
  if (Length(sPath) < 3) or DirectoryExists(sPath) then
    Exit;
  MkDirMulti(SysUtils.ExtractFilePath(sPath));

  try
    MkDir(sPath);
  except
    { Handle errors }
  end;
end;

Example:

MkDirMulti('C:\\Program Files\\My Game\\Audio\\Speech');
  1. No comments yet.