findnextdirectory?

This is a discussion on findnextdirectory? within the C++ Programming forums, part of the General Programming Boards category; there is a function FindFirstFile() and FindNextFile in win32. I need something similar but with FindFirstDirectory() and FindNextDirectory. Can anyone ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    137

    findnextdirectory?

    there is a function FindFirstFile() and FindNextFile in win32. I need something similar but with FindFirstDirectory() and FindNextDirectory. Can anyone lead me in the right direction. Thank you for your time.

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
    yourWin32FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)

  3. #3
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    The idea is simple. Folders or directories do not have any .extensions. So you could read all files into an array and filter out all the ones with dot extensions.

    However, a more reliable way would be to use this example, taken from adrianxw's site.

    Code:
    #include <windows.h> 
    #include <iostream> 
    using namespace std;
    
    int main()
    {
        HANDLE hFind;
        WIN32_FIND_DATA FindData;
    
        cout << "A very basic FindFirst/Next demo.\n" << endl;
    
        // Find the first file
    
        hFind = FindFirstFile("C:\\*.*", &FindData);
        
    
    
        while (FindNextFile(hFind, &FindData))
        { 
           //cout << FindData.cFileName;
            if (FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
            { 
                cout << FindData.cFileName;
                cout << " File is a directory!";
                cout << endl;
            }
            
        
    
        }
    
    // Close the file handle
    
        FindClose(hFind);
        cin.get();
        return 0;
    }

    Just as a side note you can easily make directory's and remove as well using this example...

    Code:
    /* MAKEDIR.C */ 
    
    #include <direct.h> //need this baby
    #include <stdlib.h> 
    #include <stdio.h> 
    
    int main( ) 
    
    { 
      
        _mkdir( "c:\\treenef" ); 
      
          printf( "Directory '\\treenef' was successfully created\n" ); 
          
          //To remove the directory uncomment below
          //if( _rmdir( "c:\\treenef" ) == 0 ) 
          //printf( "Directory '\\treenef' was successfully removed\n"  ); 
          
       
      
       system("pause");
       return 0; 
    }
    Last edited by treenef; 12-05-2005 at 03:37 AM.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by treenef
    The idea is simple. Folders or directories do not have any .extensions.
    Since when? There are no such restrictions. You can create folder names with extensions if you want to -- and they can have more than one period also. And file and folder names can begin with a period just the same as in *nix. Checking for the absense of the period is no way to determine whether the name is a folder or not.

  5. #5
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Since when? There are no such restrictions.
    Oops.

    Good point. I guess you'll just have to stick to the examples given above then.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21