Hi,
I'm trying to count files in a desired directory using MFC...

I tried the following code:

int CountFiles(const std::string &refcstrRootDirectory,
               const std::string &refcstrExtension,
               bool              bSubdirectories = true)
{
  int             iCount          = 0;
  std::string     strFilePath;          // Filepath
  std::string     strPattern;           // Pattern
  //std::string     strExtension;         // Extension
  HANDLE          hFile;                // Handle to file
  WIN32_FIND_DATA FileInformation;      // File information
  std::string DoublebackSlash = "\\";

  strPattern = refcstrRootDirectory + "\\*.*";
  hFile = ::FindFirstFile(strPattern.c_str(), (&FileInformation));
  if(hFile != INVALID_HANDLE_VALUE)
  {
    do
    {
      if(FileInformation.cFileName[0] != '.')
      {
        strFilePath.erase();
        strFilePath = refcstrRootDirectory;// + "\\" +FileInformation.cFileName;
        
        strFilePath += DoublebackSlash;

        CString cfilename(FileInformation.cFileName);
        CT2CA pszConvertedAnsiString(cfilename);
        std::string str (pszConvertedAnsiString);

        strFilePath += str;

        if(FileInformation.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
        {
          if(bSubdirectories)
          {
            // Search subdirectory
            int iRC = CountFiles(strFilePath,
                                 refcstrExtension,
                                 bSubdirectories);
            if(iRC != -1)
              iCount += iRC;
            else
              return -1;
          }
        }
        else
        {
          // Check extension
            CString cs(FileInformation.cFileName);
            //USES_CONVERSION;

           // Convert a TCHAR string to a LPCSTR
           CT2CA pszConvertedAnsiString(cs);
           // construct a std::string using the LPCSTR input
           std::string strExtension (pszConvertedAnsiString);   //Extension

            
          strExtension = strExtension.substr(strExtension.rfind(".") + 1);

          if((refcstrExtension == "*") ||
             (strExtension == refcstrExtension))
          {
            // Increase counter
            ++iCount;
          }
        }
      }
    } while(::FindNextFile(hFile, &FileInformation) == TRUE);

    // Close handle
    ::FindClose(hFile);
  }

  return iCount;
}

but i got the following error:

hFile = ::FindFirstFile(strPattern.c_str(), (&FileInformation));

Error 4 error C2664: 'FindFirstFileW' : cannot convert parameter 1 from 'const char *' to 'LPCWSTR'

I tried to pass it in a Unicode string using _T(""), L""

hFile = ::FindFirstFile(_T(strPattern.c_str()), (&FileInformation));

but nothing happened...

here's the calling for the function:

void CSoundProjectDlg::OnBnClickedButton2()
{
    int iNumberOfFiles = 0;


  // Count all files in 'c:' and its subdirectories
  iNumberOfFiles = CountFiles("c:", "*");
  if(iNumberOfFiles == -1)
  {
    // fire an error
  }

  // Print results

  // Count '.avi' files in 'c:'
  iNumberOfFiles = CountFiles("c:", "avi", false);
  if(iNumberOfFiles == -1)
  {
    // fire an error
  }

  // Print results
}

Thanks,

typedef char* LPCSTR;
typedef wchar_t* LPWCSTR;

#ifndef _UNICODE
#define LPCTSTR LPCSTR;
#else
#define LPCTSTR LPWCSTR;
#endif

yes you correct that's the problem. that's unicode.

allright ,

I tried to pass it in a Unicode string using _T(""), L""
Help with Code Tags
(Toggle Plain Text)

hFile = ::FindFirstFile(_T(strPattern.c_str()), (&FileInformation));

hFile = ::FindFirstFile(_T(strPattern.c_str()), (&FileInformation));
but nothing happened...

First of all _T is a macro call , macros and functions are different
and sometimes they looks like similar. But it leave bugs that very
hard to debug even.

you can use _T("my string") like this
but for a variable you can't use it .

#define __T(x) L ## x

so , what it does is just only concatrate the L prefix like saying .
L"my string"

so you cannot use it for the variables.

There are other functions to convert a ASCII string to unicode , use them.

* Edit using the L prefix to the variables

Looks like the latest versions of the C++ specification allows.
However no one is going to keep all the spec in mind.
sorry about it.

however __T(x) is definied with a L if you using the unicode inside
you'r project. Try define unicode first , and if not worked then
trying the other functions.

#include "stdafx.h"


#include <iostream>
using namespace std ;

#include <string>
using std::string ;
#include <windows.h>

#define __T(x) L ## x

int main(int argc, char* argv[])
{	
	string my_string("this is a string");
	__T(my_string.c_str());

			
	return 0;
}

look at this code , and it returns the error ,
H:\STL\string\string.cpp(19) : error C2065: 'Lmy_string' : undeclared identifier

Anyway I don't know the latest versions of the C++, however

But L"my String" is correct.

well i tried to google but i didn't find any thing useful concerning converting ASCII to Unicode and vice versa using MFC...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.