C++ 遍历文件夹(含详细代码)

遍历当前目录

/****************************************
*   遍历当前目录下的文件夹和文件,默认是按字母顺序遍历
****************************************/
#include<io.h>
#include<iostream>
using namespace std;
bool FindAllFilesOnCurFolder(string path, int &file_num)
{
	_finddata_t file_info;
	//可以定义后面的后缀为*.exe,*.txt等来查找特定后缀的文件,*.*是通配符,匹配所有类型,路径连接符最好是左斜杠/,可跨平台
	string current_path = path + "/*.*";   
	int handle = _findfirst(current_path.c_str(), &file_info);
	//返回值为-1则查找失败  
	if (-1 == handle)
		return false;
	do
	{
		string attribute;
		if (file_info.attrib == _A_SUBDIR) //是目录  
			attribute = "dir";
		else
			attribute = "file";
		
		//获得的最后修改时间是time_t格式的长整型
		cout << file_info.name << ' ' << file_info.time_write << ' ' << file_info.size << ' ' << attribute << endl;  
		file_num++;

	} while (!_findnext(handle, &file_info));
	
	//关闭文件句柄  
	_findclose(handle);
	return true;
}

遍历当前目录及所有子目录

/*
*深度优先递归遍历当前目录下的文件夹、文件及子文件夹和文件
*/
#include<io.h>
#include<iostream>
using namespace std;
void DfsListFolderFiles(string path)
{
	_finddata_t file_info;
	string current_path = path + "/*.*"; 
	int handle = _findfirst(current_path.c_str(), &file_info);
	//返回值为-1则查找失败  
	if (-1 == handle)
	{
		cout << "cannot match the path" << endl;
		return;
	}

	do
	{
		//目录  
		if (file_info.attrib == _A_SUBDIR)
		{
			cout << file_info.name << endl;
			//.是当前目录,..是上层目录,须排除掉这两种情况
			if (strcmp(file_info.name, "..") != 0 && strcmp(file_info.name, ".") != 0)   
				DfsListFolderFiles(path);   
		}
		else
		{
			cout << file_info.name << endl;
		}
	} while (!_findnext(handle, &file_info)); 
   //关闭文件句柄  
	_findclose(handle);
}

重要说明

欢迎大家关注我的个人微信公众号,一起探讨和学习C++后端、客户端的开发知识!
在这里插入图片描述

  • 4
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
### 回答1: #include <stdio.h> #include <dirent.h>int main(int argc, char *argv[]) { DIR *dir; struct dirent *entry; if ((dir = opendir(argv[1])) == NULL) { printf("Error al abrir el directorio.\n"); return 1; } while ((entry = readdir(dir)) != NULL) { printf("%s\n", entry->d_name); } closedir(dir); return 0; } ### 回答2: 以下是一个使用C语言编写的遍历文件夹代码: ```c #include <stdio.h> #include <dirent.h> #include <string.h> void traverseFolder(const char *folderPath) { DIR *dir; struct dirent *entry; // 打开目标文件夹 dir = opendir(folderPath); if (dir == NULL) { printf("无法打开文件夹\n"); return; } // 读取目标文件夹中的每个文件或子文件夹 while ((entry = readdir(dir)) != NULL) { if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) { // 判断是否是文件夹 if (entry->d_type == DT_DIR) { char path[256]; snprintf(path, sizeof(path), "%s/%s", folderPath, entry->d_name); printf("文件夹:%s\n", path); traverseFolder(path); // 递归调用函数遍历文件夹 } else { char path[256]; snprintf(path, sizeof(path), "%s/%s", folderPath, entry->d_name); printf("文件:%s\n", path); } } } // 关闭目标文件夹 closedir(dir); } int main() { const char *folderPath = "/path/to/folder"; // 替换为目标文件夹的路径 traverseFolder(folderPath); return 0; } ``` 该代码首先通过调用`opendir`函数打开目标文件夹,然后使用`readdir`函数逐个读取目录中的文件和子文件夹。然后通过判断`d_type`字段的值,可以确定当前项是文件夹还是文件。如果是文件夹,则使用递归方式调用`traverseFolder`函数遍历该子文件夹。如果是文件,则直接输出文件路径。最后使用`closedir`函数关闭目标文件夹。将`/path/to/folder`替换为实际的目标文件夹路径即可进行测试。 ### 回答3: 下面是一个用 C 语言编写的遍历文件夹代码: ```c #include <stdio.h> #include <dirent.h> #include <string.h> // 递归遍历文件夹 void traverseDir(const char *dir) { DIR *dp; struct dirent *entry; // 打开目录 dp = opendir(dir); if (dp == NULL) { printf("无法打开目录。\n"); return; } // 读取目录中的条目 while ((entry = readdir(dp)) != NULL) { // 忽略 "." 和 ".." 目录 if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) continue; // 拼接文件路径 char path[256]; sprintf(path, "%s/%s", dir, entry->d_name); // 判断是否为目录 if (entry->d_type == DT_DIR) { // 是目录,递归遍历 traverseDir(path); } else { // 不是目录,打印文件名 printf("%s\n", path); } } // 关闭目录 closedir(dp); } int main() { const char *dir = "/path/to/folder"; // 替换为实际的文件夹路径 // 调用遍历函数 traverseDir(dir); return 0; } ``` 以上代码使用递归的方式遍历指定文件夹下的所有文件和子文件夹,并将文件路径打印出来。请将代码中的 `"/path/to/folder"` 替换为实际的文件夹路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值