Last active
February 21, 2017 08:39
-
-
Save freeonterminate/e2316f0f829115851358 to your computer and use it in GitHub Desktop.
Get File Size !
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* | |
* Get File Size | |
* | |
* Copyright (c) 2015, 2016 HOSOKAWA Jun. | |
* | |
* CONTACT | |
* Twitter @pik or [email protected] | |
* | |
* LAST UPDATE | |
* 2016/03/30 Delete warning | |
* 2015/12/22 First Release | |
* | |
* PLATFORM | |
* Windows, OS X, iOS, Android | |
* Delphi (XE8 <- maybe) 10 seattle, 10.1 Berlin | |
* Maybe, Appmethod and C++Builder | |
* | |
* ORIGINAL SOURCE | |
* https://gist.github.com/freeonterminate/e2316f0f829115851358 | |
* | |
* HOW TO USE | |
* 1. uses System.IOUtils.Files; | |
* 2, TFile.GetSize('FullPath'): | |
*) | |
unit System.IOUtils.Files; | |
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 | |
Handle: THandle; | |
Rec: TWin32FindData; | |
OrgErrMode: Integer; | |
begin | |
Result := -1; | |
if (not TFile.Exists(iFileName)) then | |
Exit; | |
OrgErrMode := SetErrorMode(SEM_FAILCRITICALERRORS); | |
try | |
Handle := FindFirstFile(PChar(iFileName), Rec); | |
try | |
if | |
(Handle <> INVALID_HANDLE_VALUE) and | |
((Rec.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) = 0) | |
then | |
Result := (Int64(Rec.nFileSizeHigh) shl 32) + Rec.nFileSizeLow; | |
finally | |
FindClose(Handle); | |
end; | |
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. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment