I have a map that is not returning the correct number. First it did, then it didn't now it's just messed up. Any help is appreciated.Thanks.

struct file_data 
{ 
    std::wstring sLastAccessTime; 
    __int64 nFileSize      ; 
};
  
int GetFileList(const wchar_t *searchkey, std::map<std::wstring, file_data> &map) 
{ 
    WIN32_FIND_DATA fd; 
    HANDLE h = FindFirstFile(searchkey,&fd); 
    if(h == INVALID_HANDLE_VALUE) 
    { 
        return 0; // no files found 
    } 
    while(1) 
    { 
       	wchar_t buf[128]; 
        FILETIME ft = fd.ftLastWriteTime; 
        SYSTEMTIME sysTime; 
        FileTimeToSystemTime(&ft, &sysTime); 
        wsprintf(buf, L"%d-%02d-%02d",sysTime.wYear, sysTime.wMonth, sysTime.wDay); 
 
        file_data filedata; 
        filedata.sLastAccessTime= buf; 
        filedata.nFileSize      = (((__int64)fd.nFileSizeHigh) << 32) + fd.nFileSizeLow; 
 
        map[fd.cFileName]= filedata; 
 
        if (FindNextFile(h, &fd) == FALSE) 
            break; 
    } 
    return map.size(); 
} 
 
int main() 
{ 
    std::map<std::wstring, file_data> map; 
    int count = GetFileList(L"C:\\Users\\DS\\Downloads\\*.pdf", map); 
	int count1 = GetFileList(L"C:\\Users\\DS\\Downloads\\*.txt", map); 
	int count2 = GetFileList(L"C:\\Users\\DS\\Downloads\\*.jpg", map);

    for(std::map<std::wstring, file_data>::const_iterator it = map.begin(); 
        it != map.end(); ++it) 
    { 
        		if (count2 != 0) 
		{ 
		printf("\n   Delete: %i   \n", count2);
		} 
		else 
		{ 
		printf ("%s \n", "Nothing");
		} 
	system("pause");
    return 0; 
}
}
Member Avatar for MonsieurPointer

You are forgetting to close your file handle. Add FindClose(h); before return map.size(); (added in code block below).

Also, you are passing the same map (map) in each call, meaning the number will constantly increase in size. Is that by design? If not, you will need to clear the map before each call:

std::map<std::wstring, file_data> map;
int count = GetFileList(L"C:\\Users\\DS\\Downloads\\*.pdf", map);
map.clear();
int count1 = GetFileList(L"C:\\Users\\DS\\Downloads\\*.txt", map);
map.clear();
int count2 = GetFileList(L"C:\\Users\\DS\\Downloads\\*.jpg", map);

Or, alternatively (and easier to maintain - change is in line #9):

int GetFileList(const wchar_t *searchkey, std::map<std::wstring, file_data> &map)
{
WIN32_FIND_DATA fd;
HANDLE h = FindFirstFile(searchkey,&fd);
if(h == INVALID_HANDLE_VALUE)
{
return 0; // no files found
}
map.clear();
while(1)
{
wchar_t buf[128];
FILETIME ft = fd.ftLastWriteTime;
SYSTEMTIME sysTime;
FileTimeToSystemTime(&ft, &sysTime);
wsprintf(buf, L"%d-%02d-%02d",sysTime.wYear, sysTime.wMonth, sysTime.wDay);
 
file_data filedata;
filedata.sLastAccessTime= buf;
filedata.nFileSize = (((__int64)fd.nFileSizeHigh) << 32) + fd.nFileSizeLow;
 
map[fd.cFileName]= filedata;
 
if (FindNextFile(h, &fd) == FALSE)
break;
}
FindClose(h);
return map.size();
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.