1

i have a program that successfully searches for the files of a particular extension in a specific drive (i am using windows), let us say it's "C:\" and then print these files on console. now i want to generalize my program. i want that my program will search in all partitions (drives) .. how i can do this.. which function should i use..here is my code sample

int SearchDirectory(std::vector<std::string> &refvecFiles,
                const std::string        &refcstrRootDirectory,
                const std::string        &refcstrExtension,
                bool                     bSearchSubdirectories = true)
{
  std::string     strFilePath;             // Filepath
  std::string     strPattern;              // Pattern
  std::string     strExtension;            // Extension
  HANDLE          hFile;                   // Handle to file
  WIN32_FIND_DATA FileInformation;         // File information


 strPattern = refcstrRootDirectory + "\\*.*";

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

    if(FileInformation.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
    {
      if(bSearchSubdirectories)
      {
        // Search subdirectory
        int iRC = SearchDirectory(refvecFiles,
                                  strFilePath,
                                  refcstrExtension,
                                  bSearchSubdirectories);
        if(iRC)
          return iRC;
      }
    }
    else
    {
      // Check extension
      strExtension = FileInformation.cFileName;
      strExtension = strExtension.substr(strExtension.rfind(".") + 1);

      if(strExtension == refcstrExtension)
      {
        // Save filename
        refvecFiles.push_back(strFilePath);
      }
    }
  }
} while(::FindNextFile(hFile, &FileInformation) == TRUE);

// Close handle
::FindClose(hFile);

DWORD dwError = ::GetLastError();
if(dwError != ERROR_NO_MORE_FILES)
  return dwError;
  }

  return 0;
}


 int main()
{
      int                      iRC         = 0;
  std::vector<std::string> vecAviFiles;
  std::vector<std::string> vecTxtFiles;


  // Search 'c:' for '.avi' files including subdirectories
   iRC = SearchDirectory(vecAviFiles, "c:", "apk");
  if(iRC)
 {
    std::cout << "Error " << iRC << std::endl;
   return -1;
  }

  // Print results
  for(std::vector<std::string>::iterator iterAvi = vecAviFiles.begin();
  iterAvi != vecAviFiles.end();
  ++iterAvi)
std::cout << *iterAvi << std::endl;

  // Search 'c:\textfiles' for '.txt' files excluding subdirectories
  iRC = SearchDirectory(vecTxtFiles, "c:", "txt", false);
  if(iRC)
 {
     std::cout << "Error " << iRC << std::endl;
    return -1;
 }

 // Print results
  for(std::vector<std::string>::iterator iterTxt = vecTxtFiles.begin();
    iterTxt != vecTxtFiles.end();
     ++iterTxt)
     std::cout << *iterTxt << std::endl; 

  // Wait for keystroke
 _getch();

 return 0;
}
hmjd
  • 120,187
  • 20
  • 207
  • 252

1 Answers1

1

I'm no Windows programmer but it looks like you have to find out what drives are available in your system and call your function for all of them. See here how to get the list of available drives on Windows: Enumerating all available drive letters in Windows

Community
  • 1
  • 1
piokuc
  • 25,594
  • 11
  • 72
  • 102
  • 1
    Your answer is simply a link to a question that asks the same as this one. Why didn't you vote to close as a dupe and instead chose to write an answer that gives no details and is a link-only answer? – David Heffernan Aug 07 '13 at 09:45
  • It's *almost* the same. Calling the function several times for each drive may seem obvious for me and and you, but maybe not for the person who asked the question. – piokuc Aug 07 '13 at 09:47
  • as one of the user answer my question that i should use GetLogicalDrives method which returns all the drive letters in a buffer, so i used char buff[10]=GetLogicalDrives(); in my program but compiler gives me "invalid initializer " error what i can do now? – user2660085 Aug 07 '13 at 09:57
  • You don't use it correctly. The function GetLogicalDrives returns a mask with bits representing availability of drives. Here is an example of how to decode the bits and print out the names of drives: http://www.tenouk.com/cpluscodesnippet/getlogicaldrives.html Instead of printing you need to put it to string (with, say, sprintf ) and call your function – piokuc Aug 07 '13 at 10:03