CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2007
    Posts
    16

    Looping thru folder with the winapi?

    I have a simple function to loop thru the files but for some reason I am only listing the folder its self. I'm sure this is not the best way to do this but does anything jump out as wrong with this. The goal is just to list the file names in the "My Pictures" folder.

    Code:
    #include <iostream>
    #include <windows.h>
    #include <shlwapi.h>
    
    TCHAR szPath[MAX_PATH];
    WIN32_FIND_DATA ffd;
    LARGE_INTEGER filesize;
    HANDLE hFind = INVALID_HANDLE_VALUE;
    DWORD dwError=0;
    
    int main(int argc, char *argv[])
    {
    	
    	SHGetFolderPath(NULL, CSIDL_MYPICTURES, NULL, 0, szPath);
    	
    	std::cout << "searching: " << szPath << std::endl << std::endl;
    	
    	hFind = FindFirstFile(szPath, &ffd);
    
    	if (INVALID_HANDLE_VALUE == hFind) 
    	{
    		std::cout << "folder not found" << std::endl;
    	} 
    
       do
       {
    
    		if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
    		{
    			std::cout << "dir: " <<ffd.cFileName << std::endl;
    		}
    		else
    		{
    			filesize.LowPart = ffd.nFileSizeLow;
    			filesize.HighPart = ffd.nFileSizeHigh;
    			std::cout << "file: " << ffd.cFileName << " size: " << filesize.QuadPart << std::endl;
    		}
    	}
    	while (FindNextFile(hFind, &ffd) != 0);
       
    	return 0;
    }
    
    // returns
    //searching: C:\Documents and Settings\ryanm\My Documents\My Pictures
    
    //dir: My Pictures
    The SHGetFolderPath call is returning "C:\Documents and Settings\ryanm\My Documents\My Pictures" however when I point FindFirstFile to that folder it loops once on that folder and only returns that folder. When it should be returning the files inside the folder, even if it was returning the folder below it there should have been more folders but its only seeing "My Pictures". Ideas?

  2. #2
    Join Date
    May 2009
    Location
    Netherlands
    Posts
    103

    Re: Looping thru folder with the winapi?

    Read on funcction recursion.

  3. #3
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,244

    Re: Looping thru folder with the winapi?

    You got only the folder because you searched for the folder itself.
    To search the folder contents you have to append \*.* or \*.
    Code:
       SHGetFolderPath(NULL, CSIDL_MYPICTURES, NULL, 0, szPath);
       strcat(szPath, "\\*");
       // ...
    See also this FAQ.

    // to wigga: Please, let's try answer the questions, not shooting in the dark!
    Last edited by ovidiucucu; June 9th, 2009 at 03:34 AM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  4. #4
    Join Date
    May 2009
    Location
    Netherlands
    Posts
    103

    Re: Looping thru folder with the winapi?

    in response to offense. As i am not just shoting in the dark. I was hinting him on how to recurse through folders.

    http://www.codeguru.com/forum/showthread.php?t=478427

    than again im coonfused if he needs to loop through folders or not ???

  5. #5
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,244

    Re: Looping thru folder with the winapi?

    Quote Originally Posted by wigga View Post
    in response to offense. As i am not just shoting in the dark. I was hinting him on how to recurse through folders.

    http://www.codeguru.com/forum/showthread.php?t=478427

    than again im coonfused if he needs to loop through folders or not ???
    [ off-topic ]

    Dear Mr. Wigga,

    I humbly have to inform you that "Read on function recursion" doesn't offer much more information than "Do a google search".
    Beside that, respectfully, I have a vague feeling that You are a little bit confused about the original post problem.
    Thank you for your time and sorry for any inconvenience!

    Faithfully yours,
    Ovidiu Cucu
    Codeguru power poster, article reviewer, and super moderator


    Well, I was just joking.
    The above "Shooting in the dark" is just informal and not offensive.
    I hope you can understand and listen from time to time the older/more experienced people observations. This way can imporove your own posts as well.

    Regards,
    Ovidiu
    Last edited by ovidiucucu; June 10th, 2009 at 01:32 AM.

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