1. Home
  2. Computing & Technology
  3. Delphi Programming
Delphi for .NET FCL Examples Reference
Directory class namespace
System.IO
Declaration
type Directory = class sealed;
Description
Exposes static methods for creating, moving, and enumerating through directories and subdirectories.
Example
program System_IO_Directory;

{$APPTYPE CONSOLE}
{$AUTOBOX ON}

uses System.IO, System.Collections;

type
  DirectoryTest = class
  public
    class procedure Main;
  end;

  TArrayOfString = array of string;
  ShowDirSizeTest = class
  public
    class function DirSize(d: DirectoryInfo): Int64;
    class procedure Main(args: TArrayOfString); 
  end;

{
  DirectoryTest

  The following example determines whether a
  specified directory exists, deletes it if
  it does, and creates it if it does not. This
  example then moves the directory, creates a
  file in the directory, and counts the
  files in the directory.
}

class procedure DirectoryTest.Main;
var
  target: string;
  path: string;
  files : TArrayOfString;
begin
  // Specify the directories you want to manipulate.
  path := 'c:\MyDir';
  target := 'c:\TestDir';

  try
    // Determine whether the directory exists.
    if (Directory.Exists(path) = False) then
      // Create the directory it does not exist.
      Directory.CreateDirectory(path);

    // Delete the target to ensure it is not there
    if Directory.Exists(target) then
      Directory.Delete(target, True);

    // Move the directory.
    Directory.Move(path, target);

    // Create a file in the directory
    System.File.CreateText((target + '\myfile.txt'));

    // Count the files in the target directory
    files := Directory.GetFiles(target);
    Console.WriteLine('The number of files in {0} is {1}',
                       target,
                       System.Array(files).Length);
  except
    on e: Exception do
      Console.WriteLine('The process failed: {0}', 
                        e.ToString);
  end;
end; //DirectoryTest

{
 ShowDirSizeTest

 The following example calculates the size of a directory
 and its subdirectories, if any, and displays the total size
 in bytes.
}

class function ShowDirSizeTest.DirSize
               (d: DirectoryInfo): Int64;
type
  TArrayOfFileInfo = array of FileInfo;
  TArrayOfDirectoryInfo = array of DirectoryInfo;
var
  enumerator: System.Collections.IEnumerator;
  di: DirectoryInfo;
  dis: TArrayOfDirectoryInfo;
  fi: FileInfo;
  fis: TArrayOfFileInfo;
  Size: Int64;
begin
  Size := 0;

  // Add file sizes.
  fis := d.GetFiles;
  enumerator := System.Array(fis).GetEnumerator;
  while enumerator.MoveNext do
  begin
    fi := FileInfo(enumerator.Current);
    Size := Size + fi.Length;
  end;

  // Add subdirectory sizes.
  dis := d.GetDirectories;
  enumerator := System.Array(dis).GetEnumerator;
  while enumerator.MoveNext do
  begin
    di := DirectoryInfo(enumerator.Current);
    Size := Size + DirSize(di);
  end;


  Result := Size;
end; //ShowDirSizeTest.DirSize

class procedure ShowDirSizeTest.Main(args: TArrayOfString);
var
  d: DirectoryInfo;
begin
  if (System.Array(args).Length <> 1) then
    Console.WriteLine('You must provide a directory
                       argument at the command line.')
  else
  begin
    d := DirectoryInfo.Create(args[0]);
    
    Console.WriteLine('The size of {0} and its 
                       subdirectories is {1} bytes.',
                      d, 
                      DirSize(d));
  end;
end; //ShowDirSizeTest.Main

//test both programs
begin
  DirectoryTest.Main;

  ShowDirSizeTest.Main(TArrayOfString.Create('c:\Fun'));
end.
Example output
The number of files in c:\TestDir is 1

The size of c:\Fun and its 
subdirectories is 2028032 bytes.

Looking for Delphi for .NET demos / source code samples? Look no further, Delphi for .NET FCL Examples Reference is what you are searching for!

Explore Delphi Programming
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming

©2009 About.com, a part of The New York Times Company.

All rights reserved.