CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Thread: Get File Size

  1. #1
    Join Date
    Dec 2001
    Location
    Bremen, Germany
    Posts
    314

    Get File Size

    Hi

    What is the fastest way to retrieve the (64 bit) filesize of a file?

    I need to get the size of *many* files from various locations (on WinXP systems,only), so what would be faster:

    CreateFile + GetFileSize + CloseHandle
    or
    FindFirstFile / FindNextFile

    or do you have another idea?

    Thank you in advance.

    Oliver.

  2. #2
    Join Date
    Jul 2004
    Posts
    155

    Re: Get File Size

    hi,
    FindFirstFile/FindNextFile/FindClose seems to be the fastest and
    preferred ...IMHO


    Praseed Pai

  3. #3
    Ejaz's Avatar
    Ejaz is offline Elite Member Power Poster
    Join Date
    Jul 2002
    Location
    Lahore, Pakistan
    Posts
    4,211

    Re: Get File Size

    Although I haven't used any of them so far, but they look handy for this purpose.
    GetFileSize
    GetCompressedFileSize
    GetFileSizeEx

  4. #4
    Join Date
    May 2004
    Location
    Simpheropol, Ukraine
    Posts
    73

    Re: Get File Size

    You can also try GetFileAttributesEx ().

    I'm not sure whether it is faster than FindFirst/FindNext/FindClose, but it is definitely faster than CreateFile/GetFileSize (which also modifies the file last access time).

  5. #5
    Join Date
    Dec 2001
    Location
    Bremen, Germany
    Posts
    314

    Lightbulb Re: Get File Size

    Thank you for your suggestions!

    @Ejaz
    But GetFileSize etc. require that the file was previously opened by a call to CreateFile. This is the API I'm currently using, but it seems a little slow to me.

    @genossa
    Bingo! I knew there was a function which takes the file path and returns the 64bit file size, but I could not remember what it was. I believe this will be much faster, so I'll try it next week and post the results of my little test

    Thank you all & have a good weekend.

    Oliver.

  6. #6
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Get File Size

    I use _stat64 which accepts a filename and which retrieves a 64 bit size.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  7. #7
    Join Date
    Dec 2001
    Location
    Bremen, Germany
    Posts
    314

    Re: Get File Size

    I just tried GetFileAttributesEx(...) and it really seems to be *much* faster than GetFileSize(...)!

    The only drawback is that GetFileAttributesEx(...) is not supported by Win95.

    @Marc G
    Thank you; I didn't knew this C runtime function (_stati64, _wstati64). I'll give them a try and report back!

    Oliver.

  8. #8
    Join Date
    Dec 2001
    Location
    Bremen, Germany
    Posts
    314

    Thumbs up Re: Get File Size

    Quote Originally Posted by Marc G
    I use _stat64 which accepts a filename and which retrieves a 64 bit size.
    At least on my Win2k workstation, _stati64 is screaming fast compared to CreateFile / GetFileSize / CloseFile!

    Maybe it is a drawback that it does not support security attributes, but I do not need that anyway.

    Thank you for this tip!

    Oliver.

  9. #9
    Join Date
    May 2004
    Location
    Simpheropol, Ukraine
    Posts
    73

    Re: Get File Size

    One more tip:

    _stat64 uses FindFirstFile/FindClose inside (just looked into the source code of
    _stat64 for Visual Studio). So, if your application is designed for Windows paltform then using FindFirstFile/FindClose directly will be a little bit faster than calling to _stat64

  10. #10
    Join Date
    Dec 2001
    Location
    Bremen, Germany
    Posts
    314

    Re: Get File Size

    Hi genossa

    Where did you look it up? I just searched the MS VC directory and the web but did not find the implementation of stati64.

    BTW: The function names are _stati64 and _wstati64().

    Oliver.

  11. #11
    Join Date
    Dec 2001
    Location
    Bremen, Germany
    Posts
    314

    Re: Get File Size

    Oh, I just found it: VC98\CRT\SRC\STAT.C function declaration "int __cdecl _tstati64".

    It really uses FindFirstFile / FindClose API. I must say that I did not believe these APIs are that fast!

    Thank you.

    Oliver.

  12. #12
    Join Date
    Jul 2004
    Posts
    155

    Re: Get File Size

    Hi ,

    on my machine i have installed VS.net in the C folder.

    u may find the source code of the routines mentioned above in the following path
    C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\crt\src

    Praseed Pai
    www.praseedpai.com

  13. #13
    Join Date
    May 2004
    Location
    Simpheropol, Ukraine
    Posts
    73

    Re: Get File Size

    BTW: The function names are _stati64 and _wstati64().
    Oops. My bad.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured