How to get the size of a file without opening it

24 03 2009

description5

 

 

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.

howcanidoit5

 

 

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.

mycodesnippet5

 

 

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
}

mynote8

 

 

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

Advertisements

Actions

Information

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s




%d bloggers like this: