How to get the size of a file without opening it

24 03 2009




Consider a situation that we are recording a WAV file using any of the Windows sound recorders to a particular path. But do you notice that the size of the file is updating every couple of seconds if you look it in the Windows Explorer.  That means Windows Explorer could check the file size without open it even though it is still being recorded.




There is WIN32_FIND_DATA structure for that, which describes a file found by the FindFirstFile, FindFirstFileEx or FindNextFile function. This structure helps us to get the file creation time, last access time, last write time, file size etc. These whole things could be accessed without open the file.




ULONGLONG GetFileSizeEx( CString strPath )
   WIN32_FIND_DATA FindData = { 0 };
   //gets a file search handle
   HANDLE hFirstFile = FindFirstFile( strPath, &FindData );  
   //if the handle is valid
   if( hFirstFile != INVALID_HANDLE_VALUE )
      //closes the file search handle
      FindClose( hFirstFile );
      ULONGLONG FileSize = FindData.nFileSizeHigh;
      FileSize <<= sizeof( FindData.nFileSizeHigh ) * 8; // Push by count of bits
      FileSize |= FindData.nFileSizeLow;
      return FileSize;
   return 0; // File not found




Not all file systems can record creation and last access time and not all file systems can record then in the same manner.




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: