Thread: Recursive search error

  1. #1
    Registered User surgeon's Avatar
    Join Date
    Jan 2015
    Posts
    26

    Recursive search error

    Hi. I'm trying to search all drives for files and have such code:

    Code:
    int main()
    {
    TCHAR szCurrDrive[] = L"C:\\";
      TCHAR szPath[MAX_PATH+1];
      DWORD i, dwDisksMask = GetLogicalDrives();
    
      for(i = 0; i < 26; i++) {
        if(dwDisksMask & 1) {
          lstrcpy(szPath, szCurrDrive);
          RecursiveSearch(szPath);
        }
        dwDisksMask >>= 1;
        szCurrDrive[0]++;
      }
    /////

    Code:
    VOID RecursiveSearch(LPWSTR szPath) {
      WIN32_FIND_DATA fdFindData;
      HANDLE hFind;
      
      PCWSTR ext1 = L".xls";
      PCWSTR ext2 = L".xlsx";
      TCHAR * CONST lpLastChar = szPath + lstrlen(szPath);
    
      lstrcat(szPath, L"*");
      hFind = FindFirstFile(szPath, &fdFindData);
      
      if(INVALID_HANDLE_VALUE == hFind) {
          DWORD lastError = GetLastError();
          std::cout << lastError;
    
          system("pause");
        return;
      }
    
      do {
          if ((0 == lstrcmp((LPCWSTR)fdFindData.cFileName, L".")) ||
              (0 == lstrcmp((LPCWSTR)fdFindData.cFileName, L".."))) {
          continue;
        }
          lstrcat(szPath, (LPCWSTR)fdFindData.cFileName);
        if(fdFindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
          lstrcat(szPath, L"\\");
          RecursiveSearch(szPath);
        } else {
            if (NULL != StrStr((LPCWSTR)fdFindData.cFileName, ext1) || StrStr((LPCWSTR)fdFindData.cFileName, ext2)
            
          {
            std::wcout<<fdFindData.cFileName<<std::endl;
            FoundFile(fdFindData.cFileName);
            
          }
        }
        
      } while(FindNextFile(hFind, &fdFindData));
    
      FindClose(hFind);
    }
    But receive ERROR_INVALID_NAME error. Where's the problem?

  2. #2
    Registered User
    Join Date
    Jul 2015
    Posts
    37
    The problem is in the first line. It should go:

    TCHAR szCurrDrive[] = L"A:\";

    because GetLogicalDrives starts from drive A.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. recursive linear search in C
    By mike1 in forum C Programming
    Replies: 9
    Last Post: 07-22-2015, 12:05 PM
  2. Recursive Binary Search
    By Tim Arevalo in forum C Programming
    Replies: 5
    Last Post: 07-30-2012, 12:37 PM
  3. recursive search
    By bhorrobi in forum Windows Programming
    Replies: 2
    Last Post: 10-03-2003, 03:41 AM
  4. recursive binary search
    By condorx in forum C Programming
    Replies: 3
    Last Post: 12-03-2002, 12:31 PM
  5. Recursive sequential search
    By supaben34 in forum C++ Programming
    Replies: 2
    Last Post: 10-10-2002, 11:06 AM