---
title: "MFC: 遍历文件夹"
author: "恶灵挽歌"
date: "2013-04-02T10:04:15Z"
tags: ["C++", "MFC"]
categories: ["编程语言"]
slug: "mfc"
draft: false
lastmod: "2013-04-02T10:07:10Z"
id: 1840552
url: "https://elingwange.iteye.com/blog/1840552"
---

```C++


void RecursiveFindFile(CString strRootPath)
{ 
	/* 
	   主要是CFileFind类的使用。
	   重要方法;
	   FindFile()
	   FindNextFile()
	*/
	// strRootPath 为目录名;
	CFileFind finder;
	CString FilePath;
	if ( strRootPath.Right(1) != "/" )
		strRootPath += "/";
	strRootPath += "*.*";

	BOOL res = finder.FindFile(strRootPath);	// 开始遍历root文件夹下有没有文件或文件夹;
	while ( res )		// res为1，表示仍有nextFile;
	{
		res = finder.FindNextFile();
		FilePath = finder.GetFilePath();

		if ( finder.IsDots() )	continue;		// 如果文件为“.”或“..”，则跳过本次循环;

		if ( finder.IsDirectory() )		// 找到的是文件夹;
		{
			RecursiveFindFile(FilePath);		// 递归;
		}
		else if ( !finder.IsDirectory() )		// 找到的是文件;
		{
			AfxMessageBox(finder.GetFileName());    // 显示文件名
		}
	}
}

```