Home / File Utils
Name Modified Size InfoDownloads / Week
Parent folder
ReadMe.md 2024-04-13 9.2 kB
RcsFileUtils.pas 2024-04-12 27.2 kB
Totals: 2 Items   36.4 kB 0

RCS_FileUtils

The purpose of this unit is to provide a number of file processing related untility routines.

Usage

The 'Interface' part of the unit looks as follows (the documentation is self explaining):

interface

uses SysUtils,
  Classes;

const AttrNotAllowedSpecial = 0; //faInvalid or faTemporary or faSymLink or faVirtual; // all others are allowed

type
  TIncludePath = (ipNone, ipRelative, ipFull);
  //TFileListMode = (mdMask, MdRegExp);

  TFileList =
    class(TList)
  private
    //TMode: TFileListMode;
    procedure DisposeFileList;
  public

    constructor Create;
    destructor Destroy; override;

    procedure MakeFileList(
      Fn: string;                                 // filename to find (rootdir is derived from it)
      AttrMustHave: Integer;                      // file attributes to find
      AttrNotAllowed: integer;                    // file attributes not allowed
      SubDirs: Boolean = false;                   // search in subdirs too
      IncludePath: TIncludePath = ipNone;         // include full path in the filenames found
      MaxNumber: Integer = MaxInt); overload;     // maximum number of entries in the list fo names found

    procedure MakeFileList(
      RootDir: string;                            // start directory
      Fn: string;                                 // filename to find
      AttrMustHave: Integer;                      // file attributes to find
      AttrNotAllowed: integer;                    // file attributes not allowed
      SubDirs: Boolean = false;                   // search in subdirs too
      IncludePath: TIncludePath = ipNone;         // include full path in the filenames found
      MaxNumber: Integer = MaxInt); overload;     // maximum number of entries in the list fo names found

    procedure MakeFileList(
      RootDir: string;                            // start directory
      FNames,                                     // list of filenames to find
      Excluded: Tstrings;                         // list of filenames to exclude
      AttrMustHave: Integer;                      // file attributes to find
      AttrNotAllowed: integer;                    // file attributes not allowed
      SubDirs: Boolean = false;                   // search in subdirs too
      IncludePath: TIncludePath = ipNone;         // include full path in the filenames found
      MaxNumber: Integer = MaxInt); overload;     // maximum number of entries in the list fo names found

    procedure AddToFileList(const Rec: TSearchrec);
    procedure GetEntry(const Index: Integer; var Rec: TSearchrec);
    procedure SortFileList;

    //property Mode: TFileListMode read TMode write TMode;
  end;

  {
  TFileList.MakeFileList:
   This procedure builds a list of 'TSearchRec' members, with attributes defined in the
   parameters. The "Attr" part of the TSearchRec members is extended with the extra
   attributes "faFile" and "faNormal".

   TFileList.MakeFileList parameters:
   - 'RootDir'     : The (top/root)directory the action starts in
                     if it is empty, an attempt is made to extract it from 'Fn',
                     if that fails then the current directory is used as RootDir
                     In functions/procedures where the parameter "RootDir" is not
                     present an attempt is made to derived from "Fn".
   - 'Fn'      (1) : The filename to be found, can contain wildcards
   - 'FNames'  (2) : a list of filenames to be found, can contain wildcards
   - 'Excluded'(2) : a list of filesnames (or directorynames) to exclude from the list
   - 'AttrMustHave : file attributes to find
   - 'AttrNotAllowed': file attributes not allowed
   - 'Check'       : Relaxed or strict checking of the file attributes (see the function 'Match')
   - 'SubDirs'     : True = also go into subdirs
   - 'IncludePath' : The path included in the filenames (see type 'TIncludepath')
   - 'MaxNumber'   : The maximum number of entries allowed in the list (default MaxInt)

      (1) and (2) are mutual exclusive
  }

