File Properties



Often we want to know the properties of a file like its full path, its date of creation, last access and last modification. Or we want to know whether the file is a hidden file, system file or read-only file. At such times, we simply right click on the filename (in Explorer or in any other software that can pop up a file dialog). From the context menu that appears we select the ‘Properties’ option and the OS shows all the file properties in a dialog box. We too can develop a program that can achieve this. Here is the code of this program.

#include "afxwin.h"

#include "afxdlgs.h"

#include "resource.h"

class myframe : public CFrameWnd

{

    public :

    myframe( )

    {

        Create ( 0, "File Properties", WS_OVERLAPPEDWINDOW, rectDefault, 0, MAKEINTRESOURCE ( 
        IDR_MENU1 ) ) ;

    }

    void FileDialog( )

    {

        CString filename ,creat, modify,access ;

        CString info, str1, str2, str3, str4 ;

        CFileStatus fs ;

        CFileDialog d ( 1, 0, 0, 0, "All Files|*.*||" ) ;

        if ( d.DoModal( ) == IDOK )

            filename = d.GetPathName( ) ;

        CFile::GetStatus ( filename, fs ) ;

        CTime cre, mod, acc ;

        cre = fs.m_ctime ;

        CString daystring ;

        int day = cre.GetDayOfWeek( ) ;

        daystring = getdaystring ( day ) ;

      creat.Format ( " Creation Day & Date :- %s %d : %d : %d Creation Time :- %d : %d : %d ", daystring, cre.GetDay( ),

                                cre.GetMonth( ), cre.GetYear( ), cre.GetHour( ), cre.GetMinute( ), cre.GetSecond( ) ) ;

                               mod = fs.m_mtime ;day = mod.GetDayOfWeek( ) ;daystring = getdaystring ( day ) ;

        modify.Format ( "Modified Day & Date :- %s %d : %d : %dModified Time :-%d : %d : %d ", daystring, mod.GetDay( ),     
        mod.GetMonth( ),mod.GetYear( ) , mod.GetHour( ), mod.GetMinute( ), mod.GetSecond( ) ) ;

        acc = fs.m_atime ;

       day = acc.GetDayOfWeek( ) ;

       daystring = getdaystring ( day ) ;

       access.Format("Accessed Day & Date :- %s %d : %d : %d ", daystring, acc.GetDay( ), acc.GetMonth( ),  acc.GetYear( ) ) ;

       str1 = str2 = str3 = str4 = "NO" ;

       if ( ( fs.m_attribute & 0x01 ) == 0x01 )

           str1 = "YES" ;

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

           str2 = "YES" ;

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

           str3 = "YES" ;

      if ( ( fs.m_attribute & 0x20 ) == 0x20 )

           str4 = "YES" ;

      info.Format ( "FileName=%s \n\n size = %ld bytes \n\nReadOnly = %s \n\n Hidden = %s \n\nSystem = %s \n\n Archive = %s
                           \n\n %s \n\n %s \n\n %s", fs.m_szFullName, fs.m_size, str1, str2, str3, str4, creat, modify, access ) ;

      MessageBox ( info ) ;

}

CString getdaystring ( int day )

{

    CString str ;

    switch ( day )

    {

        case 1 :

            str = "Sunday" ;

            break ;

        case 2 :

            str = "Monday" ;

            break ;

        case 3 :

            str = "Tuesday" ;

            break ;

        case 4 :

            str = "Wednesday" ;

            break ;

        case 5 :

            str = "Thursday" ;

            break ;

        case 6 :

            str = "Friday" ;

            break ;

        case 7 :

            str = "Saturday" ;

            break ;

    }

    return str ;

}

DECLARE_MESSAGE_MAP( )

} ;

BEGIN_MESSAGE_MAP ( myframe, CFrameWnd )

ON_COMMAND ( 101, FileDialog )

END_MESSAGE_MAP( )

class myapp : public CWinApp

{

    public :

        BOOL InitInstance( )

        {

            myframe *p ;

            p = new myframe ;

            m_pMainWnd = p ;

            p -> ShowWindow ( SW_SHOWNORMAL ) ;

            return TRUE ;

        }

} ;

myapp a ;

On execution of the program a window appears with a menu attached to it. On selecting the menu item a file dialog is popped up. On selecting a file from this file dialog the properties of this file are shown in another dialog.

The file dialog is obtained by building a CFileDialog object and calling the member function DoModal( ) using it. Once the file is selected from this dialog its complete path is obtained using CFileDialog::GetPathName( ). This path name is passed to the GetStatus( ) static function of the CFile class. Along with the path a structure variable of the type CFileStatus is also passed. This structure is set up with the various file properties. These properties are then formatted properly and displayed in a dialog box. Note that the structure CFileStatus has been declared in the file ‘afx.h’.


Download