Thread: File Search - more in depth help needed

  1. #1
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002

    File Search - more in depth help needed

    This code, not written by me just edited, scans the given directory for mp3's, problem is it only scans that folder. IE if you type c:\ and c:\ had immeadiate mp3s, not in folders, it'll pick them up. How can i alter this code so that when c:\ is enterd it scans all the subfolders also, ie searches the entire hard disk. Heres the 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
    	int i = 0;
    	cout << "Enter Directory: ";
    	cin>>str;//Get path
    	str += "\\*.mp3*";//Add this to indicate we want mp3 files
    	HANDLE hFile =  FindFirstFile(str.c_str(),&wfd);//Find first
    	if(hFile ==INVALID_HANDLE_VALUE){//If error..abort
    		cout << "Error!! Aborting" << endl;
    		return 1;
    		cout << wfd.cFileName << endl;
    	}while(FindNextFile(hFile,&wfd));//Get the rest of the files
    	cout<<"\nFound "<<i<<" Files.\n"<<endl;
    	FindClose(hFile);//Clean up!
    	return 0;

  2. #2
    I lurk
    Join Date
    Aug 2002
    Check the dwFileAttributes field in WIN32_FIND_DATA, if the FILE_ATTRIBUTE_DIRECTORY flag is set, then you can enter that directory using cFileName and start searching.

    if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
    cout << "Directory named " << wfd.cFileName << " found!";

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Request for comments
    By Prelude in forum A Brief History of
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM