Click to See Complete Forum and Search --> : Looping thru folder with the winapi?


ryanmills
June 8th, 2009, 02:17 AM
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.


#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?

wigga
June 8th, 2009, 07:39 AM
Read on funcction recursion.

ovidiucucu
June 9th, 2009, 04:15 AM
You got only the folder because you searched for the folder itself.
To search the folder contents you have to append \*.* or \*.
SHGetFolderPath(NULL, CSIDL_MYPICTURES, NULL, 0, szPath);
strcat(szPath, "\\*");
// ...
See also this FAQ (http://www.codeguru.com/forum/showthread.php?t=312461).

// to wigga: Please, let's try answer the questions, not shooting in the dark!

wigga
June 9th, 2009, 07:06 PM
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 ???

ovidiucucu
June 10th, 2009, 02:29 AM
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