MFC is equivalent to Java File # isDirectory ()

Is there an equivalent to the Java file isDirectory () method in MFC? I tried using this:


static bool isDirectory(CString &path) {
  return GetFileAttributes(path) & FILE_ATTRIBUTE_DIRECTORY;   
}

      

but it doesn't work.

+1


source to share


4 answers


Sorry for the possible "inconsistency" of the answer to the question, but maybe you will see that this is useful because anytime I need something like this on Windows, I DO NOT use MFC, but the usual Windows API:



//not completely tested but after some debug I'm sure it'll work
bool IsDirectory(LPCTSTR sDirName)
{
    //First define special structure defined in windows
    WIN32_FIND_DATA findFileData; ZeroMemory(&findFileData, sizeof(WIN32_FIND_DATA));
    //after that call WinAPI function finding file\directory
    //(don't forget to close handle after all!)
    HANDLE hf = ::FindFirstFile(sDirName, &findFileData);
    if (hf  ==  INVALID_HANDLE_VALUE) //also predefined value - 0xFFFFFFFF
    return false;
    //closing handle!
    ::FindClose(hf);
    // true if directory flag in on
    return (findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
}

      

+2


source


CFileFind :: IsDirectory ()

http://msdn.microsoft.com/en-us/library/scx99850(VS.80).aspx

EDIT:

  #include <afxwin.h>
  #include <iostream>

  using namespace std;

  CFileFind finder;

  fileName += _T("c:\\aDirName");
  if (finder.FindFile(fileName))
  {
        if (finder.FindNextFIle())
        {            
              if (finder.IsDirectory())
              {
                    // Do directory stuff...
              }
        }
  }

      



If you change the filename to have wildcards, you can do

  while(finder.findNextFile()) {...

      

to get all matching files.

+4


source


MFC solution on demand: a_FSItem for item being checked (check required CFile :: GetStatus ()).

  CFileStatus t_aFSItemStat;
  CFile::GetStatus( a_FSItem, t_aFSItemStat );

  if ( ( t_aFSItemStat.m_attribute & CFile::directory )
    return true;

  return false;

      

if you want to include the root of the volume as a valid directory just add it to the test

t_aFSItemStat.m_attribute & CFile::volume

      

+1


source


Its not MFC, but I am using this:

bool IsValidFolder(LPCTSTR pszPath)
{
    const DWORD dwAttr = ::GetFileAttributes(pszPath);
    if(dwAttr != 0xFFFFFFFF)
    {
        if((FILE_ATTRIBUTE_DIRECTORY & dwAttr) &&
           0 != _tcscmp(_T("."), pszPath) &&
           0 != _tcscmp(_T(".."), pszPath))
        {
            return true;
        }
    }

    return false;
}

      

+1


source







All Articles