Original Post
Hello. Recently I have been trying to make a program that will list all files and sub directories in a given directory. I found some code on an older thread in here and tried to use it. I had to change it around a bit for it to run but it still has issues. The first problem is that the program only seems to find directories and not actual files. The second problem is that the directory names that it does retrieve are nothing but random characters that don't make any sense (like TaSLvpCiiLRsTSxld~~). Here is my code: Thanks
void searchDirectory(string start)
{
bool fFinished;
HANDLE hList;
char szDir[MAX_PATH+1];
char szSubDir[MAX_PATH+1];
char szFileName[MAX_PATH+1];
WIN32_FIND_DATAW FileData;
// Get the proper directory path (doesn't do anything now)
sprintf(szDir, "%s\\*", start);
// Get the first file
hList = FindFirstFileW(L"E:\\*.*", &FileData);
if (hList == INVALID_HANDLE_VALUE)
{
printf("No files found\n\n");
}
else
{
fFinished = false;
while (!fFinished)
{
// Check the object is a directory or not
if (FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
//Make sure directory is not ""
if ((strcmp((const char *)FileData.cFileName, ".") != 0) &&
(strcmp((const char *)FileData.cFileName, "..") != 0))
{
sprintf(szFileName, "\nFound Directory: %s", FileData.cFileName);
printf("%s", szFileName);
}
}
else
printf("%s", FileData.cFileName);
//Check to see if finished
if (!FindNextFileW(hList, &FileData))
{
if (GetLastError() == ERROR_NO_MORE_FILES)
{
fFinished = true;
}
}
}
}
FindClose(hList);
}