Thread: getting a list of files and dirs

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    23

    getting a list of files and dirs

    how would i get a list of files in directorys in any given directory?
    use the findfirstfile and then findnextfile commands with a *.* paramater as the filename? or maybe the ex ones?
    let us eat and drink

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793

    Re: getting a list of files and dirs

    Originally posted by Andrewthegreen
    how would i get a list of files in directorys in any given directory?
    use the findfirstfile and then findnextfile commands with a *.* paramater as the filename? or maybe the ex ones?
    You can use the FindFirstFile & FindNextFile Apis....

    Code:
    #include <windows.h>//For FindFirstFile etc
    #include <iostream>//For cout etc
    #include <string>//For...well...ugh...string!
    using std::string;
    using std::cout;
    using std::cin;
    using std::endl;
    
    int main(int argc,char** argv){
    
    	WIN32_FIND_DATA wfd;//This holds the data on a found file
    	string str;//To hold directory path
    
    	cout << "Enter Directory to list files for" << endl;
    	cin >> str;//Get path
    
    	str += "\\*.*";//Add this to indicate we want all files
    	
    	HANDLE hFile =  FindFirstFile(str.c_str(),&wfd);//Find first
    	if(hFile ==INVALID_HANDLE_VALUE){//If error..abort
    		cout << "Error!! Aborting" << endl;
    		return 1;
    	}
    
    	do{
    		cout << wfd.cFileName << endl;
    	}while(FindNextFile(hFile,&wfd));//Get the rest of the files
    
    	FindClose(hFile);//Clean up!
    	return 0;
    
    }

  3. #3
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    and if you only wanted the files, then just AND the attributes parameter with the FILE_ATTRIBUTE_DIRECTORY, and NOT the whole thing.

    Here's what I mean: (following from Fordy's code)

    Code:
    do{
            if( !(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
                   cout << wfd.cFileName << endl;
    }while(FindNextFile(hFile,&wfd));//Get the rest of the files

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    23
    gosh, thanks! its not really for me, so ill put your names on it
    let us eat and drink

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Directories and files using dirent.h
    By totalnewbie in forum C Programming
    Replies: 6
    Last Post: 11-19-2008, 05:10 PM
  2. Stripping Files out of Dirs
    By kippwinger in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2006, 11:36 AM
  3. Moving files from one directory to multiple dirs
    By csj561 in forum C Programming
    Replies: 7
    Last Post: 03-18-2005, 03:52 PM
  4. Linux, forms, dirs, permissions, cgi-bin, and C
    By ronin in forum C Programming
    Replies: 0
    Last Post: 12-19-2003, 06:10 PM