?
Solved

How to count the number of files in a directory in Microsoft VC++ ?

Posted on 2009-02-12
6
Medium Priority
?
864 Views
Last Modified: 2012-05-06
how do i count number of files in a directory in Visual C++.
0
Comment
Question by:imperialguy
6 Comments
 
LVL 6

Accepted Solution

by:
Mushq earned 375 total points
ID: 23629582
0
 

Author Comment

by:imperialguy
ID: 23629629
hi... thanks for the comment.... isn't there any Win32API that i can use to directly to obtain the file count? the solution given by that "codeproject" website is very huge for the current purpose.
0
 
LVL 16

Expert Comment

by:t0t0
ID: 23630092
It can be don in DOS with:

   FOR /F "TOKENS=1" %A IN ('DIR /A:-D ^| FIND /I "FILE(S)"') DO ECHO %A

or you could get it to actually return the value which can be captured by your program as in:

   FOR /F "TOKENS=1" %A IN ('DIR /A:-D ^| FIND /I "FILE(S)"') DO EXIT /B %A

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:peter41
ID: 23630936
There does not exist direct Win32 function which takes directory name on input and tells you number of items.
Only way in Win32,
is iterating with help of FindFirstFile() and FindNextFile() like introduced in above codeproject.com article. Win32 API is quite low-level, mostly there is needed to write quite much of lines to implemen something.
0
 
LVL 12

Expert Comment

by:Gideon7
ID: 23634512
See code snippet.  It is written for MFC but easily adaptable to pure C.
int KeepNumberOfFiles(LPCTSTR szPrefix, LPCTSTR szSuffix)
{
	CString strPattern = szPrefix;
	strPattern += _T('*');
	strPattern += szSuffix;
 
	WIN32_FIND_DATA fd;
	HANDLE hFind;
 
	if ((hFind = ::FindFirstFile((LPCTSTR)strPattern, &fd)) == INVALID_HANDLE_VALUE) {
		if (::GetLastError() == ERROR_FILE_NOT_FOUND) {
			return 0;
		}
		ThrowWarn(_T("Cannot list files in %s: %s"),
			(LPCTSTR)strPattern, GetLastErrorText());
	}
 
	do {
		if (strcmp(fd.cFileName, _T(".")) == 0 || strcmp(fd.cFileName, _T("..")) == 0) {
			continue;
		}
 
		++nFiles;
 
	} while (::FindNextFile(hFind, &fd));
 
	DWORD dwError = ::GetLastError();
 
	::FindClose(hFind); hFind = INVALID_HANDLE_VALUE;
 
	if (dwError != ERROR_NO_MORE_FILES) {
		::SetLastError(dwError);
		ThrowWarn(_T("Error while enumerating files in %s: %s"),
			(LPCTSTR)strPattern, GetLastErrorText());
	}
 
	TRACE(_T("Found %d files.\n"), nFiles);
 
	return nFiles;
}

Open in new window

0
 

Author Comment

by:imperialguy
ID: 23637194
@Gideon
Guess the only way is to count the files manually using FindFirstFile() and FindNextFile() APIs. You're solution is as good as the one mentioned in codeproject.com site. I think there is no direct API we can use to get the file count. So, this solution is cool.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question