findfile

#include "stdafx.h"


#include <windows.h>  
#include <stdio.h>  
#include <iostream>  
#include <string>  
using namespace std;  

void findFile(string start)  
{  

	//start 是遍历的目录,如要遍历D盘下test文件夹下的所有文件,那么start = "d:\\test";  
	string strtemp;  
	string token = ".";  
	string tokend = "..";  
	HANDLE hfile;  
	WIN32_FIND_DATAA fileDate;//WIN32_FIND_DATA结构描述了一个由FindFirstFile, FindFirstFileEx, 或FindNextFile函数查找到的文件信息  
	DWORD errorcode = 0;  
	hfile = FindFirstFileA((start + "\\*.*").c_str() ,&fileDate);  
	//通过FindFirstFileA()函数,根据当前的文件存放路径查找该文件来把待操作文件的相关属性读取到WIN32_FIND_DATA结构中去  
	while(hfile!= INVALID_HANDLE_VALUE && errorcode != ERROR_NO_MORE_FILES)  
	{  

		strtemp = fileDate.cFileName;  
		bool flag = false;  
		if((fileDate.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)&&(strtemp != token)&& (strtemp != tokend))  
		{  
			//如果当前文件是目录文件,则递归调用findFile  
			flag = true;  
			cout<<strtemp<<" is a direcotry"<<endl;  
			findFile(start + "\\" + strtemp);  

		}  
		else  
			cout<<strtemp<<endl;//打印文件名  
		bool isNextFile = FindNextFileA(hfile,&fileDate);//判断该目录下是否还有文件  
		if(flag == true && isNextFile == true)//如果还有文件,则调用SetLastError,设为NO_ERROR,这样才能继续遍历后面的文件  
			SetLastError(NO_ERROR);  
		else  
			errorcode=GetLastError();  

	}  

}  
int main()  
{  
	string start="C:\\Program Files";  
	findFile(start);  
	system("pause");
}  

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值