How to Create Multiple Directories

Posted on December 15th, 1999 in General | 1 Comment »

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+ have 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 FilesMy GameAudioSpeech’);

How to Get the Windows System Directory

Posted on December 15th, 1999 in System | No Comments »

To found out the name of the Windows directory it’s best to use the Windows API GetSystemDirectory() function.

Here’s the Delphi implementation:

uses Windows;
...
function GetSystemDir: string;
const
(* The length of the directory buffer. Usually 64 or even 16 is enough :-)
**
** Must be DWORD type.
*)
  dwLength: DWORD = 255;
var
  psSysDir: PChar;
begin
  GetMem(psSysDir, dwLength);
  GetSystemDirectory(psSysDir, dwLength);
  Result := string(psSysDir);
  FreeMem(psSysDir, dwLength);
end;