EF.Sys.Windows
This unit gathers system-related routines and classes.<br />Services provided by this unit fall in these categories:<br />- File services. <br />- Process management services.<br />- Windows Registry access. <br />- Environment and localization.<br />- Security and network services.
TEFFileProcessor class
Abstract base class for classes that do things to a file or a set of files contained in a folder or in two parallel folder hierarchies.
property SourcePath: string read FSourcePath write FSourcePath;Locates the files to process. Operations that work on two folders (such as the copy operation) use both SourcePath and DestinationPath, while operations that work on one folder only (such as the delete operation) only need and use SourcePath.
property DestinationPath: string read FDestinationPath write FDestinationPath;Locates the files to process. Operations that work on two folders (such as the copy operation) use both SourcePath and DestinationPath, while operations that work on one folder only (such as the delete operation) only need and use SourcePath.
property RecurseSubdirs: Boolean read FRecurseSubdirs write FRecurseSubdirs default DEFAULT_RECURSE_SUBDIRS;When True, causes the object to process ASourcePath's subfolders recursively.
Setting this property to True is incompatible with any value for FileMask except '.'.
property FileMask: string read FFileMask write FFileMask;Set this property to filter certain file types only. Supports the wildcards * and ?.
property OnProcessFileError: TEFProcessFileErrorProc read FOnProcessFileError write FOnProcessFileError;This event is fired in case of errors, after whatever number of retries specified in RetryCount. The handler receives a reference parameter called Action which may be set to one of the following values:<br />eaRetry: Immediately retry the operation on the current file;<br />eaSkip: Skip the current file and process the next file; <br />eaAbort: Raise a silent exception (EAbort);<br />eaFail: Re-raise the original error.
property DefaultErrorAction: TEFFileErrorAction read FDefaultErrorAction write FDefaultErrorAction default DEFAULT_DEFAULT_ERROR_ACTION;Default action in case of errors. When OnProcessFileError is handled, this is the value passed to the handler, otherwise it's the value used to decide what to do.
property BeforeProcessFile: TEFBeforeProcessFileProc read FBeforeProcessFile write FBeforeProcessFile;Fired before processing each file. The handler receives the file name(s) in input.
property AfterProcessFile: TEFProcessFileProc read FAfterProcessFile write FAfterProcessFile;Fired after processing each file. The handler receives the file name(s) in input.
property RetryCount: Integer read FRetryCount write FRetryCount default DEFAULT_RETRY_COUNT;Indicates how many times a failed operation should be retried.
property RetryDelay: Integer read FRetryDelay write FRetryDelay default DEFAULT_RETRY_DELAY;Indicates how many milliseconds to wait between a try and the next retry in case of errors.
property Exceptions: TStrings read FExceptions write SetExceptions;A list of exclusion patterns. Any file whose name matches one of the exceptions is not processed.
TEFSourcePathOnlyFileProcessor class
Abstract base class for classes that do things to a file or a set of files contained in a single folder.
TEFFileDeleter class
Deletes a file or a group of files contained in a folder.
property BeforeDeleteDirectory: TEFDeleteDirectoryNotifyEvent read FBeforeDeleteDirectory write FBeforeDeleteDirectory;Fired before deleting a folder.
property AfterDeleteDirectory: TEFDeleteDirectoryNotifyEvent read FAfterDeleteDirectory write FAfterDeleteDirectory;Fired after a folder has been deleted.
property DeleteEmptyRootFolder: Boolean read FDeleteEmptyRootFolder write FDeleteEmptyRootFolder default DEFAULT_DELETE_EMPTY_ROOT_FOLDER;Set this property to True to cause the folder indicated by SourcePath to be deleted, but only if it remains empty after deleting all requested files and folders.
property DeleteEmptySubfolders: Boolean read FDeleteEmptySubfolders write FDeleteEmptySubfolders default DEFAULT_DELETE_EMPTY_SUBFOLDERS;Set this property to True to always delete a folder once all files in it have been deleted. Warning: if you set a FileMask that doesn't cause the deletion of all files, and set this property to True, you'll cause a potentially endless loop of OnProcessFileError events, since you can only delete a folder if it's empty.
procedure DeleteFiles;Deletes all files that match the settings.
procedure DeleteFile(const AFileName: string);Deletes a single file.
TEFFileChecker class
Makes sure that a file (or a group of files) is writable and is not in use. That is, it can be overwritten.
procedure CheckFiles;Checks all files that match the settings.
procedure CheckFile(const AFileName: string);Checks a single file.
TEFFileWriter class
Writes some text content to a file. Works one file at a time. An existing file is deleted and rewritten.
procedure WriteFile(const AFileName, AFileContent: string);Writes AContent to the specified file. If the file already exists it is deleted before writing.
TEFFileLister class
Lists all files of a given format (or array of formats) in a folder or folder hierarchy.
function ListFiles(const AFileNameList: TStrings;Adds the names of all files of the given formats to AFileNameList. If AFullPaths is True (or RecurseSubDirs is True) then the added names are full path names.
Parameters:
AFileNameList— List that will hold the file names.AFileFormats— An array of file formats, which are file extensions without the dot. The special file format '*', meaning all file formats, is supported.AFullPaths— Pass True to get the full path names, False for just the file names.
Returns: Returns the number of items added.
TEFSourceDestPathFileProcessor class
Abstract base class for classes that do things to a file or a set of files contained in two parallel folder hierarchies.
TEFFileCopier class
Copies a file or a group of files and folders.
procedure CopyFiles;Copies all files that match the settings.
procedure CopyFile(const ASourceFileName, ADestinationFileName: string);Copies a single file.
Routines
function IsFileInUse(const AFileName: string): Boolean;Tries to open the file AFileName for writing, and returns False in case of errors. Use it to know whether a file can be deleted/overwritten or not.
function GetFileCount(const APath: string): Integer;Returns the number of files in the given folder. APatch should be an absolute or relative path name, with or without trailing path delimiter.
function RemoveTrailingPathDelimiter(const APath: string): string;Removes a trailing path delimiter from APath, if present. It's the opposite of SysUtils.IncludeTrailingPathDelimiter.
function ExtractFileFormat(const AFileName: string): string;Extract the file format from AFileName. The file format is the file extension without the leading '.'.
procedure FindAllFiles(const AFileFormat, ARootPath: string;Appends to AFileNames the names of all files of the given format found in ARootPath. By default includes also the files found in ARootPath's subfolders.
This function is a wrapper around TEFFileLister. If you need more flexibility then use TEFFileLister directly, or write a different wrapper.
procedure FindAllFiles(const AFileFormats: array of string;Appends to AFileNames the names of all files of one of the given formats found in ARootPath. By default includes also the files found in ARootPath's subfolders.
This function is a wrapper around TEFFileLister. If you need more flexibility then use TEFFileLister directly, or write a different wrapper.
procedure EnumDirectories(const ARootPath: string;Calls the specified procedure once for each directory directly underneath the specified root path. This function is a wrapper around TEFFileLister. If you need more flexibility then use TEFFileLister directly, or write a different wrapper.
function IsDirectoryEmpty(const APath: string): Boolean;Returns True if the specified directory exists and is empty, False otherwise.
function ExecuteApplication(const AFileName: string;Executes an application synchronously and returns the application's exit code (or -1 in case of errors) and the console output. Use this function to execute console applications and command files. The application is run in hidden state, so that no console window is displayed.
function ExecuteApplication(const AFileName, AWorkingDirectory: string;Executes an application synchronously (AWait = True) or Asynchronously (AWait = False), with a specified working directory.
function ExecuteApplication(const AFileName: string;Executes an application asynchronously, but the process handle is not closed. Instead, it is retained and returned so that, for example, TerminateApplication can be called later. The caller is responsible for closing the process handle.
function TerminateApplication(const AProcessHandle: Cardinal): Boolean;Terminates the application and returns True. Returns False in case of an error.
procedure TrimProcessWorkingSet;Wraps SetProcessWorkingSetSize. See MSDN to know more about it. Call this function periodically, or once after application startup, to unmap all unused pages from the process' virtual memory space. This is the same thing that happens on NT-based systems when the main window of an application is minimized, and it is done to save memory.
procedure ShrinkProcessWorkingSet(const AMaxMemory: Cardinal);Tries to return as much memory as possible to the OS, if the current process' working set is greater than the specified number of bytes. This reduces memory fragmentation in long running processes, such as services. This function is inexpensive and it can be called very often. The cost of shrinking the memory set will only occurr when it is greater than the specified amount. Don't specify irrealistically lower amounts. Example: EmptyProcessWorkingSet(200 * 1024 *1024);
function RegDeleteKeyAndSubkeys(const AParentKey: TRegistry;Deletes a Registry key and all its subkeys. Returns False in case of errors. Returns True anyway if the key doesn't exist.
function GetMachineName: string;Returns the network name of the machine.
function SafeGetWindowsDirectory: string;Wraps GetWindowsDirectory. Raises exceptions in case of buffer overflows.
function SafeGetSystemDirectory: string;Wraps GetSystemDirectory. Raises exceptions in case of buffer overflows.
function GetTempDirectory: string;Return the current user's temporary directory, including the trailing path delimiter.
function GetProgramFilesx86Directory: string;Returns the full path of the Program Files(x86) directory, or the Program Files directory if the other does not exist.
function GetProgramFilesDirectory: string;Returns the full path of the Program Files directory.
procedure DeleteFile(const AFileName: string);Deletes the specified file. It is a simple wrapper around TEFFileDeleter. If you need more flexibility then use TEFFileDeleter directly or write a different wrapper.
procedure DeleteAllFiles(const APath: string);Deletes all files and folders in APath (but not APath itself). It is a simple wrapper around TEFFileDeleter. If you need more flexibility then use TEFFileDeleter directly or write a different wrapper.
procedure DeleteTree(const APath: string);Deletes APath with all files and folders therein. It is a simple wrapper around TEFFileDeleter. If you need more flexibility then use TEFFileDeleter directly or write a different wrapper.
procedure CopyFile(const ASourceFileName, ADestinationFileName: string);Copies the specified file. It is a simple wrapper around TFileCopier. If you need more flexibility then use TEFFileCopier directly or write a different wrapper.
procedure CopyAllFilesAndFolders(const ASourcePath, ADestinationPath: string;Copies all files and folders from ASourcePath to ADestinationPath. It is a simple wrapper around TFileCopier. If you need more flexibility then use TFileCopier directly or write a different wrapper.
procedure CopyAllFilesAndFoldersExcept( const ASourcePath, ADestinationPath: string;Copies all files and folders from ASourcePath to ADestinationPath, except those matching the patterns specified in AExceptions. It is a simple wrapper around TEFFileCopier. If you need more flexibility then use TFileCopier directly or write a different wrapper.
procedure CheckFileInUse(const AFileName: string);Checks that the specified file can be written to, otherwise raises an exception. It is a simple wrapper around TEFFileChecker. If you need more flexibility then use TEFFileChecker directly or write a different wrapper.
function SameDirectory(const ADirectory1, ADirectory2: string): Boolean;Returns True if two strings refer to the same directory. Both must exist for this function to work.
function GetCurrentProcessMemory: Cardinal;Returns the number of bytes in the current process' working set.
function GetResourceBytes(const AInstance: THandle;Loads a resource of the specified name and type from the specified instance handle, and returns the raw bytes. Returns nil if the resource is not found or is empty.
function GetRCDATAResourceBytes(const AInstance: THandle;Loads a resource of the specified name and type RCDATA from the specified instance handle, and returns the raw bytes. Returns nil if the resource is not found or is empty.
procedure GetVerInfo( const FileName : string;Returns information about version numbers (Major, Minor, Release, Build registered into FileName (usually application file)
function IsRunningAsService: Boolean;Tries to detect if the process is being run as a service by looking at services.exe as parent process and returns True if it is.
function IsInstallingService: Boolean;True if the install or uninstall command line switch was passed on the command line.
