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

Posted on 2009-02-12
Last Modified: 2012-05-06
how do i count number of files in a directory in Visual C++.
Question by:imperialguy
    LVL 6

    Accepted Solution


    Author Comment

    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.
    LVL 16

    Expert Comment

    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

    LVL 6

    Expert Comment

    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 article. Win32 API is quite low-level, mostly there is needed to write quite much of lines to implemen something.
    LVL 12

    Expert Comment

    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) {
    	} while (::FindNextFile(hFind, &fd));
    	DWORD dwError = ::GetLastError();
    	::FindClose(hFind); hFind = INVALID_HANDLE_VALUE;
    	if (dwError != ERROR_NO_MORE_FILES) {
    		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


    Author Comment

    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 site. I think there is no direct API we can use to get the file count. So, this solution is cool.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
    Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode ( They will have you believe that Unicode requires you to use…
    The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now