0

I have a method that opens a saved settings file (when clicking file->open), which I am trying to get to open a file automatically when I open another set of files.

So to do this I want to count how many of a specific type of file I have in a single folder, to make sure that there is only 1. There will be other file types in the folder as well, so I need a way to get around them.

I already have the path to the folder, but is there an easy way to search for file types? Perhaps even get their names?

I'm pretty new to MFC, and being as old as it is, I can't find much help in the specific areas I want. Thanks!

edit (for clarification) - For example lets say I have a folder containing 3 word documents and a text file. I want it to be able to search the folder and say that there are 3 files with that are a .docx or 1 file that is a .txt (depending on what I tell it to search for). And it would be nice if I could save the name of one of the found files.

  • Your question is unclear. Are you looking for [this](https://msdn.microsoft.com/en-us/library/windows/desktop/aa365200(v=vs.85).aspx)? – Jabberwocky Jul 18 '17 at 14:47
  • No unfortunately, I don't need to list them, just search for them.Say I have a folder containing 3 word documents and a text file. I want it to be able to search the folder and say that there are 3 files with that are a .docx or 1 file that is a .txt. And it would be nice if I could save the name of one of the found files. – ReddestHorse Jul 18 '17 at 14:55
  • What do you think is the difference between listing files and searching for files? – interjay Jul 18 '17 at 15:02
  • @ReddestHorse so you actually _want_ to list them. – Jabberwocky Jul 18 '17 at 15:06

2 Answers2

0

I hope to resolve it with CFileFind. Below is a sample using CFileFind.

CFileFind finder;
static const TCHAR szFileToFind[] = _T("C:\\WINDOWS\\SYSTEM.INI");

BOOL bResult = finder.FindFile(szFileToFind);

if (bResult)
{
    finder.FindNextFile();

    TRACE(_T("Root of %s is %s\n"), szFileToFind, (LPCTSTR)finder.GetRoot());

    TRACE(_T("Title of %s is %s\n"), szFileToFind,(LPCTSTR)finder.GetFileTitle());

    TRACE(_T("Path of %s is %s\n"), szFileToFind, (LPCTSTR)finder.GetFilePath());

    TRACE(_T("URL of %s is %s\n"), szFileToFind, (LPCTSTR)finder.GetFileURL());

    TRACE(_T("Name of %s is %s\n"), szFileToFind, (LPCTSTR)finder.GetFileName());

    finder.Close();
}
else
{
    TRACE(_T("You have no %s file.\n"), szFileToFind);
}
gwangsoo
  • 9
  • 2
0

Building off of what gwangsoo said, I used CFileFind with GetFilePath and GetFileName.

However, I took a lot from the answer on How to get list of files in a directory programmatically . The differences on mine include implementing CFileFind as well (makes it rather repetitive and inefficient, but it works) so that I can use CString instead of a tchar when finding a substring for the file type I want.

CFileFind finder;
CString find;
find = ".txt";
finder.FindFile(stored_path+"\\*");
WIN32_FIND_DATA search_data;
memset(&search_data, 0, sizeof(WIN32_FIND_DATA));
HANDLE handle = FindFirstFile(stored_path+"\\*", &search_data);
while(handle != INVALID_HANDLE_VALUE)
{
   finder.FindNextFile();
   if (finder.GetFileName().Find (find) != -1)
   {
      save_count++;
      if (save_count == 1)
      {
         save_name = finder.GetFileName();
         save_path = finder.GetFilePath();
      }
   }
   if(FindNextFile(handle, &search_data) == False )
      break;
}

I also included the fourth line finder.FindFile(stored_path+"\*"); as a way to get inside the folder I had currently saved (as stored_path).

Thanks for the help guys!