Home  |  Delphi .net Home  |  System.IO.Directory  |  GetFiles Method
GetFiles  
Method  
Gets the files in the specified Directory
Directory Class
System.IO NameSpace
CF1.  Function GetFiles ( PathString : String; ) : Array of String;
CF2.  Function GetFiles ( PathString:StringPathString : String; Filter : String; ) : Array of String; Static;
CF : Methods with this mark are Compact Framework Compatible
Description
The GetFiles method returns an array of strings that hold the absolute paths of the files in the specified absolute or relative PathString 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.Directory
 
 
An example of the 1st syntax
program Project1;
{$APPTYPE CONSOLE}

uses
  System.IO;

var
  Path  : String;
  Files : Array of String;
  i     : Integer;

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

  // Get the files in this folder
  Files := System.IO.Directory.GetFiles(Path);

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

  Console.Readline;
end.
Show full unit code
  Files in : C:\Documents and Settings\Neil\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
  Path  : String;
  Files : Array of String;
  i     : Integer;

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

  // Get all project files in this folder
  Files := System.IO.Directory.GetFiles(Path, 'Project*');

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

  Console.Readline;
end.
Show full unit code
  Project Files in : C:\Documents and Settings\Neil\My Documents\Borland Studio Projects :
  
  Project1.exe
  Project1.pdb
  Project1.rsp
 
 
Delphi Programming © Neil Moffatt All rights reserved.  |  Contact the author