Click here to Skip to main content
15,889,403 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This function is returning a count of files in the directory.....However If the file doesn't exsist the function breaks. How can I add NULL to this to cover no files of the type desired. I want it to return 0 if no files are found.




C++
int count = GetFileList(L"C:\\Users\\DS\\Downloads\\*.pdf", map);


The function itself.
C++
int GetFileList(const wchar_t *searchkey, std::map &map)


My apologies, here is the entire code.

C++
struct file_data 
{ 
    std::wstring sLastAccessTime; 
    __int64 nFileSize      ; 
};
  
int GetFileList(const wchar_t *searchkey, std::map &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() 
{ 
#define NULL 0;
	std::map 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::const_iterator it = map.begin(); 
        it != map.end(); ++it) 
    { 
       
		if (count != 0) 
		{ 
		printf("\n   Delete: %i   \n", count);
		} 
		else 
		{ 
		printf ("%s \n", "Nothing");
		
		    } 
	system("pause");
    return 0;
}
}
Posted
Updated 30-Sep-11 10:00am
v3
Comments
PJ Arends 30-Sep-11 15:46pm    
What is the code for the GetFileList function?

Member 7766180 wrote:
I want it to return 0 if no files are found.
This code already ensure 0 is returned when no files are returned.
C++
if(h == INVALID_HANDLE_VALUE) 
{ 
    return 0; // no files found 
} 

You should change your main() to check for the number of found files before using the map in the for loop.

Try this:
C++
GetFileList(L"C:\\Users\\DS\\Downloads\\*.pdf", map);
GetFileList(L"C:\\Users\\DS\\Downloads\\*.txt", map);
GetFileList(L"C:\\Users\\DS\\Downloads\\*.jpg", map);

if( map.size() > 0 )
{
	for(std::map::const_iterator it = map.begin(); it != map.end(); ++it)
	{
		....
	}
}


Also you need to close the find handle before the GetFileList functions returns.
Add FindClose(h); before return map.size();
 
Share this answer
 
v5
Comments
Member 7766180 30-Sep-11 16:12pm    
So just mone this
if(h == INVALID_HANDLE_VALUE)
{
return 0; // no files found
}
to just under main() But will it cover the 3 differnt file types. because right now it fails if one of the file types is null. I still think the map is not syntaxed properly as well. Still new. So.......thank you for your help Andre.
André Kraak 30-Sep-11 16:18pm    
I have updated the solution.
Member 7766180 30-Sep-11 16:35pm    
I tried it and it returns 11. It should be 4. I have 3 txt and 1 jpg. 0 pdf's.
Member 7766180 30-Sep-11 16:36pm    
If I remove the += and just use =. I get 8.
André Kraak 30-Sep-11 16:41pm    
My mistake, because the map is being added to each call of GetFileList the count variable is being incrementally increased.

We should use map.size() to check if any files have been found.

I have changed the solution.
Er, ahrm - perhaps you'd care to edit the question so that it actually includes the function in question, rather than an (incomplete) definition of it? Just a thought...
 
Share this answer
 

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