现在的位置: 首页 > 综合 > 正文

取得指定路径下所有文件与文件夹

2018年06月06日 ⁄ 综合 ⁄ 共 1003字 ⁄ 字号 评论关闭

方法1:

BOOL GetFileList(CString strSourceDir, CString fileListPath)
{
	//保存文件列表
	CString strFileList = ""; 

	//要查找的目录
	strSourceDir = strSourceDir + "*.*";

	_finddata_t file; 
	long longf; 

	if((longf = _findfirst(strSourceDir, &file))==-1l) 
	{
		return FALSE;
	} 
	else 
	{ 
		string tempName; 
		while( _findnext(longf, &file ) == 0) 
		{ 
			tempName = ""; 
			tempName = file.name; 
			if (tempName == "..") 
			{ 
				continue; 
			} 

		//保存文件名
		strFileList.Insert(strFileList.GetLength(),file.name);
		strFileList.Insert(strFileList.GetLength(), "\r\n"); 
		} 
	} 

	_findclose(longf); 

	CFile txtFile; //文件列表写TXT文件
	txtFile.Open(fileListPath,CFile::modeCreate|CFile::modeWrite);
	txtFile.Write(strFileList,strFileList.GetLength()); 
	txtFile.Close();

	return TRUE;
}

方法二:

int _tmain(int argc, _TCHAR* argv[])
{
	SHFILEINFO sfi;   
	CFileFind* fileFind=new CFileFind();   
	BOOL res = fileFind->FindFile("C:\\Users\\xiexiangming\\Desktop\\SML\\*");   
	int i=0;   
	while(res)   
	{   
		res = fileFind->FindNextFile();   
		if(!fileFind->IsDirectory())   
			  continue; 
		SHGetFileInfo(fileFind->GetFilePath(),0,&sfi,sizeof(sfi),SHGFI_ICON);
		printf("\n%s",fileFind->GetFileName());
	}  
	system("pause");
	return 0;
}

抱歉!评论已关闭.