Click here to Skip to main content
15,893,622 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
This code runs and works well, but I don't really want it to give me a messagebox with names. I want to get a count instead of the number of files there are instead. How can I rework this so that I get a count? Thank you.

C++
int GetFileList(const wchar_t *searchkey, std::map<std::wstring,> &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);
        map[fd.cFileName] = buf;
        if(FindNextFile(h, &fd) == FALSE)
            break;
    }
    return map.size();
}

void main()
{ 
    std::map<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,>::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);
    }
} 
Posted
Comments
Sergey Alexandrovich Kryukov 28-Sep-11 19:11pm    
Voted 1 and reported as abuse. We should not help people who do not go in software development but trying to use codes they cannot even read properly. It leads nowhere.
--SA
Chuck O'Toole 30-Sep-11 21:31pm    
SA, Did you ever get any feedback on the abuse report? Just how do the "powers that be" followup on such reports? The volume of new questions posted about the same old 10 or 20 lines of code seems to support your report and I was wondering what happens next.
Member 7766180 30-Sep-11 22:44pm    
Are you kidding me? I thought you were trying to help me? How is this abuse? I'm trying to learn and do something and you want to report me. Shame on you!!!!! I'm jumping through all of your hoops to learn, you said I was learning and then.....want to get rid of me? Post the code to my problem and I'm outta here!!!!!!!
Kode King 30-Sep-11 23:23pm    
Chuck O'Toole....Your next! You are no better, Ive been watching you as well. Maybe if you would help with some actual code, people could learn. Instead you take people thru one rabbit hole to the next. Terrible!
Chuck O'Toole 1-Oct-11 10:00am    
I don't know you, you don't know me. I assume you are familiar with the "Give a man a fish" vs. "Teach a man to fish" example. I spent a lot of time posing questions to lead that person to the logic of debugging on his own. If all I did was give him answers, he wouldn't learn anything. I consider that helping. If I just gave him the code, he'd be back asking for yet another piece of code. So, after 2 days of exchanges designed for him to discover the answer himself, what you call the rabbit hole, he finally claims to have understood. And then he goes and ask 3 more questions, pasting the same 20 lines of code over again, and asking questions that clearly demonstrate that he doesn't have the basest understanding of programming, despite his claims of knowing VB and has done programming in the past. Lots of people who've been around alot longer than you or he have suggested that he go and actual read a book on programming and do the exercises in the book rather that go off and grab some "IP Packet Sniffer" off the net and try to make it work. Same with this "delete files created within the last minute" project. There's a reason you don't just give matches to children, you teach them about fire first. If he chooses to continue to engage people until they give him the code, he can do that without me. I earnestly tried and have given up. PS, if you saw The Matrix, you'd know the rabbit hole is where the truth was. Not necessarily a bad destination.

1 solution

You already got count. Remove a call to a MessageBoxW and re-think your life. Seriously.

—SA
 
Share this answer
 
Comments
Member 7766180 28-Sep-11 19:18pm    
Thank you for your answer.
Sergey Alexandrovich Kryukov 28-Sep-11 19:19pm    
You are very welcome, I only sorry you don't follow my advice...
--SA
Member 7766180 28-Sep-11 19:30pm    
Just a quick one. I want to see the value of count, so I tried this and it didn't work.
if (count != 0)
{
printf ("%s \n", "Delete",count);

}
else
{
printf ("%s \n", "Nothing");

Thank you so much!
Chuck O'Toole 28-Sep-11 19:36pm    
printf ("%s \n", "Delete",count);

Has a format for only 1 argument, a string (%s) yet you are trying to print two values, "Delete" and count. Where is the format specifier that would output count?
Member 7766180 28-Sep-11 19:46pm    
I tried this and it returned 1. I want to get the total count, not the count of each line.
printf("\n Delete: %i", count); I'm getting several lines.....
Delete: 1
Delete: 1
Delete: 1

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900