You've already forked lina-components
mirror of
https://bitbucket.org/Dennis07/lina-components.git
synced 2025-08-24 21:49:04 +02:00
Version 1.0 DEV 1.07
Signed-off-by: Dennis07 <den.goehlert@t-online.de>
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -8,14 +8,9 @@ unit uFileTools;
|
||||
|
||||
interface
|
||||
|
||||
{ Da Delphi vor Version 2009 noch keine Generics kannte, musste die TWinFiles-
|
||||
Deklaration auf Compiler-Versionen 20.0 oder h�her beschr�nkt werden.
|
||||
Meines Wissens nach gibt es keine einfache M�glichkeit, Typisierte Listen
|
||||
auch in �ltere Versionen zu integrieren. }
|
||||
|
||||
uses
|
||||
{ Standard-Units }
|
||||
Classes, SysUtils, ShellAPI, Forms, Winapi.Windows, Dialogs,
|
||||
Classes, SysUtils, ShellAPI, Forms, Windows, Dialogs,
|
||||
{$IFNDEF NO_GENERIC}
|
||||
Generics.Collections,
|
||||
{$ENDIF}
|
||||
@@ -24,6 +19,7 @@ uses
|
||||
|
||||
type
|
||||
{ Fehlermeldungen }
|
||||
EDriveNoExist = class(Exception);
|
||||
EFileNoExist = class(Exception);
|
||||
EMissingTypeDesc = class(Exception);
|
||||
EMissingExts = class(Exception);
|
||||
@@ -33,36 +29,53 @@ type
|
||||
{ Hilfsklassen }
|
||||
TFileExecuteMode = (feOpen,feEdit,feExplore,feFind,fePrint);
|
||||
TFileNameStyles = set of (fnDirectory,fnExtension);
|
||||
TFileAttributes = set of (faReadOnly,faHidden,faSystem,faArchive,faTemporary);
|
||||
|
||||
type
|
||||
{ Hauptklassen }
|
||||
TWinDrive = class
|
||||
private
|
||||
{ Private-Deklarationen }
|
||||
FDriveChar: Char;
|
||||
public
|
||||
{ Public-Deklarationen }
|
||||
constructor Create(ADriveChar: Char);
|
||||
destructor Destroy; override;
|
||||
property DriveChar: Char read FDriveChar;
|
||||
function GetSpaceTotal: Int64;
|
||||
function GetSpaceFree: Int64;
|
||||
end;
|
||||
|
||||
TWinFile = class
|
||||
private
|
||||
{ Private-Deklarationen }
|
||||
FFileName: String;
|
||||
FExecuteMode: TFileExecuteMode;
|
||||
FDrive: TWinDrive;
|
||||
public
|
||||
{ Public-Deklarationen }
|
||||
constructor Create(AFileName: String);
|
||||
destructor Destroy;
|
||||
destructor Destroy; override;
|
||||
property FileName: String read FFileName;
|
||||
property ExecuteMode: TFileExecuteMode read FExecuteMode write FExecuteMode;
|
||||
property Drive: TWinDrive read FDrive write FDrive;
|
||||
function GetExtension(WithDot: Boolean = True): String; //ExtractFileExt()
|
||||
function GetPath: String; //ExtractFilePath()
|
||||
function GetDir: String; //ExtractFileDir()
|
||||
function GetFileName(WithExt: Boolean = True): String; //ExtractFileName()
|
||||
function GetFolderName: String; //ExtractFileFolder()
|
||||
// GetSize: Int64
|
||||
// GetVersion: Extended
|
||||
// GetAttribute: ...
|
||||
function GetSize: Int64; //GetFileSize()
|
||||
// GetVersion: Extended //GetFileVerion()
|
||||
function GetAttributes: TFileAttributes; //GetFileAttributes();
|
||||
// GetOwner: String
|
||||
// GetCreated: TDateTime
|
||||
// GetModified: TDateTime
|
||||
function GetModified: TDateTime; //GetFileModified()
|
||||
function GetCreated: TDateTime; //GetFileCreated()
|
||||
function GetAccessed: TDateTime; //GetFileAccessed()
|
||||
function Execute: Boolean; //ExecuteFile()
|
||||
function SafeExecute: Boolean; //.............
|
||||
end;
|
||||
|
||||
TWinFiles = array of TWinFile;
|
||||
TWinFileArray = array of TWinFile;
|
||||
|
||||
{$IFNDEF NOGENERIC}
|
||||
TWinFileList = TList<TWinFile>;
|
||||
@@ -74,9 +87,17 @@ type
|
||||
InDir: Boolean = False): Boolean;
|
||||
function ExtractFileFolder(FileName: String): String;
|
||||
procedure ListFiles(Dir: String; OutList: TStrings; FileExts: array of String;
|
||||
NameStyles: TFileNameStyles = []); overload;
|
||||
NameStyles: TFileNameStyles = []);
|
||||
procedure ListFolders(Dir: String; OutList: TStrings;
|
||||
NameStyles: TFileNameStyles = []); overload;
|
||||
NameStyles: TFileNameStyles = []);
|
||||
function ExtractDriveChar(const FileName: String): Char;
|
||||
function DriveCharToFileDir(DriveChar: Char): ShortString;
|
||||
function DriveCharToFilePath(DriveChar: Char): ShortString;
|
||||
function GetFileSize(FileName: String): Int64;
|
||||
function GetFileModified(FileName: String): TDateTime;
|
||||
function GetFileCreated(FileName: String): TDateTime;
|
||||
function GetFileAccessed(FileName: String): TDateTime;
|
||||
function GetFileAttributes(FileName: String): TFileAttributes;
|
||||
|
||||
//const
|
||||
{ Dateierweiterungen f�r ListFiles() }
|
||||
@@ -91,8 +112,6 @@ type
|
||||
|
||||
implementation
|
||||
|
||||
{ TWinFile }
|
||||
|
||||
function FEModeToPChar(FEMode: TFileExecuteMode): PChar;
|
||||
begin
|
||||
case FEMode of
|
||||
@@ -212,10 +231,230 @@ begin
|
||||
Result := ExtractFileName(ExtractFileDir(FileName)); //Name d. �bergeord. Ordners
|
||||
end;
|
||||
|
||||
function ExtractDriveChar(const FileName: String): Char;
|
||||
begin
|
||||
Result := FileName[1];
|
||||
if Length(FileName) >= 2 then
|
||||
begin
|
||||
if FileName[2] <> DriveDelim then
|
||||
begin
|
||||
Result := #0;
|
||||
end else
|
||||
begin
|
||||
if Length(FileName) >= 3 then
|
||||
begin
|
||||
if FileName[3] <> PathDelim then
|
||||
begin
|
||||
Result := #0;
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
|
||||
function DriveCharToFileDir(DriveChar: Char): ShortString;
|
||||
begin
|
||||
Result := DriveChar + DriveDelim;
|
||||
end;
|
||||
|
||||
function DriveCharToFilePath(DriveChar: Char): ShortString;
|
||||
begin
|
||||
Result := DriveCharToFileDir(DriveChar) + PathDelim;
|
||||
end;
|
||||
|
||||
function FileTimeToDateTime(FileTime: TFileTime): TDateTime;
|
||||
var
|
||||
LocalTime: TFileTime;
|
||||
SysTime: TSystemTime;
|
||||
begin
|
||||
FileTimeToLocalFileTime(FileTime,LocalTime);
|
||||
FileTimeToSystemTime(LocalTime,SysTime);
|
||||
Result := SystemTimeToDateTime(SysTime);
|
||||
end;
|
||||
|
||||
function GetFileSize(FileName: String): Int64;
|
||||
var
|
||||
FileHandle: THandle;
|
||||
FindData: TWIN32FINDDATA;
|
||||
begin
|
||||
Result := -1;
|
||||
if FileExists(FileName) then
|
||||
begin
|
||||
FileHandle := FindFirstFile(pchar(FileName),FindData);
|
||||
if FileHandle = INVALID_HANDLE_VALUE then
|
||||
begin
|
||||
Exit;
|
||||
end;
|
||||
Result := (FindData.nFileSizeHigh * (MAXDWORD)) + FindData.nFileSizeLow;
|
||||
Windows.FindClose(FileHandle);
|
||||
end;
|
||||
end;
|
||||
|
||||
function GetFileModified(FileName: String): TDateTime;
|
||||
var
|
||||
FileHandle: THandle;
|
||||
FindData: TWIN32FINDDATA;
|
||||
begin
|
||||
Result := StrToTime('00.00.0000 00:00:00');
|
||||
if FileExists(FileName) then
|
||||
begin
|
||||
FileHandle := FindFirstFile(PChar(FileName),FindData);
|
||||
if FileHandle = INVALID_HANDLE_VALUE then
|
||||
begin
|
||||
Exit;
|
||||
end;
|
||||
Result := FileTimeToDateTime(FindData.ftLastWriteTime);
|
||||
Windows.FindClose(FileHandle);
|
||||
end;
|
||||
end;
|
||||
|
||||
function GetFileCreated(FileName: String): TDateTime;
|
||||
var
|
||||
FileHandle: THandle;
|
||||
FindData: TWIN32FINDDATA;
|
||||
begin
|
||||
Result := StrToTime('00.00.0000 00:00:00');
|
||||
if FileExists(FileName) then
|
||||
begin
|
||||
FileHandle := FindFirstFile(PChar(FileName),FindData);
|
||||
if FileHandle = INVALID_HANDLE_VALUE then
|
||||
begin
|
||||
Exit;
|
||||
end;
|
||||
Result := FileTimeToDateTime(FindData.ftCreationTime);
|
||||
Windows.FindClose(FileHandle);
|
||||
end;
|
||||
end;
|
||||
|
||||
function GetFileAccessed(FileName: String): TDateTime;
|
||||
var
|
||||
FileHandle: THandle;
|
||||
FindData: TWIN32FINDDATA;
|
||||
begin
|
||||
Result := StrToTime('00.00.0000 00:00:00');
|
||||
if FileExists(FileName) then
|
||||
begin
|
||||
FileHandle := FindFirstFile(PChar(FileName),FindData);
|
||||
if FileHandle = INVALID_HANDLE_VALUE then
|
||||
begin
|
||||
Exit;
|
||||
end;
|
||||
Result := FileTimeToDateTime(FindData.ftLastAccessTime);
|
||||
Windows.FindClose(FileHandle);
|
||||
end;
|
||||
end;
|
||||
|
||||
function GetFileAttributes(FileName: String): TFileAttributes;
|
||||
var
|
||||
FileHandle: THandle;
|
||||
FindData: TWIN32FINDDATA;
|
||||
begin
|
||||
Result := [];
|
||||
if FileExists(FileName) then
|
||||
begin
|
||||
FileHandle := FindFirstFile(PChar(FileName),FindData);
|
||||
if FileHandle = INVALID_HANDLE_VALUE then
|
||||
begin
|
||||
Exit;
|
||||
end;
|
||||
if (FindData.dwFileAttributes and FILE_ATTRIBUTE_READONLY) = FILE_ATTRIBUTE_READONLY then
|
||||
begin
|
||||
Result := Result + [faReadOnly];
|
||||
end;
|
||||
if (FindData.dwFileAttributes and FILE_ATTRIBUTE_HIDDEN) = FILE_ATTRIBUTE_HIDDEN then
|
||||
begin
|
||||
Result := Result + [faHidden];
|
||||
end;
|
||||
if (FindData.dwFileAttributes and FILE_ATTRIBUTE_SYSTEM) = FILE_ATTRIBUTE_SYSTEM then
|
||||
begin
|
||||
Result := Result + [faSystem];
|
||||
end;
|
||||
if (FindData.dwFileAttributes and FILE_ATTRIBUTE_ARCHIVE) = FILE_ATTRIBUTE_ARCHIVE then
|
||||
begin
|
||||
Result := Result + [faArchive];
|
||||
end;
|
||||
if (FindData.dwFileAttributes and FILE_ATTRIBUTE_TEMPORARY) = FILE_ATTRIBUTE_TEMPORARY then
|
||||
begin
|
||||
Result := Result + [faTemporary];
|
||||
end;
|
||||
Windows.FindClose(FileHandle);
|
||||
end;
|
||||
end;
|
||||
|
||||
{ ----------------------------------------------------------------------------
|
||||
TWinDrive
|
||||
---------------------------------------------------------------------------- }
|
||||
|
||||
constructor TWinDrive.Create(ADriveChar: Char);
|
||||
begin
|
||||
FDriveChar := ADriveChar;
|
||||
if DirectoryExists(ADriveChar) = False then
|
||||
begin
|
||||
raise EDriveNoExist.Create('Drive not found: "' + DriveChar + '"');
|
||||
end;
|
||||
end;
|
||||
|
||||
destructor TWinDrive.Destroy;
|
||||
begin
|
||||
//...
|
||||
inherited;
|
||||
end;
|
||||
|
||||
function TWinDrive.GetSpaceTotal: Int64;
|
||||
var
|
||||
RootChars: array[0..4] of Char;
|
||||
Root: PChar;
|
||||
Dir: String;
|
||||
Buffer: Int64;
|
||||
begin
|
||||
RootChars[0] := FDriveChar;
|
||||
RootChars[1] := DriveDelim;
|
||||
RootChars[2] := PathDelim;
|
||||
RootChars[3] := #0;
|
||||
Root := RootChars;
|
||||
Dir := GetCurrentDir;
|
||||
if SetCurrentDir(DriveCharToFilePath(FDriveChar)) then
|
||||
begin
|
||||
GetDiskFreeSpaceEx(Root,Buffer,Result,nil);
|
||||
SetCurrentDir(Dir);
|
||||
end else
|
||||
begin
|
||||
Result := -1;
|
||||
end;
|
||||
end;
|
||||
|
||||
function TWinDrive.GetSpaceFree: Int64;
|
||||
var
|
||||
RootChars: array[0..4] of Char;
|
||||
Root: PChar;
|
||||
Dir: String;
|
||||
Buffer: Int64;
|
||||
begin
|
||||
RootChars[0] := FDriveChar;
|
||||
RootChars[1] := DriveDelim;
|
||||
RootChars[2] := PathDelim;
|
||||
RootChars[3] := #0;
|
||||
Root := RootChars;
|
||||
Dir := GetCurrentDir;
|
||||
if SetCurrentDir(DriveCharToFilePath(FDriveChar)) then
|
||||
begin
|
||||
GetDiskFreeSpaceEx(Root,Result,Buffer,nil);
|
||||
SetCurrentDir(Dir);
|
||||
end else
|
||||
begin
|
||||
Result := -1;
|
||||
end;
|
||||
end;
|
||||
|
||||
{ ----------------------------------------------------------------------------
|
||||
TWinFile
|
||||
---------------------------------------------------------------------------- }
|
||||
|
||||
constructor TWinFile.Create(AFileName: String);
|
||||
begin
|
||||
ExecuteMode := feOpen;
|
||||
FFileName := AFileName;
|
||||
Drive := TWinDrive.Create(ExtractDriveChar(FFileName));
|
||||
if FileExists(AFileName) = False then
|
||||
begin
|
||||
raise EFileNoExist.Create('File not found: "' + AFileName + '"');
|
||||
@@ -224,7 +463,8 @@ end;
|
||||
|
||||
destructor TWinFile.Destroy;
|
||||
begin
|
||||
ExecuteMode := feOpen;
|
||||
Drive.Free;
|
||||
inherited;
|
||||
end;
|
||||
|
||||
function TWinFile.GetExtension(WithDot: Boolean = True): String;
|
||||
@@ -254,6 +494,11 @@ begin
|
||||
Result := ExtractFileFolder(FFileName);
|
||||
end;
|
||||
|
||||
function TWinFile.GetSize: Int64;
|
||||
begin
|
||||
Result := GetFileSize(FFileName);
|
||||
end;
|
||||
|
||||
function TWinFile.GetPath: String;
|
||||
begin
|
||||
Result := ExtractFilePath(FFileName); //Gesamter Ordnerpfad (ohne Dateiname)
|
||||
@@ -264,14 +509,34 @@ begin
|
||||
Result := ExtractFileDir(FFileName);
|
||||
end;
|
||||
|
||||
function TWinFile.GetModified: TDateTime;
|
||||
begin
|
||||
Result := GetFileModified(FFileName);
|
||||
end;
|
||||
|
||||
function TWinFile.GetCreated: TDateTime;
|
||||
begin
|
||||
Result := GetFileCreated(FFileName);
|
||||
end;
|
||||
|
||||
function TWinFile.GetAccessed: TDateTime;
|
||||
begin
|
||||
Result := GetFileAccessed(FFileName);
|
||||
end;
|
||||
|
||||
function TWinFile.GetAttributes: TFileAttributes;
|
||||
begin
|
||||
Result := GetFileAttributes(FFileName);
|
||||
end;
|
||||
|
||||
function TWinFile.Execute: Boolean;
|
||||
begin
|
||||
Result := ExecuteFile(FileName,ExecuteMode);
|
||||
Result := ExecuteFile(FFileName,ExecuteMode);
|
||||
end;
|
||||
|
||||
function TWinFile.SafeExecute: Boolean;
|
||||
begin
|
||||
Result := ExecuteFile(FileName,ExecuteMode,True);
|
||||
Result := ExecuteFile(FFileName,ExecuteMode,True);
|
||||
end;
|
||||
|
||||
end.
|
||||
|
Reference in New Issue
Block a user