Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

Getting File Properties

1. File Property And CFile

We can get important file properties by making a call to the CFile’s static function GetFileStatus(). This function takes CFileStatus object as a reference param and fills it with the needed data. In this example, we will see how to use the CFileStatus object to fetch some critical file properties.

2. About the Example

The below picture shows the screenshot of example:

About the File Property Example
About the File Property Example

When the user clicks the browse button, the example displays file selection dialog. From this dialog, user picks a file name. The example shows the file properties in the relevant user interface items as in the above screen.

3. Creating the Example

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:

Example in Class View
Example in Class View

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

Control in Sample Application
Control in Sample Application

After the UI design, we should assign a control variable for each controls. One can refer the below table:

UI ElementControl Variable Name
File Name Edit Boxm_ctrl_edit_filename
Created On Edit Boxm_ctrl_edit_filecreatedon
Modified On Edit Boxm_ctrl_edit_filemodifiedon
File Size Edit Boxm_ctrl_edit_filesize
Read Only Check Boxm_ctrl_chk_readonly
Hidden Check Boxm_ctrl_chk_hidden
System Check Boxm_ctrl_chk_system

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

4. Pick a File

To select a file in the system, we can use MFC’s common file dialog. Next, we create an Instance of CFileDialog and then display it using the  DoModal call. The below picture shows the dialog:

Picking a File
Picking a File

The GetPathName() function of the CFileDialog’s will give us the filename selected by the user with the path. Even though the name states path name, we actually get filename with fully qualified path. Below is the code for reference:

5. File Created & 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, we pass the CFileStatus object 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 times (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:

6. File Attributes & Size

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

The Bitwise OR operation collects the file attributes when we make a call to the GetStatus function. To make a check for the particular attributes, we should do Bitwise AND operation. The below enum give the hexadecimal-decimal constant for each attribute: 

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

The below video explains and shows how the sample application works:

Source Code : Download

Categories: MFC

Tags: , , , ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.