Thread: Allowing my search function to search sub directories!

  1. #1
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342

    Question Allowing my search function to search sub directories!

    This script works fine, however, it only scans the directory specified, but not it's sub directorys. How can I get it to do that?

    Code:
     
    void FindFileExample ()
    {
      WIN32_FIND_DATA FileData;   // Data structure describes the file found
      HANDLE hSearch;			 // Search handle returned by FindFirstFile
    
      TCHAR szSearchPath[] = TEXT("A:\\Sum_2\\"); 
    
      BOOL bFinished = FALSE;
    
      // Create a new directory.
    
      if (!CreateDirectory (szDirPath, NULL))
      {
    	 Error(TEXT("Unable to create new directory."));
    	return;
      }
    
      // Start searching for .txt files in the root directory.
    
      hSearch = FindFirstFile (szSearchPath), &FileData);
      if (hSearch == INVALID_HANDLE_VALUE)
      {
    		Error(TEXT("No .TXT files found."));
    	return;
      }
    
      // Copy each .txt file to the new directory and change it to
      // read-only, if it is not already read-only.
    
      while (!bFinished)
      {
    	AddStringToData(FileData.cFileName);
    
    	if (!FindNextFile (hSearch, &FileData))
    	{
    	  bFinished = TRUE;
    
    	  if (GetLastError () == ERROR_NO_MORE_FILES)
    	  {
    		Error(TEXT("Found all of the files."));
    	  }
    	  else
    	  {
    		Error(TEXT("Unable to find next file."));
    	  }
    	}
      }
    
      // Close the search handle.
    
      if (!FindClose (hSearch))
      {
    	Error(TEXT("Unable to close search handle."));
    
      }
    }
    Thanks, August.

    btw: I have the functions Error() and AddStringToData() allready declared.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    For the love of all things Holy please post your WINDOWS questions in the WINDOWS board.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    You need to use recursion. First give FindFileExample() a parameter -- the starting path to the files. After finding a file, check its attributes to see if it is a directory. If its a directory, do a recursive call to the function with the new path.
    Code:
    int FindFileExample (string path)
    {
    
     ...
        if( data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
       {
          // this is a directory
          if( strcmp(data.cFileName,".") != 0 && strcmp(data.cFileName,"..) != 0)
          {
               string newpath = path + "\\" + data.cFileName;
                FindFileExample(newpath);
           }
    
       }
    }

  4. #4
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Cool! I didn't know you could make a function call it's self!
    Thanks, Ancient.

    Quote Originally Posted by Thantos
    For the love of all things Holy please post your WINDOWS questions in the WINDOWS board.
    This script runs in a DOS application.
    I am sorry for not posting a DOS program in the WINDOWS board.
    Last edited by Queatrix; 09-30-2005 at 10:12 AM.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > How can I get it to do that?
    My guess is read the F'FAQ
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    does it run in DOS or in a windows console application? I would find it surprising that you are actually running it in DOS. If you are using it as a win32 console application, it can still use the Win32 API and is therefore a windows application. If you are using the FindFirstFile API than it's windows.

  7. #7
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Clearly said, rockytriton. I wish the board guidelines or rules would have been a little more
    specific.

    Sorry about that, Thantos.

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by Cool-August
    Cool! I didn't know you could make a function call it's self!
    Welcome to the world of recursion
    Quote Originally Posted by Cool-August
    Clearly said, rockytriton. I wish the board guidelines or rules would have been a little more specific.
    Its actually pretty easy: If the situation requires you be on a windows OS post it in the windows board. Regardless if its a console or "windowed" app. If the situation requires a *nix OS post it in that board, regardless if its a console or "windowed" app.

  9. #9
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    I got a new problem!

    Now when ever it finds the folder "System Volume Information" it says that "Access is denied". and then it ends my loop. Making it so that the rest of the files that it would have found, remain unfound. How can I prevent this like the Windows Explore Search program avoids it?

    Thanks.

  10. #10
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    I never had that problem, but my login account as Administrative rights. You might try adding FILE_ATTRIBUTE_READONLY to the list. I don't know if this will work for you or not -- you will have to test it.
    Code:
       if( (data.dwFileAttributes & (FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_READONLY) )

  11. #11
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    It only happens on NTFS partions. And I am logged in as the Administrator.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  2. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  3. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM