I'm trying to add the file size to this map. It looks like I'm making a mess of things. Any help is appreciated. Thank you.
int GetFileList(const wchar_t *searchkey, std::map<std::wstring, std::wstring> &map)
{
WIN32_FIND_DATA fd;
HANDLE h = FindFirstFile(searchkey,&fd);
if(h == INVA开发者_如何学CLID_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);
map[fd.cFileName] = buf;
map[fd.nFileSizeHigh] = buf;
map[fd.nFileSizeLow] = buf;
if(FindNextFile(h, &fd) == FALSE)
break;
}
return map.size();
}
void main()
{
std::map<std::wstring, std::wstring> map;
int count = GetFileList(L"C:\\Users\\DS\\Downloads\\*.zip", map)
&& GetFileList(L"C:\\Users\\DS\\Downloads\\*.txt", map);
for(std::map<std::wstring, std::wstring>::const_iterator it = map.begin();
it != map.end(); ++it)
{
//MessageBoxW(NULL,it->first.c_str(),L"File Name",MB_OK);
//MessageBoxW(NULL,it->second.c_str(),L"File Date",MB_OK);
}
}
Well, you need to decide what you're mapping from, and what you're mapping to.
Probably you want to map from file-name to struct {file-size, file-time}.
Keeping it similar to your code:
struct file_data
{
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;
GetFileList(L"C:\\Users\\DS\\Downloads\\*.zip", map);
GetFileList(L"C:\\Users\\DS\\Downloads\\*.txt", map);
for(std::map<std::wstring, file_data>::const_iterator it = map.begin();
it != map.end(); ++it)
{
MessageBoxW(NULL,it->first.c_str(),L"File Name",MB_OK);
MessageBoxW(NULL,it->second.sLastAccessTime.c_str(),L"File Date",MB_OK);
}
return 0;
}
精彩评论