Rx File Utilities

RX Library

UNIT FileUtil
Routine
ClearDir
CopyFile
DeleteFiles
DirExists
FileDateTime
FileLock
FileUnlock
GetFileSize
HasAttr
LongToShortFileName
LongToShortPath
MoveFile
NormalDir
ShortToLongFileName
ShortToLongPath
ValidFileName


Routine ClearDir
Declaration: function ClearDir(const Path: string; Delete: Boolean): Boolean;

The ClearDir function erases all files from directory named by Path from the disk. When Delete parameter is True the directory Path also will be erased. If fiels or path cannot be deleted or does not exist, the function returns False.

ClearDir example:

if ClearDir('c:\temp', True) then
__ShowMessage('Directory C:\TEMP deleted')
else
__ShowMessage('Can''t delete directory C:\TEMP');


Routine CopyFile
Declaration: procedure CopyFile(const FileName, DestName: string; ProgressControl: TControl);

CopyFile copies the file specified by FileName parameter to the new file specified by DestName parameter.

When ProgressControl is not nil, the control specified by ProgressControl (TGauge in 16-bit version or TProgressBar in 32-bit version) will be display the percentage of file copying progress.

CopyFile example:
CopyFile('c:\work\unit1.pas', 'd:\test\unit1.pas', Gauge);


Routine DeleteFiles
Declaration: function DeleteFiles(const FileMask: string): Boolean;

The DeleteFiles function erases all file specified by FileMask from the disk. If files cannot be deleted or does not exist, the function returns False.

DeleteFiles example:
DeleteFiles('work\myproj\*.pas');


Routine DirExists
Declaration: function DirExists(Name: string): Boolean;

The DirExists function determines whether the directory specified as the value of the Name parameter exists. If the directory exists, the function returns True. If the directory does not exist, the function returns False. If only a directory name is entered as the value of Name, DirExists searches for the directory within the current directory. If a full path name is entered, DirExists searches for the directory along the designated path.

DirExists example:
This example uses an edit box, a label, and a button on a form. When the user enters a directory name in the edit box and clicks the button, whether or not the directory exists is reported in the caption of the label:

procedure TForm1.Button1Click(Sender: TObject);
begin
__if DirExists(Edit1.Text) then
____Label1.Caption := Edit1.Text + ' exists'
__else
____Label1.Caption := Edit1.Text + ' does not exist';
end;


Routine FileDateTime
Declaration: function FileDateTime(const FileName: string): TDateTime;

FileDateTime returns the date-and-time of the specified file.

FileDateTime example:
D1 := FileDateTime('unit1.pas');


Routine FileLock
Declaration: function FileLock(Handle: Integer; Offset, LockSize: Longint): Integer;

The FileLock function locks a region in an open file. Locking a region prevents other processes from accessing the region. Handle identifies the file with a region to be locked.

Offset specifies the starting byte offset in the file where the lock should begin.

LockSize specifies the length of the byte range to be locked.

FileLock example:
var FileHandle : Integer;
begin
__FileHandle := FileOpen(FileName, fmOpenWrite or fmShareDenyNone);
__if FileHandle > 0 then
__try
____{ valid file handle }
____if FileLock(FileHandle, 0, 1024) = 0 then
____try
______{ first 1024 bytes of opened file is locked }
____finally
______FileUnlock(FileHandle, 0, 1024);
____end;
__finally
____FileClose(FileHandle);
__end else
__{Open error: FileHandle = negative DOS error code};
end;


Routine FileUnlock
Declaration: function FileUnlock(Handle: Integer; Offset, LockSize: Longint): Integer;

The FileUnlock function unlocks a region in an open file. Unlocking a region enables other processes to access the region. Handle identifies a file that contains a region locked with FileLock.

Offset specifies the starting byte offset in the file where the locked region begins. LockSize specifies the length of the byte range to be unlocked.

FileUnlock example:
var
__FileHandle : Integer;
begin
__FileHandle := FileOpen(FileName, fmOpenWrite or fmShareDenyNone);
__if FileHandle > 0 then
__try
____{ valid file handle }
____if FileLock(FileHandle, 0, 1024) = 0 then
____
try
______{ first 1024 bytes of opened file is locked }
____finally
______FileUnlock(FileHandle, 0, 1024);
____end;
__finally
____FileClose(FileHandle);
__end else
____{Open error: FileHandle = negative DOS error code};
end;


Routine GetFileSize
Declaration: function GetFileSize(const FileName: string): Longint;

GetFileSize returns the size of a file, specified by FileName parameter, in bytes.

GetFileSize example:
FSize := GetFileSize('c:\utils\arj.exe');


Routine HasAttr
Declaration: function HasAttr(const FileName: string; Attr: Integer): Boolean;

HasAttr returns True if specified file has attributes Attr.

HasAttr example:
if HasAttr('c:\my_file.exe', faReadOnly) then
__raise Exception.Create('Can't delete read-only file');


Routine LongToShortFileName
Declaration: function LongToShortFileName(const LongName: string): string;

Use this procedure to obtain the short version of the file name specified by LongName parameter.

LongToShortFileName example:
FileName := LongToShortFileName(FileName);


Routine LongToShortPath
Declaration: function LongToShortPath(const LongName: string): string;

Use this procedure to obtain the short version of the directory name specified by LongName parameter.

LongToShortPath example:
DirectoryName := LongToShortPath(DirectoryName);


Routine MoveFile
Declaration: procedure MoveFile(const FileName, DestName: TFileName);

Moves or renames the file passed in FileName to the directory specified as part of DestName parameter.

Tries to just rename the file. If that fails, try to copy the file and delete the original.

Raises an exception if the source file is read-only, and therefore cannot be deleted/moved/renamed.

MoveFile example:
MoveFile(FileName, ChangeFileExt(FileName, '.BAK'));


Routine NormalDir
Declaration: function NormalDir(const DirName: string): string;

Add a default backslash '/' to the end of a directory name DirName. If DirName already has '/' symbol at the end, this function does nothing.

NormalDir example:
__{...}
__Result := NormalDir(DirName) + ExtractFileName(MyFileName);
__{...}


Routine ShortToLongFileName
Declaration: function ShortToLongFileName(const ShortName: string): string;

Use this procedure to obtain the long version of the file name specified by ShortName parameter.

ShortToLongFileName example:
FileName := ShortToLongFileName(FileName);


Routine ShortToLongPath
Declaration: function ShortToLongPath(const ShortName: string): string;

Use this procedure to obtain the long version of the directory name specified by ShortName parameter.

ShortToLongPath example:
DirectoryName := ShortToLongPath(DirectoryName);


Routine ValidFileName
Declaration: function ValidFileName(const FileName: string): Boolean;

ValidFileName indicates whether FileName string refers to a valid name of physical file.

ValidFileName example:
if not ValidFileName(S) then
__ShowMessage('Invalid file name');


Index Page | About | Download
Creation Date: 4 Feb 1998 | Last Update: 16 Mar 2000