Directory Listing

This is a discussion on Directory Listing within the C++ Programming forums, part of the General Programming Boards category; How can I get a listing of all files in a directory in Windows. I want to use the smallest ...

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    4

    Directory Listing

    How can I get a listing of all files in a directory in Windows. I want to use the smallest amount of header files and the least amount of code possible, if there's a portable method that'd be great but if it has to be windows specific that's ok. Thanks

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Code:
    #include <windows.h>
    #include <iostream>
    
    int main(int argc, char *argv[])
    {
    	using namespace std;
    	WIN32_FIND_DATA FindFileData;	
    	HANDLE hFind;
    
    	hFind = FindFirstFile("C:\\*.*", &FindFileData);
    	while (hFind != INVALID_HANDLE_VALUE) 
    	{
    		cout << hFind.cFileName << endl;
    		FindNextFile(hFind, &FindFileData);
    	} 
    	cout << "Finished" << endl;
    	return 0;
    }
    Windows specific.
    Last edited by Eibro; 09-08-2002 at 12:01 PM.

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    Code:
    #include <iostream>
    using std::cout;
    using std::endl;
    #include <windows.h>
    
    int main(void){
    
    	WIN32_FIND_DATA wfd = {0}; 
    
    	HANDLE hFile = FindFirstFile("C:\\*.*",&wfd);
    
    	if(hFile == INVALID_HANDLE_VALUE){
    		cout << "Found nothing" << endl;
    		return 1;
    	}
    
    	while(FindNextFile(hFile,&wfd)){
    		if(wfd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
    			cout << "Found folder " << wfd.cFileName << endl;
    		else cout << "Found file " << wfd.cFileName << endl;
    	}
    
    	return 0;
    
    }
    <edit>:: Damn...beaten ::</edit>

  4. #4
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Originally posted by Fordy

    <edit>:: Damn...beaten ::</edit>
    ::Blows smoke from the barrel of revolver::



  5. #5
    Registered User
    Join Date
    Aug 2002
    Posts
    4
    Cool. Thanks much, maybe now i can finish my DND Tools, I needed this to be able to load races, classes, etc. dynamicaly(sp?) from a folder. Ex. Resources\Races\ would cantain a text file for each race and you could add to and edit them from those files. Want a new race just add in a new file with the races attributes, etc. Any Roleplaying fans?

  6. #6
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Code:
    Originally posted by Fordy
    
    while(FindNextFile(hFile,&wfd)) {
      if(wfd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
        cout << "Found folder " << wfd.cFileName << endl;
      else
        cout << "Found file " << wfd.cFileName << endl;
    }
    Just wanted to point out that when you're checking the file attributes, you should say:
    if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
    rather than
    if(wfd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
    because fileattributes is a set of flags and may contain the FILE_ATTRIBUTE_DIRECTORY flag but not be equal to it.
    Last edited by LuckY; 09-09-2002 at 09:34 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Couple errors please help :-D
    By JJJIrish05 in forum C Programming
    Replies: 9
    Last Post: 03-06-2008, 01:54 AM
  2. Listing specific files in a directory
    By knirirr in forum C Programming
    Replies: 14
    Last Post: 01-29-2008, 04:42 AM
  3. Replies: 4
    Last Post: 06-05-2006, 03:24 AM
  4. recursive directory listing help
    By laney69er in forum C Programming
    Replies: 2
    Last Post: 03-13-2006, 12:07 PM
  5. Directory listing
    By brif in forum C++ Programming
    Replies: 1
    Last Post: 10-10-2002, 06:44 AM

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