您的位置:首页 > 其它

递归搜索指定类型的文件

2009-04-26 13:51 302 查看
// SearchMusic.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <windows.h>
#include <stdlib.h>

long File_Count = 0;
long Dir_Count = 0;
void Dir_A_S(LPCSTR pszDir,LPCSTR pszExt);

int _tmain(int argc, _TCHAR* argv[])
{
//Dir_A_S("C://Documents and Settings//Administrator//");
Dir_A_S("C://Documents and Settings//Administrator//Local Settings//Temporary Internet Files//",".gif");
printf("/nFind file count : %ld/n",File_Count);
printf("/nDirectory count : %ld/n",Dir_Count);
system("pause");
return 0;
}

// 调用方式 Dir_A_S("c://");
void Dir_A_S(LPCSTR pszDir,LPCSTR pszExt)
{
printf("%s/n",pszDir);// 输出路径名字
Dir_Count++;

WIN32_FIND_DATA ffd ;
char szDirFile[1024];
sprintf(szDirFile, "%s*", pszDir);

HANDLE hFind = FindFirstFile(szDirFile, &ffd);
if ( INVALID_HANDLE_VALUE != hFind )
{
char szDir[1024];

if(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY &&
strcmp(ffd.cFileName, ".") &&
strcmp(ffd.cFileName, ".."))
{//确定找到一个目录并且不为 . 或 ..
sprintf(szDir, "%s%s//", pszDir, ffd.cFileName);
Dir_A_S(szDir,pszExt);
}

while( FindNextFile(hFind, &ffd) )
{
if(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY &&
strcmp(ffd.cFileName,".") &&
strcmp(ffd.cFileName,".."))
{//确定找到一个目录并且不为 . 或 ..
sprintf(szDir,"%s%s//",pszDir,ffd.cFileName);
//printf("%s/n",szDir);

Dir_A_S(szDir, pszExt);
}
if( strcmp( ffd.cFileName, ".." ) )
{// 输出文件名
int i = strlen(ffd.cFileName);
char buff[128];
for( int j = 0; j < i; j++ )
{
if( '.' == ffd.cFileName[j] )
{
int k = 0;
while( j < i )
{
buff[k++] = ffd.cFileName[j++];
}
buff[k] = '/0';
break;
}
}
if( 0 == strcmp( buff, pszExt ) )
{
printf("/t%s/n", ffd.cFileName);
File_Count++;
}
}
}
FindClose( hFind );
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: