直接上代码:
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
int API::TraverseDir_Num(const char* strVideoDir)//返回该文件夹的文件个数
{
//遍历目录
static int num = 0;
DIR* dp;
struct dirent *entry;
struct stat statbuf;
dp = opendir(strVideoDir);
if(!dp)
{
LLERROR("无法打开目录:%s", strVideoDir);
return -1;
}
chdir(strVideoDir);
while((entry = readdir(dp)) != nullptr)
{
lstat(entry->d_name, &statbuf);
if(S_ISDIR(statbuf.st_mode))
{
if(!strcmp(".",entry->d_name) || !strcmp("..",entry->d_name))
{
continue;
}
char strNewDir[256];
sprintf(strNewDir, "%s/%s", strVideoDir, entry->d_name);
TraverseDir_Num(strNewDir);
}
else
{
num += 1;
}
};
chdir("..");
closedir(dp);
return num;
}