Official Download Embarcadero Delphi XE8 Lite by clicking the link above to Download. The Embarcadero Delphi XE8 Lite is the latest release, tested working 100% full version.
Delphi 2010 has a nice set of new file access functions in
IOUtils.pas
(I especially like the UTC versions of the date-related functions). What I miss so far is something likeWhat is the Delphi 2010-way to get the size of a file? Do I have to go back and use FindFirst to access
TSearchRec.FindData
?Thanks.
jpfolleniusjpfollenius
6 Answers
I'm not sure if there's a 'Delphi 2010' way, but there is a Windows way that doesn't involve FindFirst and all that jazz.
I threw together this Delphi conversion of that routine (and in the process modified it to handle > 4GB size files, should you need that).
You could actually just use GetFileSize() but this requires a file HANDLE, not just a file name, and similar to the GetCompressedFileSize() suggestion, this requires two variables to call. Both GetFileSize() and GetCompressedFileSize() overload their return value, so testing for success and ensuring a valid result is just that little bit more awkward.
GetFileSizeEx() avoids the nitty gritty of handling > 4GB file sizes and detecting valid results, but also requires a file HANDLE, rather than a name, and (as of Delphi 2009 at least, I haven't checked 2010) isn't declared for you in the VCL anywhere, you would have to provide your own import declaration.
DelticsDeltics
tz.tz.
You can also use DSiFileSize from DSiWin32. Works in 'all' Delphis. Internally it calls CreateFile and GetFileSize.
gabrgabr
I'd like to mention few Pure Delphi ways. Though i think Deltics made a most speed-effective answer for Windows platform, yet sometimes you want just rely on RTL and also make portable code that would work in Delphi for MacOS or in FreePascal/Virtual Pascal/whatever.
There is
FileSize
function left from Turbo Pascal days. The sample above lacks 'read-only' mode setting. You would require that to open r/o file such as one on CD-ROM media or in folder with ACLs set to r/o. Before calling
ReSet
there should be zero assigned to FileMode
global var.It would not work on files above 2GB size (maybe with negative to cardinal cast - up to 4GB) but is 'out of the box' one.
There is one more approach, that you may be familiar if you ever did ASM programming for MS-DOS. You
I can't say exactly which Delphi version introduced those, but i think it was already in some ancient version like D5 or D7, though that is just common sense and i cannot check it.
That would take you an extra THandle variable and
Seek
file pointer to 1st byte, then to last byte, and check the difference.I can't say exactly which Delphi version introduced those, but i think it was already in some ancient version like D5 or D7, though that is just common sense and i cannot check it.
That would take you an extra THandle variable and
try-finally
block to always close the handle after size was obtained.Aside from 1st approach this is int64-capable.It is also compatible with FreePascal, though with some limitations
You can also create and use
TFileStream
-typed object - which was the primary, officially blessed avenue for file operations since Delphi 1.0
As a side note, this avenue is of course integrated with aforementioned
IOUtils
unit.Arioch 'TheArioch 'The
This is a short solution using FileSize that does the job:
From what I know, FileSize is available only from XE2.
pavel.lazarpavel.lazar
Richard WinstonRichard Winston
Not the answer you're looking for? Browse other questions tagged delphifiledelphi-2010 or ask your own question.
I use Delphi RAD 2010 and Indy 10.5.5. Now I'm working with IdSMTP and IdMessage. But found some issues in them work. And find information that in late version, for example in 10.5.9 this problem has gone. But on Indy official site I can't find downloads link.
T_E_M_AT_E_M_A
2 Answers
But on Indy official site I can't find downloads link.
- Go to http://www.indyproject.org
- Click on Indy.Sockets (VCL)
- Click on Downloads
- Click on Version 10
- Click on Development Snapshot
- Click on Development Snapshot (again)
- Click on Indy via SVN, Fulgan.com, or precompiled binaries.
Remy LebeauRemy Lebeau
You can check out Indy from the public subversion repository.
Detailed instructions are at http://www.indyproject.org/sockets/download/svn.en.aspx
mjnmjn