The Code Project View our sponsorsCodejock Software - Serious GUI CodeAdvertise on the CodeProject
Home >> Files & Folders >> General

Recursively listing file type information and icon
By BLaZe

Code to recursively search folders to display file type information and icons 
 VC 4-6, Win95-98, NT4, W2K, MFC
 Posted 22 Jun 2001
Articles by this author
Send to a friend
Printer friendly version
Home Latest updates Submit your article About Us Advertise on the Code Project Contact us Discussion Forums
Navigation bar
11 users have rated this article. result:
3.55 out of 5.

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 :-)

About BLaZe

My name is BLaZe and I have 15 years old

[Top] Sign in to vote for this article:     PoorExcellent  

View our sponsorsDownload Visual Build - Automate your software buildsAdvertise on the CodeProject

Hint: For improved responsiveness, use Internet Explorer 4 (or above) with Javascript enabled, choose 'Use DHTML' from the View dropdown and hit 'Set Options'.
 Keyword Filter
 View   Per page   Messages since
New threadMessages 1 to 5 of 5 (Total: 5)First Prev Next Last
Subject 
Author 
Date 
  so good
lizifeng 22:31 25 Jun 01 
  Re: so good
caifangbao 22:33 25 Jun 01 
  Re: so good
zhuwenyuan 22:35 25 Jun 01 
  Re: so good
BLaZe 23:08 25 Jun 01 
  Clip Children
BLaZe 12:45 23 Jun 01 
Last Visit: 12:00 Friday 1st January, 1999First Prev Next Last
Home >> Files & Folders >> General
Advertise on The Code Project
Article content copyright BLaZe, 2001
everything else © CodeProject, 1999-2001.