Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Counting file names in a directory that match a pattern

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hello,

The heading says it all. I want to count files that match a pattern, say, *.c in a directory. I have to implement this on both AIX and Windows, so any help for these platforms will be appreciated.

Thanks,
SS
 
reading directory is platform dependend procedure.
For UNIX you can use something like that:

#if MACHINE == SUN4_SOLARIS
#include <dirent.h>
#else
#include <sys/types.h>
#endif

#if MACHINE == SUN4_SOLARIS
struct dirent *pFileOfDirectory = NULL;
#else
struct direct *pFileOfDirectory = NULL;
#endif
/* Don't sure about AIX */
{
DIR *pDir = NULL;
pDir = opendir(strDirPath);
if (pDir == NULL)
{
goto error;
}
while ((pFileOfDirectory = readdir(pDir)) != NULL)
{
/* Read file name */
if ((int)strcmp(pFileOfDirectory->d_name, &quot;.&quot;) == 0 ||
(int)strcmp(pFileOfDirectory->d_name, &quot;..&quot;) == 0) continue;

/* Make you filter */
AddByPattern(pFileOfDirectory->d_name,Pattern,ResArrayOfNames);
}
}
For WIN:
#include <stdio.h>
#include <windows.h>
#include <windowsx.h>
#include <tchar.h>

{
WIN32_FIND_DATA sFindData;
HANDLE hFind;

/* remember current directory */
if (!GetCurrentDirectory( 255, (LPSTR)&strCurrentDir) )
{
goto error;
}
/* set Db directory */
if(!SetCurrentDirectory(strDbDirPath))
{
goto error;
}
/* Make sure we've successfully changed to the new directory */
if ( !GetCurrentDirectory( 255, (LPSTR)& strDbName) )
{
goto error;
}

hFind = FindFirstFile (__TEXT(&quot;*.*&quot;), &sFindData);
bDirwalkAgain = ( hFind != INVALID_HANDLE_VALUE ) ;
/* take a file */
while (bDirwalkAgain)
{
if ( strcmp(sFindData.cFileName,&quot;.&quot;) != 0 && strcmp(sFindData.cFileName,&quot;..&quot;) != 0)
{

/* Make you filter */
AddByPattern(sFindData.cFileName, Pattern,ResArrayOfNames);
}

bDirwalkAgain = FindNextFile(hFind, &sFindData);
}
FindClose(hFind); /* close search */
/* Change the default directory back to the original */
SetCurrentDirectory(strCurrentDir);
}

It is not an exact code, but I hope you have got the idea.
 
the answer is findfirst() and findnext() functions in include file
&quot;dir.h&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top