统计c盘的PE文件的个数 (遍历所有文件)

PE文件有    if(tmp=="exe"||tmp=="sys"||tmp=="dll"||tmp=="ocx"||tmp=="com") 这几种格式。

代码如下

View Code
#pragma warning (disable:4786)
#include <iostream>
#include <io.h> // _findfirst() _findnext()
#include <string.h> //strcat()
#include <stdio.h>//gets() puts()
#include <string>
#include <cstring>
#include <direct.h> //_chdir() _getcwd()
#include <vector>
using namespace std;
vector<string>PE;
bool Judge(string path)
{
int i;
if(path.size()<3)return 0;
string tmp;
for(i=path.size()-3;i<path.size();i++)
{
tmp+=path[i];
}
for(i=0;i<tmp.size();i++)
{
if(tmp[i]>='A'&&tmp[i]<='Z')
{
tmp[i]=tmp[i]-'A'+'a';
}
}
if(tmp=="exe"||tmp=="sys"||tmp=="dll"||tmp=="ocx"||tmp=="com") return 1;
else return 0;
}
void DispSubFiles()//遍历目录,包括子目录
{
long handle;
char path[_MAX_PATH];
struct _finddata_t t;
handle=_findfirst("*",&t);
if(handle==-1)//如果handle=-1,表示目录不存在
{
return ;
}
if(t.attrib & _A_SUBDIR)//1. 判断第一个文件:如果为目录
{
if(t.name[0]!='.')//如果目录非空
{
_chdir(t.name);
_getcwd(path,_MAX_PATH);
//puts(path);
DispSubFiles();//递归调用
_chdir("..");//当所有文件遍历完毕,返回上级目录
}
//如果为空目录,即t.name为"."或"..",则不输出
}
else//如果第一个是文件,不为目录
{
_getcwd(path,_MAX_PATH);
strcat(path,"\\");
strcat(path,t.name);
string tmp=path;
PE.push_back(path);

}
while(!(_findnext(handle,&t)))//2. 判断下一个文件,直至遍历所有文件
{
if(t.attrib & _A_SUBDIR)//如果第一个文件为目录
{
if(t.name[0]!='.')//如果目录非空
{
_chdir(t.name);
_getcwd(path,_MAX_PATH);
//puts(path);
DispSubFiles();//递归调用
_chdir("..");//当所有文件遍历完毕,返回上级目录
}
//如果为空目录,即t.name为"."或"..",则不输出
}
else//如果是文件,不为目录,显示文件完整路径
{
_getcwd(path,_MAX_PATH);//获取文件路径
strcat(path,"\\");
strcat(path,t.name);//文件完整路径
string tmp=path;
if(Judge(path)){
//puts(path);//输出路径
PE.push_back(tmp);
}

}
}
_findclose(handle);
}
void main()
{
char dirc[_MAX_PATH];
cout<<"请输入要查找的目录(如:E:\\):"<<endl;
cin>>dirc;
_chdir(dirc);//打开指定目录
DispSubFiles();//遍历所有目录及文件
printf("%d\n",PE.size());
}

统计C盘结果如下:

总文件:192006

PE文件:17386

posted on 2011-11-11 00:05  zhxfl  阅读(1058)  评论(9编辑  收藏  举报

导航