{ FileNameExists
This function checks if file 'Fn' with attributes 'Attr' exists and returns its name.
If it does not exist, it returns ''.
All parameters have the same meaning as in 'TFileList.MakeFileList' (see above)
}
function FileNameExists(
  Fn: string;                                     // filename to check
  AttrMustHave: Integer;                          // file attributes to find
  AttrNotAllowed: integer;                        // file attributes not allowed
  SubDirs: Boolean = false;                       // search in subdirs too
  IncludePath: TIncludePath = ipNone              // include full path in the filenames found
  ): string;


{ GetFileNames
This procedure gets filenames in a TStrings object (like a TMemo.Lines).
The directory to start in is extracted from "Fn". If that fails then the
current directory is used as RootDir.
If 'RemoveExtension' is set to 'True', the extensions are removed from the filenames
All other parameters have the same meaning as in 'TFileList.MakeFileList' (see above)
}
procedure GetFileNames(
  Fn: string;                                     // filename to find (the rootdir is derived from it)
  AttrMustHave: Integer;                          // file attributes to find
  AttrNotAllowed: Integer;                        // file attributes not allowed
  RemoveExtension: Boolean;                       // the extention of the filenames found is to be removed in the list
  List: TStrings;                                 // the search results (found filenames)
  Subdirs: Boolean = false;                       // search in subdirs too
  IncludePath: TIncludePath = ipNone;             // include full path in the filenames found
  MaxNumber: Integer = MaxInt                     // maximum number of entries in the list fo names found
  ); overload;


{ GetFileNames
This procedure gets filenames in a TStrings object (like a TMemo.Lines).
The filename search action starts in "RootDir". If Rootdir is empty then an attempt
is made to extract RootDir from "Fn". If that fails then the
current directory is used as RootDir.
If 'RemoveExtension' is set to 'True', the extensions are removed from the filenames
All other parameters have the same meaning as in 'TFileList.MakeFileList' (see above)
}
procedure GetFileNames(
  RootDir: string;                                // start directory
  Fn: string;                                     // filename to find
  AttrMustHave: Integer;                          // file attributes to find
  AttrNotAllowed: integer;                        // file attributes not allowed
  RemoveExtension: Boolean;                       // the extention of the filenames found is to be removed in the List
  List: TStrings;                                 // the search results (found filenames)
  Subdirs: Boolean = false;                       // search in subdirs too
  IncludePath: TIncludePath = ipNone;             // include full path in the filenames found
  MaxNumber: Integer = MaxInt                     // maximum number of entries in the list fo names found
  ); overload;


{ GetFileNames
This procedure is identical to the above one, but one can give more paths to search
for in de TStrings variable 'FNames'.
Here if Rootdir is empty then the current directory is used as RootDir.
Additionally the files in "Excluded" are not in the filename list.
}
procedure GetFileNames(
  RootDir: string;                                // start directory
  FNames,                                         // list of filenames to find
  Excluded: Tstrings;                             // list of filenames to exclude
  AttrMustHave: Integer;                          // file attributes to find
  AttrNotAllowed: integer;                        // file attributes not allowed
  RemoveExtension: Boolean;                       // the extention of the filenames found is to be removed in the List
  List: TStrings;                                 // the search results (found filenames)
  Subdirs: Boolean = false;                       // search in subdirs too
  IncludePath: TIncludePath = ipNone;             // include full path in the filenames found
  MaxNumber: Integer = MaxInt                     // maximum number of entries in the list fo names found
  ); overload;


{ DirectoryNameExists
This function checks if "DirName" exists ("DirName" can contain wildcards in any
part of it) and returns its (with resolved wildcards) name (with or without trailing backslash).
If the directory "DirName" does not exist, the function returns an empty string.
}
function DirectoryNameExists(DirName: string; const IncludeBackSlash: Boolean =
  true): string;


{ ExpandToLongPathName
This functions returns the "long" pathname of "ShortPath", with or without trailing backslash.
See "ExtractShortPathName" (in SysUtils) for the opposite function.
}
function ExpandToLongPathName(ShortPath: string; const IncludeBackSlash: Boolean
  = true): string;


Have fun!

Source: ReadMe.md, updated 2024-04-13