Delphi Basics Home  |  Delphi Basics .NET Home  |  System.IO.DirectoryInfo  |  GetFiles Method
GetFiles  
Method  
Gets the files in the specified Directory
DirectoryInfo Class
System.IO NameSpace
CF1.  Function GetFiles : Array of FileInfo;
CF2.  Function GetFiles ( Filter : String ) : Array of FileInfo;
CF : Methods with this mark are Compact Framework Compatible
Description
The GetFiles method returns an array of FileInfo objects for all files in the current directory. Optionally, this list may be limited by the Filter string.
 
The array size is dynamically set by this method.
 
This filter string may contain valid file name characters, but may not have consecutive . characters. Use * wildcard to represent a sequence of 0 or more characters, and ? to represent a single character.
Microsoft MSDN Links
System.IO
System.IO.DirectoryInfo
System.IO.DirectoryInfo.GetFiles
 
An example of the 1st syntax
program Project1;
{$APPTYPE CONSOLE}

uses
  System.IO;

var
  DirInfo : System.IO.DirectoryInfo;
  Path  : String;
  Files : Array of FileInfo;
  i     : Integer;

begin
  // Get the current folder
  Path := System.IO.Directory.GetCurrentDirectory;
  Console.WriteLine('Files in : ' + Path + ' :');
  Console.WriteLine;

  // Create our directory info object
  DirInfo := System.IO.DirectoryInfo.Create(Path);

  // Get the files in this folder
  Files := DirInfo.GetFiles;

  // Display the files - just the file names that is
  for i := 0 to Length(Files)-1 do
    Console.WriteLine(Files[i].Name);

  Console.Readline;
end.
   Files in : C:\Documents and Settings\Thomas\My Documents\Borland Studio Projects :
  
  
   DelphiBasics.txt
   Project1.exe
   Project1.pdb
   Project1.rsp
An example of the 2nd syntax
program Project1;
{$APPTYPE CONSOLE}

uses
  System.IO;

var
  DirInfo : System.IO.DirectoryInfo;
  Path  : String;
  Files : Array of FileInfo;
  i     : Integer;

begin
  // Get the current folder
  Path := System.IO.Directory.GetCurrentDirectory;
  Console.WriteLine('Project Files in : ' + Path + ' :');
  Console.WriteLine;

  // Create our directory info object
  DirInfo := System.IO.DirectoryInfo.Create(Path);

  // Get the project files in this folder
  Files := DirInfo.GetFiles('Project*');

  // Display the files - just the file names that is
  for i := 0 to Length(Files)-1 do
    Console.WriteLine(Files[i].Name);

  Console.Readline;
end.
   Project Files in : C:\Documents and Settings\Thomas\My Documents\Borland Studio Projects :
  
   Project1.exe
   Project1.pdb
   Project1.rsp
 
© CodeGear 2006 - 2007. All rights reserved.