// 函数功能:递归遍历文件
#pragma warning(disable:4996)
#include <stdio.h>
#include <string.h>
#include <io.h>
int nFileNum;
// 函数功能:浏览文件
void scanfile(char* pPath, char* pName)
{
char strFileName[256];
nFileNum++;
strcpy(strFileName, pPath);
strcat(strFileName, pName);
printf(“%s/n”, strFileName);
}
// 函数功能:浏览目录,递归
void scandir(char *strCurPath)
{
char strTemp[2048];
struct _finddata_t ffblk;
int nReturn = 0;
strcpy(strTemp, strCurPath);
strcat(strTemp, “*.*”);
long lHandle = long(_findfirst(strTemp, &ffblk));
while (lHandle != -1 && nReturn != -1)
{
if(strcmp(ffblk.name, “.”) == 0 || strcmp(ffblk.name, “..”) == 0)
{
nReturn = _findnext(lHandle, &ffblk);
continue;
}
if (ffblk.attrib & _A_SUBDIR)
{
// Is Dir
strcpy(strTemp, strCurPath);
strcat(strTemp, ffblk.name);
strcat(strTemp, “//”);
scandir(strTemp);
}
else
{
// Is File
scanfile(strCurPath, ffblk.name);
}
nReturn = _findnext(lHandle, &ffblk);
}
_findclose(lHandle);
}
void main()
{
nFileNum = 0;
scandir(“c://”);
printf(“/nTotal files: %d/n”, nFileNum);
}
评论0