Showing posts with label MFC. Show all posts
Showing posts with label MFC. Show all posts

Monday, July 22, 2013

Return the filename from the full file path in MFC

Below is the code snippet for a C++ function to return the filename from the full file path in MFC:

#include "StdAfx.h"

CString CAppUtilManager::ExtractName(CString strFullPath) const
{
 int pos=strFullPath.ReverseFind('\\');
 return strFullPath.Right(strFullPath.GetLength()-pos-1);
}

Return the list of files from a directory in MFC

Below is the code snippet that implements a C++ function to return the list of files in a directory in MFC:


#include "StdAfx.h"

void CAppUtilManager::GetFiles(const CString& parent_folder, const CString& filters, CStringArray& files, BOOL bRecursive) const
{
 CFileFind fFind;
 BOOL bWorking=fFind.FindFile(parent_folder+_T("\\")+filters);

 while(bWorking==TRUE)
 {
  bWorking=fFind.FindNextFile();
  if(fFind.IsDots())
  {
   continue;
  }
  if(fFind.IsDirectory())
  {
   if(bRecursive==TRUE)
   {
    GetFiles(fFind.GetFilePath(), filters, files, bRecursive);
   }
  }
  else
  {
   files.Add(fFind.GetFilePath());
  }
 }
 fFind.Close();
}

Return the list of sub folders from a directory in MFC

Below is the code snippet for a C++ function to return a list of sub folders from a directory in MFC:

#include "StdAfx.h"

void CAppUtilManager::GetSubFolders(const CString& parent_folder, CStringArray& sub_folders) const
{
 CFileFind fFind;
 BOOL bWorking=fFind.FindFile(parent_folder+_T("\\*.*"));

 while(bWorking==TRUE)
 {
  bWorking=fFind.FindNextFile();
  if(fFind.IsDots())
  {
   continue;
  }
  if(fFind.IsDirectory())
  {
   sub_folders.Add(fFind.GetFilePath());
  }
 }
 fFind.Close();
}

Open a Folder Browser with Create Button in MFC

Below is the code snippet that implements a C++ function to open a folder browser in MFC with a Create Folder button, and return the path to the selected folder.


#include "StdAfx.h"
#include "shlobj.h"

CString CAppUtilManager::BrowseForDirectory() const
{
 int MAX_PATH=256;
 TCHAR display_name[MAX_PATH];
 TCHAR path[MAX_PATH];
    BROWSEINFO bi = { 0 };
    bi.lpszTitle = _T("Select an existing or created folder");
 bi.pszDisplayName=display_name;
 bi.ulFlags |= BIF_NEWDIALOGSTYLE;
    LPITEMIDLIST pidl = SHBrowseForFolder(&bi);

 CString directory_path(_T(""));

    if(pidl != 0)
    {
        // get the name of the folder and put it in path
        SHGetPathFromIDList (pidl, path);

        //Set the current directory to path
        directory_path=path;

        // free memory used
        IMalloc * imalloc = 0;
        if ( SUCCEEDED( SHGetMalloc ( &imalloc )) )
        {
            imalloc->Free ( pidl );
            imalloc->Release ( );
        }
    }

 return directory_path;
}

Having TinyXML working with MFC

If you are using TinyXml with MFC, remember to add the following line

#include "stdafx.h"

to the top of the the source files tinystr.cpp, tinyxml.cpp, and tinyxmlerror.cpp, tinyxmlparser.cpp to prevent compilation error in MFC project.