C++如何统计文件夹下文件个数

如题所述

dir /b | find /v /c "$$$$" > 1.log

该结果统计当前目录下的文件和目录数。

如果只需要文件,使用

dir /b /a-d | find /v /c "$$$$" >1.log
--
※ 来源:·水木社区 newsmth.net·[FROM: 125.46.17.*]

今天去水木看到的.果然强.我来解释一下意思

dir /b 使用空格式(没有标题信息或摘要)。

dir /a-d /a是显示具有指定属性的文件。d是目录,-d就是去掉目录

| 通道符,把dir /b的输出当中后面find的输入

find

/v 显示所有未包含指定字符串的行。

/c 仅显示包含字符串的行数

"$$$$" 特殊字符,一般文件中都没这个字符,不过可以用$$$$来命名文件夹,所以我建议用冒号,这个不能当作文件夹或者文件的名字.

> 输出到

1.log 文件

这个比较好:dir /b | find /v /c ":" > 1.log

=============================

扩展:

包含子目录:

C++代码
tree /F | find /c "." // 带点的文件
tree /F | find /v /c "/" // /是文件命名时禁止使用的,统计全部文件
Linux 下:

C++代码
# ls -l * |grep "^-"|wc -l // to count files
# ls -l * |grep "^d"|wc -l // to count dir
Linux,包括子目录

C++代码
# ls -lR * |grep "^-"|wc -l // to count files
# ls -lR * |grep "^d"|wc -l // to count dir
==================================================

CSDN相关解释:

int count;

int CGetFileCountDlg::GetFileCount(CString szPath)
{
count = 0;
Recurse(szPath);
return count;
}
void CGetFileCountDlg::Recurse(LPCTSTR pstr)
{
CFileFind finder;

// build a string with wildcards
CString strWildcard(pstr);
strWildcard += _T("\\*.*");

// start working for files
BOOL bWorking = finder.FindFile(strWildcard);

while (bWorking)
{
bWorking = finder.FindNextFile();

if (finder.IsDots()) //判断是否是文件
continue;
if(!finder.IsDirectory())
{
count ++;
}
// if it's a directory, recursively search it
if (finder.IsDirectory()) //判断是否是文件夹 //路径
{
CString str = finder.GetFilePath();
Recurse(str);
}

}

finder.Close();
}

count 就是目录下文件的个数

----------------------------------------------------------------------------

SDK的

HANDLE hFind;
WIN32_FIND_DATA dataFind;
BOOL bMoreFiles=TRUE;
int iCount=0;//统计文件数的变量

//m_strDir就是你要指定的路径
hFind=FindFirstFile(m_strDir+"\*.*",&dataFind);//找到路径中所有文件

//遍历路径中所有文件
while(hFind!=INVALID_HANDLE_VALUE&&bMoreFiles==TRUE)
{
if(dataFind.dwFileAttributes!=FILE_ATTRIBUTE_DIRECTORY)//判断是否是文件
{
iCount++;
}
bMoreFiles=FindNextFile(hFind,&dataFind);
}
FindClose(hFind);
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2016-09-24
int count;

int
CGetFileCountDlg::GetFileCount(CString szPath)
{
count
= 0;
Recurse(szPath);
return count;
}
void CGetFileCountDlg::Recurse(LPCTSTR pstr)
{
CFileFind finder;

// build a string with
wildcards
CString strWildcard(pstr);
strWildcard +=
_T("\\*.*");

// start working for files
BOOL bWorking = finder.FindFile(strWildcard);

while (bWorking)
{
bWorking =
finder.FindNextFile();

if (finder.IsDots())
//判断是否是文件
continue;
if(!finder.IsDirectory())
{
count ++;
}
// if it's a
directory, recursively search it
if (finder.IsDirectory())
//判断是否是文件夹 //路径
{
CString str =
finder.GetFilePath();
Recurse(str);
}

}

finder.Close();
}

count 就是目录下文件的个数

SDK的

HANDLE hFind;
WIN32_FIND_DATA dataFind;
BOOL bMoreFiles=TRUE;
int iCount=0;//统计文件数的变量

//m_strDir就是你要指定的路径
hFind=FindFirstFile(m_strDir+"\*.*",&dataFind);//找到路径中所有文件

//遍历路径中所有文件
while(hFind!=INVALID_HANDLE_VALUE&&bMoreFiles==TRUE)
{
if(dataFind.dwFileAttributes!=FILE_ATTRIBUTE_DIRECTORY)//判断是否是文件
{
iCount++;
}
bMoreFiles=FindNextFile(hFind,&dataFind);
}
FindClose(hFind);
第2个回答  推荐于2017-12-15
就是不能确定文件夹内文件数目,所以在第一句加了个set,每次要改数字,非常只是要得到某文件夹下的文件数的话我想可以使用dir命令来实现假设目录为C:\\,luRWQh本回答被网友采纳
第3个回答  2012-05-29
没听懂什么意思啊
相似回答