#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    São Paulo - Brazil
    Posts
    2
    Rep Power
    0

    Question How to retrieve only the subdirectories in a directory?


    Hello all... My name is Ricardo, and i´m a youger programmer from São Paulo , Brazil.

    I have a little question that im unable to solve here.

    How can i retrieve only the directories string names from a directory?

    Im using the DIR * structure, in ident.h .

    Well, here is an example code for dos prompt that give me all the files and directories in a directory:

    void PrintDirectory (const char* DirName)
    {
    DIR * dir = opendir(DirName);
    readdir(dir);

    while( dir->d_handle != -1 )
    {
    cout << dir->dd_dir.d_name << "\n";
    readdir(dir);
    }
    }

    This simple routine give me all the files and directories in the given path, but i need only the directories.... There is any way to retrieve the directories only, without testing the name strings one by one, causing a huge unecessary computational process?

    Is there any function that do that?
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,593
    Rep Power
    4207
    Depends on the OS you're using, but some of them (FreeBSD) have a member field in dirent called d_type that specifies what kind of filetype this is.

    If not, you can always use fstat(2) to figure out what the file type is.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    São Paulo - Brazil
    Posts
    2
    Rep Power
    0
    Thanks for the reply...

    What i need to do exactly is open all my local disc ( C:\ ) on a window TreeView. For this, i will create a recursive algorithm to insert all the directories and subdirectories of my local drive in the treeview, in their exact position, exactly like Windows Explorer do.

    But , like Windows Explorer, i want to insert only directories in the treeview, not files... But how to retrieve the directories string names only? if i test all the names to find only the directory ones, it will crash my machine with computational process, and it will read EVERYTHING on my hard drive before load the treeview.

    How to solve this problem???

    Thanks All
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,593
    Rep Power
    4207
    If you're doing this for Windows, why not use FindFirstFile(), FindNextFile() and FindClose(). I'd posted some specific code in VC++ to find directories using these functions some time ago. See Traverse an NTFS Directory for more.

    >> if i test all the names to find only the directory ones, it will crash my machine with computational process, and it will read EVERYTHING on my hard drive before load the treeview.

    This is the reason why Windoze Explorer doesn't recurse into ALL directories ahead of time. If you notice, it only reads the top level of directories ahead of time and when you click on a directory, it then reads all the subdirectories within that directory. If you want to implement something like this, see http://www.codeproject.com/treectrl/pathpicker.asp for an example.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo

IMN logo majestic logo threadwatch logo seochat tools logo