Click here to Skip to main content
15,879,535 members
Articles / Desktop Programming / MFC
Article

Recursively listing file type information and icon

Rate me:
Please Sign up or sign in to vote.
3.67/5 (6 votes)
21 Jun 2001 105.6K   1.1K   31   18
Code to recursively search folders to display file type information and icons

Introduction

Here I developed some code that uses the CFileFind Class to search the selected Drive or Folder recursively and display the file information and icon in a list control.

BOOL CShellDlg::ListFile(LPCTSTR szPath)
{
    CFileFind ff;
    CString path = szPath;
    path += "\\*.*";
    BOOL res = TRUE;
    CWaitCursor waiter;
    if(ff.FindFile(path))
    {
        while(res)
        {
            
            m_List.LockWindowUpdate();
            
            res = ff.FindNextFile();
            if(!ff.IsDots())
            {
                counter++;
                CString file = ff.GetFilePath();
                int i = m_List.InsertItem(0,ff.GetFileName(),
                                          GetIconIndex(ff.GetFilePath()));
                CString size;
                if (ff.GetLength64() < 1024) 
                size.Format("%d   B",ff.GetLength());
                else 
                {
                    // The following weird LONGLONG casts are because VC 6.0
                    // doesn't implement int64 to double conversion =8-ooo
                    if (ff.GetLength64() < (1024*1024))
                        size.Format("%3.2f KB",
                                    (LONGLONG) ff.GetLength64() / 1024.0);
                    else 
                    {
                        if (ff.GetLength64() < (1024*1024*1024))
                            size.Format("%3.2f MB", 
                                       (LONGLONG) ff.GetLength64() / (1024*1024.0));
                        else
                            size.Format("%1.2f GB", 
                                       (LONGLONG) ff.GetLength64() / (1024.0*1024*1024));
                    }
                }     
                m_List.SetItemText(i,1,size);
                CString type = GetTypeName(ff.GetFilePath());
                m_List.SetItemText(i,2,type);
                
                CString date;
                CTime time;
                ff.GetCreationTime(time);
                date = time.Format("%x");
                m_List.SetItemText(i,3,date);
                            
                m_List.SetItemText(i,4,ff.GetFilePath());
                if(ff.IsDirectory())
                {
                    path = ff.GetFilePath();
                    ListFile(path);
                }
                m_aniIcon.SetIcon(h_aIcon[m_icon]);
                m_icon++;
                if(m_icon >= ICONS)
                {
                    m_icon = 0;
                }
                
                CString sin = ff.GetRoot();
                m_SearchIN.SetWindowText(sin);
                m_fFiles.SetWindowText(file);
                CString found;
                found.Format("%u",m_List.GetItemCount());
                m_Count.SetWindowText(found);
            }
        }
        m_List.UnlockWindowUpdate();
    }
    return TRUE;
}

TCHAR* CShellDlg::GetTypeName(CString strFPath)
{
    SHFILEINFO sfi;
    memset(&sfi, 0, sizeof(sfi));
    
    static TCHAR lpBuff[MAX_PATH];
    lpBuff[0] = TCHAR ('\0');

    SHGetFileInfo (
    strFPath, 
        0, 
        &sfi, 
        sizeof(sfi), 
        SHGFI_TYPENAME
    );

    lstrcpy(lpBuff, sfi.szTypeName);
    if (lpBuff[0] == TCHAR('\0'))
    {
        int nDotIdx = strFPath.ReverseFind (TCHAR('.'));
        int nBSIdx = strFPath.ReverseFind (TCHAR('\\'));
        if (nDotIdx > nBSIdx)
        {
            strFPath = strFPath.Mid(nDotIdx+1);
            strFPath.MakeUpper();
            lstrcpy (lpBuff, strFPath + TCHAR (' '));
        }

        lstrcat (lpBuff, _T("File"));
    }

    return lpBuff;
}

int CShellDlg::GetIconIndex(const CString &csFileName)
{
    SHFILEINFO    sfi;

        SHGetFileInfo(
           (LPCTSTR)csFileName, 
           0,
           &sfi, 
           sizeof(SHFILEINFO), 
           SHGFI_SYSICONINDEX | SHGFI_SMALLICON );

        return sfi.iIcon;
}

Thank you to all of you :-)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
My name is BLaZe and I have 15 years old

Comments and Discussions

 
Questiongreat article Pin
Simon_7777-Mar-16 16:06
Simon_7777-Mar-16 16:06 
Questionhow do i get the desired in vc .net Pin
itbuff6-Nov-06 3:33
itbuff6-Nov-06 3:33 
GeneralSHGetFileInfo with SHGFI_SYSICONINDEX flag doesn't work Pin
Tanmay Sathe25-Feb-04 12:51
Tanmay Sathe25-Feb-04 12:51 
GeneralRe: SHGetFileInfo with SHGFI_SYSICONINDEX flag doesn't work Pin
Lilith Wong15-Jun-06 18:36
Lilith Wong15-Jun-06 18:36 
GeneralThanks Pin
supertedusa7-Aug-03 8:26
supertedusa7-Aug-03 8:26 
GeneralThe icons disappear in W98. Pin
Tomas21-Apr-03 7:29
Tomas21-Apr-03 7:29 
GeneralRe: The icons disappear in W98. Pin
Ridgeley Yu2-Jun-03 17:37
Ridgeley Yu2-Jun-03 17:37 
GeneralRe: The icons disappear in W98. Pin
Tomas2-Jun-03 21:53
Tomas2-Jun-03 21:53 
GeneralRe: The icons disappear in W98. Pin
Ridgeley Yu3-Jun-03 2:29
Ridgeley Yu3-Jun-03 2:29 
GeneralRe: The icons disappear in W98. Pin
Tomas3-Jun-03 20:10
Tomas3-Jun-03 20:10 
GeneralPatteren Matching / Multiple File selection Pin
alpu6-Mar-03 16:55
alpu6-Mar-03 16:55 
GeneralChanges to run on VC7 Pin
TigerNinja_16-Feb-03 12:55
TigerNinja_16-Feb-03 12:55 
Questionhow to get info like explorere properties? Pin
17-Sep-01 20:20
suss17-Sep-01 20:20 
Generalso good Pin
25-Jun-01 16:31
suss25-Jun-01 16:31 
GeneralRe: so good Pin
25-Jun-01 16:33
suss25-Jun-01 16:33 
it's test nofity
GeneralRe: so good Pin
25-Jun-01 16:35
suss25-Jun-01 16:35 
GeneralRe: so good Pin
BLaZe25-Jun-01 17:08
BLaZe25-Jun-01 17:08 
GeneralClip Children Pin
BLaZe23-Jun-01 6:45
BLaZe23-Jun-01 6:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.