Tuesday, September 02, 2014

[ MFC ] - Get File Properties using CFileStatus and CFile::GetStatus

1. Introduction

We can get important file properties by making a call to the CFile’s static function GetFileStatus(). This function accepts CFileStatus object as a reference parameter and fills this object with the required information. In this article, I will show how do we use this object to retrieve some important file attributes.

2. About the example

The below screenshot shows the screen shot of example:

When the browse button is clicked, a file selection dialog is displayed. Once the file is picked from the file selection dialog, the file properties displayed in the dialog as described in the screen shot.

3. Creating the Application

Create a dialog based MFC application and name it as FileProp by accepting all the default settings in wizard pages. Once the application is created the class view looks like below:

The below-shown screen shot will help you in designing the application dialog. The dialog marked with number 1 is text boxes and all three check boxes marked as 3 are placed in the group box marked as number 2. Rename of the OK button as close and add a browse button marked as 4. That is all; the dialog is ready with the UI elements.

After the UI design, each control should be assigned with control variable and this association is shown in the below table:

UI Element
Control Variable Name
File Name Edit Box
Created On Edit Box
Modified On Edit Box
File Size Edit Box
Read Only Check Box
Hidden Check Box
System Check Box

To know how to add a control variable watch the video shown in the below link:

4.  Coding – Pick File

To select a file in the system, MFC’s common file dialog is used. An Instance of CfileDialog is created and then the dialog is displayed using the DoModal call. The displayed dialog is shown below:

The file name selected in this dialog is retrieved through CfileDialog’s member function GetPathName(). Even though the name states path name, we actually get filename with fully qualified path. The code for this is shown below:

//Sample 3.0: Get File Properties
void CFilePropDlg::OnBnClickedButtonBrowse()
                //Sample 3.1: Get Name of the File
                CFileDialog dlg ( TRUE, NULL, NULL, NULL, "All Files|*.*||" ) ;
                if ( dlg.DoModal( ) == IDOK )
                                m_filename = dlg.GetPathName() ;       

5. Coding – File Created and Modified time

We have the file name from the previous section of code. Now we will retrieve the properties of this selected file. As already specified in the introduction, CFileStatus object is passed to the GetStatus function of the CFile and this call fills the file properties in the CFileStatus structure. In the below code, m_ctime, m_mtime members are used to get the file created time as well as file modified time.

These members (m_ctime, m_mtime) are CTime instances and using the Format function of the CTime, the time (i.e.) created time and modified times are formatted in more readable format. Then this formatted time is displayed in the application example. Below is the code:

                //Sample 3.2: Get the File Status Object
                CFileStatus status;
                CFile::GetStatus(m_filename, status);

                //Sample 3.3: Get File Creation, Modification
                CString time_formatted;
                CTime time;
                time = status.m_ctime;
                time_formatted = time.Format("%d-%b-%Y   %H:%M:%S");
                time = status.m_mtime;
                time_formatted = time.Format("%d-%b-%Y   %H:%M:%S");
                m_ctrl_edit_filemodifiedon.SetWindowText( time_formatted);

6.  Coding – File Attributes and Size

The size of the file is retrieved through the member m_size. This member is formatted in a CString object then displayed on the dialog. Below is code for it:

//Sample 3.4: Get File Size
CString file_size;
file_size.Format("%ld", status.m_size );

The file attributes are collected by the Bitwise OR operation when we make a call to the GetStatus function. To make a check for the particular attributes we should do Bitwise AND operation. The hexadecimal decimal constant for each attributes are given below: 

enum Attribute {
   normal =    0x00,
   readOnly =  0x01,
   hidden =    0x02,
   system =    0x04,
   volume =    0x08,
   directory = 0x10,
   archive =   0x20

OK. In the CFileStatus these attribute values are pushed into the member m_attribute and the constant values listed above are tested using the Bitwise AND operation. In the below code we do such a test and place/remove the check mark on the corresponding the checkboxes.

//Sample 3.5: Get the File Attributes
if ( (status.m_attribute & 0x01) == 0x01 )

if ( (status.m_attribute & 0x02) == 0x02)

if ( (status.m_attribute & 0x04) == 0x04)


The below video Explains and shows how the sample application works.

Watch How Sample works. Video

Source Code : Download

No comments:

Post a Comment

Leave your comment(s) here.

Like this site? Tell it to your Firend :)