读取指定文件夹下的所有文件(包含子文件夹中的)并打印生成EXCEL表格

用C++来完成这个功能可能比较麻烦 但目前不熟悉其他语言 只能这样

#include <WINDOWS.H>
#include<iostream>
#include <STRING> 

#include <cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
//以下是文件读入输出需要的头文件
#include<fstream>
#include<cstdlib>
#include<streambuf>

using namespace std;

string Path[1024];
string name[1024];
void g_EnumFile(char* path, int depth, int &cnt)    //不传地址会只会打印最大数目文件夹
{
    string strPath = path;
    //设置通配符,这里*.*搜索所有文件	
    strPath += "\\*.*";
    WIN32_FIND_DATA fd;
    HANDLE hFile = ::FindFirstFile(strPath.c_str(), &fd);
    if (hFile == INVALID_HANDLE_VALUE)
	return;
    while (true)
    {
	//跳过名为'.'和'..'的文件		
	if (fd.cFileName[0] != '.')
	{
	    //如果是目录,递归遍历			
	    if (fd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
	    {
		string nextPath = path;
		nextPath += "\\";
		nextPath += fd.cFileName;
		g_EnumFile((char*)nextPath.c_str(), depth + 1, cnt);
	    }
	    else//不是目录			
	    {
		//输出目录层次的缩进				
		for (int i = 0; i<depth; i++)
		    cout << "    ";
		cout << path << "\\" << fd.cFileName << endl;
		cnt++;    //若不传地址 此处的cnt为该目录下的文件个数   
                          // 最终打印的只有cnt的最大数个文件 且随机 
		Path[cnt] = path;
		name[cnt] = fd.cFileName;
	    }
	}
	if (::FindNextFile(hFile, &fd) == 0)
	    break;
    }
    ::FindClose(hFile);
}

int main()
{
    int count = 0;
    char currentDir[MAX_PATH] = { 0 };
    ::GetCurrentDirectory(sizeof(currentDir), currentDir);
    g_EnumFile("C:\\app\\src\\main\\java", 0, count);    
    g_EnumFile("C:\\app\\src\\main\\jni", 0, count);    //表格中会紧接着最后一行显示 count此时已经不是0

    ofstream ofile;
    ofile.open("sheet.csv", ios::out | ios::trunc);    //表格名字sheet.csv
    ofile << "path" << "," << "name" << endl;
    for (int i = 1; i <= count; i++)
	ofile << Path[i] << "," << name[i] << endl;

    ofile.close();
    ifstream iFile("sheet.csv");
    string readStr((std::istreambuf_iterator<char>(iFile)),std::istreambuf_iterator<char>());
    cout << readStr.c_str();

    cout << count << endl;
    system("pause");
    return 0;
}

下面是打印VS 的可执行文件所在目录下的所有文件

但是   它会打印VS这个文件自带的几个文件夹 

暂时还不知道怎么去修改

//成功
#include <WINDOWS.H>
#include<iostream>
#include <STRING> 
//#include <unistd.h>

#include <cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
//以下是文件读入输出需要的头文件
#include<fstream>
#include<cstdlib>
#include<streambuf>

using namespace std;

string Path[1024];
string name[1024];
void g_EnumFile(char* path, int depth, int &cnt)
{
	//int count[] = {0};
	//int cnt = 0;
	string strPath = path;
	//设置通配符,这里*.*搜索所有文件	
	strPath += "\\*.*";
	WIN32_FIND_DATA fd;
	HANDLE hFile = ::FindFirstFile(strPath.c_str(), &fd);
	if (hFile == INVALID_HANDLE_VALUE)
		return;
	while (true)
	{
		//跳过名为'.'和'..'的文件		
		if (fd.cFileName[0] != '.')
		{
			//如果是目录,递归遍历			
			if (fd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
			{
				string nextPath = path;
				nextPath += "\\";
				nextPath += fd.cFileName;
				g_EnumFile((char*)nextPath.c_str(), depth + 1, cnt);
			}
			else//不是目录			
			{
				//输出目录层次的缩进				
				for (int i = 0; i<depth; i++)
					cout << "    ";
				cout << path << "\\" << fd.cFileName << endl;
				cnt++;
				Path[cnt] = path;
				name[cnt] = fd.cFileName;
			}
		}
		if (::FindNextFile(hFile, &fd) == 0)
			break;
	}
	::FindClose(hFile);
}

int main()
{
	char path[MAX_PATH];
	GetCurrentDirectory(MAX_PATH, path);
	//去掉执行的文件名。
	(strrchr(path, '\\'))[1] = 0;

	int count = 0;
	char currentDir[MAX_PATH] = { 0 };
	::GetCurrentDirectory(sizeof(currentDir), currentDir);
	g_EnumFile(path, 0, count);

	ofstream ofile;
	ofile.open("total.csv", ios::out | ios::trunc);
	ofile << "path" << "," << "name" << endl;
	for (int i = 1; i <= count; i++)
		ofile << Path[i] << "," << name[i] << endl;

	ofile.close();
	ifstream iFile("Statistics.csv");
	string readStr((std::istreambuf_iterator<char>(iFile)), std::istreambuf_iterator<char>());
	cout << readStr.c_str();

	cout << count << endl;
	system("pause");
	return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值