Thread: Listing sub directories

  1. #1
    Registered User
    Join Date
    Apr 2007
    Sydney, Australia

    Listing sub directories

    I have some code to find all the sub directories of a directory, but have found it can be VERY slow if there are alot of FILES in the sub directories.

    void getSubDirs(char *pDir)
    	char searchdir[256];
    	sprintf(searchdir, "%s*", pDir);
    	WIN32_FIND_DATA filedata;
    	HANDLE hFind = FindFirstFile(searchdir, &filedata);
    			if(filedata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
    				if(filedata.cFileName[0] != '.')
    					char directory[200];
    					sprintf(directory, "%s%s%s", pDir, filedata.cFileName, fSep);
    		}while(FindNextFile(hFind, &filedata));
    Thats the code im using. It obviously iterates through all the files, not just directories which is causing the slowdown. Is there any faster way of listing sub directories?

    Example of output for getSubDirs("world\\");

    Last edited by 39ster; 07-11-2007 at 10:17 PM.

  2. #2
    Registered Abuser
    Join Date
    Jun 2006
    On first glance, I'm guessing because it re-checks the parent directory over and over again. You put in a check to stop it from checking the current directory again ("."), but not the parent directory ("..").

    Edit: d'oh, nm, I see it now.
    Last edited by @nthony; 07-12-2007 at 03:04 PM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > but have found it can be VERY slow if there are alot of FILES in the sub directories.
    There are two things, neither of which are easy to overcome.
    1. Your processor works at <1ns per instruction, and your disk has >1mS seek times (probably closer to 10mS). That's at least a 1E6 difference in performance. Yes, it's going to seem slow every time your code causes the HD to seek somewhere else.

    2. As far as the file system is concerned, it only has directory entries. Each one has to be read in turn, then you test the "dwFileAttributes" to see what you've really got.

    > char searchdir[256];
    Use PATH_MAX (or is it MAX_PATH) to declare the space for pathnames. 200+ chars isn't enough.
    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.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Sydney, Australia

    The code checks the first letter of the folder for a '.'. ".." has a '.' in the first letter so it will be excluded.


    Hmm, well i tried breaking if it isnt a directory (because i figured directories would come first, so if it isnt a directory there must be no more directories in the folder) but that seemed to skip some :/
    Last edited by 39ster; 07-12-2007 at 06:11 AM.

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    There is no guarentee in what order you will receive the files/dirs in a directory, so don't assume any.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. VC6 directories
    By Magos in forum Tech Board
    Replies: 0
    Last Post: 03-11-2005, 05:52 PM
  2. listing files in directories portably
    By ab384 in forum C Programming
    Replies: 2
    Last Post: 11-03-2004, 10:50 AM
  3. listing directories with rwx permission..
    By Mak in forum Linux Programming
    Replies: 5
    Last Post: 03-24-2004, 08:43 PM
  4. Listing file in directories
    By Longie in forum C Programming
    Replies: 6
    Last Post: 09-26-2002, 08:56 AM
  5. Working with directories...
    By C Seņor in forum C Programming
    Replies: 4
    Last Post: 04-20-2002, 11:45 AM