Below is a short code I've extracted from the FAQ on "Viewing files in a directory".
But I have a small problem with the file type. I'm only looking for mp3 files, but if do *.mp3 it can't locate sub directories... How would you suggest for me to do this, I thought about strstr() but it seems like a really lame idea.
Thanks in advance.
Code:
#include <iostream>
#include <windows.h>
using namespace std;
void doit(char *root)
{
HANDLE h;
WIN32_FIND_DATA info;
char newroot[MAX_PATH];
SetCurrentDirectory(root);
if ((h=FindFirstFile("*.mp3", &info)) != INVALID_HANDLE_VALUE)
{
do
{
if (!(strcmp(info.cFileName, ".") == 0 || strcmp(info.cFileName, "..") == 0))
{
if (info.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
{
sprintf(newroot, "%s\\%s", root, info.cFileName);
doit(newroot);
SetCurrentDirectory("..");
}
else
cout << info.cFileName << "\n";
}
} while (FindNextFile(h, &info));
FindClose(h);
}
}
int main()
{
doit("c:\\dir1\\dir2");
return(0);
}