CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Oct 2008
    Posts
    15

    solved: Find all directories in a directory

    hi all,

    i did not find any answer to my following question: how can i find all directories which are inside a specified directory using the MFC.

    I wanted to use a similar approach to finding a file inside a directory but i did not find an analogon to FindFile.
    Sounds strange, but i hope you will understand me!

    Does anybody know how i can realize this ?!


    best regards,
    #50
    Last edited by Raute50; October 30th, 2008 at 10:51 AM. Reason: problem was solved

  2. #2
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Find all directories in a directory

    You keep using FindFile, it also returns directories. Use CFindFile::IsDirectory to check if the returned file is actually a directory.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  3. #3
    Join Date
    Oct 2008
    Posts
    15

    Re: Find all directories in a directory

    i tried to but it did not work ?!

    my code for finding files looks like this:
    Code:
    void CMyDlg::OnButton1() 
    {
    	 WIN32_FIND_DATA FindFileData;
    	 HANDLE hFind;
    	 CString argv = "C:\\MyDirectory\\Textfiles\\*.txt";
    	 hFind = FindFirstFile(argv, &FindFileData);
    	 if (hFind == INVALID_HANDLE_VALUE) 
       {
    		 if (ERROR_FILE_NOT_FOUND == GetLastError())
    			 MessageBox("ERROR_FILE_NOT_FOUND");
       } 
    	 else
    		 {
    		 // List all the files in the directory with some info about them.
    		 LARGE_INTEGER filesize;
    			 CString files = "found these files : \n";
    		 do
    		 {
    				if (!(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
    				{
    					files += FindFileData.cFileName;
    					files += "\n";
    				}
    		 }
    		 while (FindNextFile(hFind, &FindFileData) != 0);
    		 MessageBox(files);
    
    		 DWORD dwError=0;
    		 dwError = GetLastError();
    		 if (dwError == ERROR_NO_MORE_FILES) 
    		 { MessageBox("ERROR_NO_MORE_FILES"); }
    	 }
    }
    this does already work for finding txt files!!!


    i tried to change the CString argv to "C:\\MyDirectory\\Textfiles\\*" or "C:\\MyDirectory\\Textfiles\\*\\"; but both did not work.
    with other things i tried i got errors.

    can you tell me more explicitly how i have to manage this ?!


    best regards,
    #50
    Last edited by Raute50; October 30th, 2008 at 09:52 AM.

  4. #4
    Join Date
    Dec 2007
    Posts
    42

    Re: Find all directories in a directory

    Well,If what you want is to find files in a directory u could do something like

    Code:
    //assign Pathname variable with the full path name
    		//*.log = delete all files that has the .log extension or you could put filename.* to find all the files with that name no matter the extension
    		CString PathName = "C:\\WINDOWS\\*.log";
    
    		//Find the file Info
    		hnd = FindFirstFile((LPCSTR) PathName,&info);
    
    		//Start the loop
    		do
    		{
    
    		//Assign csFile with Full path
    		CString csFile = "C:\\WINDOWS\\";
    
    		//here you can get the files name,size,creating time,attributes,acces time,etc.
    		csFile += info.cFileName;
    
    		//You can delete,move,copy,rename,write,read or what ever you want.
    		DeleteFile(csFile);
    		}
    
    		//While findnextfile keep asking for findnextfile keep looping
    		while( FindNextFile(hnd, &info) );
    
    		//Close file search
    		FindClose(hnd);

    Hope that helped if that's what u where looking for.

  5. #5
    Join Date
    Oct 2008
    Posts
    15

    Re: Find all directories in a directory

    no ... it doesn't help anyway ... because i already know how to find files .... i want to know how i find directories.

    Thats why i chose the title of this thread!

    #50

  6. #6
    Join Date
    Dec 2007
    Posts
    42

    Re: Find all directories in a directory

    Well,Can you post the errors that its giving you?

  7. #7
    Join Date
    Oct 2008
    Posts
    15

    Re: Find all directories in a directory

    the only other way i tried to was "C:\\MyDirectory\\Textfiles\\*\";

    error C2001: newline in constant
    error C2146: syntax error : missing ';' before identifier 'hFind'


    but as i wrote down i understood the error ...


    any hints ?!

    #50

  8. #8
    Join Date
    Oct 2008
    Posts
    15

    Re: Find all directories in a directory

    solving my own problem:

    i used WIN32_FIND_DATA FindFileData; in my code, which has to be replaced by CFileFind FindFileData; !!!
    Afer some more changes and with the right use of FindFileData.IsDirectory() everything works correctly!

    Thanks for all answers ...


    best regards,
    #50

  9. #9
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Find all directories in a directory

    Quote Originally Posted by Raute50
    i tried to but it did not work ?!
    ...
    i tried to change the CString argv to "C:\\MyDirectory\\Textfiles\\*" or "C:\\MyDirectory\\Textfiles\\*\\"; but both did not work.
    with other things i tried i got errors.
    Define "not work".
    Victor Nijegorodov

  10. #10
    Join Date
    Oct 2008
    Posts
    15

    Re: Find all directories in a directory

    one of them caused the following MessageBox
    MessageBox("ERROR_FILE_NOT_FOUND");

    the other caused a MessageBox where the only entry was:
    found these files : \n

    i did not remember which case of "...\\?" caused which MessageBox ?



    hope i could help you ?!


    #50

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured