-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathFileUtils.pas
More file actions
70 lines (55 loc) · 1.21 KB
/
FileUtils.pas
File metadata and controls
70 lines (55 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
unit FileUtils;
interface
uses
System.SysUtils,
System.IOUtils;
type
TFile = System.IOUtils.TFile;
TFileHelper = record helper for TFile
public
class function GetSize(const iFileName: String): Int64; static;
end;
implementation
uses
System.Classes
{$IFDEF MSWINDOWS}
, Winapi.Windows
{$ENDIF}
{$IFDEF POSIX}
, Posix.SysStat
{$ENDIF}
;
{ TFileHelper }
class function TFileHelper.GetSize(const iFileName: String): Int64;
{$IFDEF MSWINDOWS}
var
info: TWin32FileAttributeData;
OrgErrMode: Integer;
begin
Result := -1;
if (not TFile.Exists(iFileName)) then
Exit;
OrgErrMode := SetErrorMode(SEM_FAILCRITICALERRORS);
try
if not GetFileAttributesEx(PWideChar(iFileName), GetFileExInfoStandard, @info) then
Exit;
Result := Int64(info.nFileSizeLow) or Int64(info.nFileSizeHigh shl 32);
finally
SetErrorMode(OrgErrMode);
end;
end;
{$ENDIF}
{$IFDEF POSIX}
var
Rec: _stat;
M: TMarshaller;
begin
Result := -1;
if (not TFile.Exists(iFileName)) then
Exit;
FillChar(Rec, SizeOf(Rec), 0);
if (stat(M.AsAnsi(iFileName).ToPointer, Rec) = 0) then
Result := Rec.st_size;
end;
{$ENDIF}
end.