Back
 Lennie's Weekly Tip: Starting the Default ScreenSaver Programatically
Lennie's Weekly Tip : Does that Directory or File Exist?
21 June 2000 Ref. 26

Sometimes you may want to check whether a specified directory (folder) or whether a file exists on a user's computer, before you continue with a task eg. before copying a file to the specified directory.

You can now use the DirectoryExists & FileExists functions.

DIRECTORYEXISTS
===============

Declaration
-----------

function DirectoryExists(Name: string): Boolean;

Description
-----------

Call DirectoryExists to determine whether the directory specified by the Name parameter exists.
If the directory exists, the function returns True.
If the directory does not exist, the function returns False.

If a full path name is entered, DirectoryExists searches for the directory along the designated path.
Otherwise, the Name parameter is interpreted as a relative path name within the current directory.
 

FILEEXISTS
==========

Declaration
-----------

function FileExists(const FileName: string): Boolean;

Description
-----------

The FileExists function returns True if the file specified by FileName exists.
If the file does not exist, FileExists returns False.
 

Example
=======

uses
  SysUtils, FileCtrl;

begin
  if (DirectoryExists('C:\TEMP\')) then

    if (FileExists(C:\TEST.TXT)) then
      CopyFile('C:\TEST.TXT', 'C:\TEMP\TEST.TXT', FALSE)
    else
      Messagedlg('Error: File TEST.TXT doesn''t exist..', mterror, [mbok], 0)

  else
    Messagedlg('Error: Directory C:\TEMP\ doesn''t exist...', mterror, [mbok], 0);
end;

In this example we will first check to see if the directory (C:\TEMP\) and the file (C:\TEST.TXT) exists before using the CopyFile WinAPI function to copy the file. If the file or the directory doesn't exist, a error message will be displayed.
 

NOTE: Remember to add SysUtils & FileCtrl units to your application's uses-clause.
 

For more information read Delphi's on-line help.
 


Back