如何获取文件夹中的所有文件及其文件夹的子文件夹名称和路径,以及C ++上的字符串数组? [英] How to get all files in a folder and its subfolders names&paths to string array on c++?

查看:84
本文介绍了如何获取文件夹中的所有文件及其文件夹的子文件夹名称和路径,以及C ++上的字符串数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序,我需要获取所有文件名和字符串数组的路径.这样做可以获取文件和文件夹的路径:

I am making a program and I need to get all files names and paths to string array.I can get files and folders path's by doing so:

HANDLE hFind;
WIN32_FIND_DATA data;
string folder="C:\\*.*";
int num=0;
string addresses[1000];
hFind=FindFirstFile(folder.c_str(),&data);
if(hFind!=INVALID_HANDLE_VALUE){
do{
    addresses[num]=folder.substr(0,folder.length()-3);
    addresses[num]+=data.cFileName;
    num++;
    }while(FindNextFile(hFind,&data));
FindClose(hFind);}

但是它只能获取该文件夹中的文件和文件夹名称路径.我需要获取该文件夹及其子文件夹的所有文件.我该怎么办?如果可能的话,请使用返回字符串数组的函数将其制成.

But it only gets files and folder names paths only in that folder.I need to get all files of that folder and it's subfolders.How can I do it?If possible please make it with function returning string array.

推荐答案

将代码重构为函数,并在获取目录条目时递归调用(请记住跳过.和..).可以通过检查是否在data.dwFileAttributes(data.dwFileAttributes& FILE_ATTRIBUTE_DIRECTORY)中设置了目录位来检测目录.

Refactor your code into function and call it recursively when you get directory entry (remember to skip . and ..). Directory can be detected by checking if directory bit is set in data.dwFileAttributes (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY).

请勿在C:上执行此操作,因为您将需要等待很长时间.为了进行开发,请创建目录C:\ Tests并在其中放置一些文件和文件夹.

Don't do it on C: because you will have to wait a long time. For development create directory C:\Tests and place there few files and folders.

#include <windows.h>
#include <string>
#include <vector>
#include <iostream>

using namespace std;

// TODO: needs proper error checking, for example when given not existing path.

void GetFilesR( vector<string>& result, const char* path )
{
    HANDLE hFind;
    WIN32_FIND_DATA data;
    string folder( path );
    folder += "\\";
    string mask( folder );
    mask += "*.*";

    hFind=FindFirstFile(mask.c_str(),&data);
    if(hFind!=INVALID_HANDLE_VALUE)
    {
        do
        {
            string  name( folder );
            name += data.cFileName;
            if ( data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
            {
                // Skip . and .. pseudo folders.
                if ( strcmp( data.cFileName, "." ) != 0 && strcmp( data.cFileName, ".." ) != 0 )
                {
                    // TODO: if you want directories appended to result, do it here.

                    // Add all files from this directory.
                    GetFilesR( result, name.c_str() );
                }
            }
            else
            {
                result.push_back( name );
            }
        } while(FindNextFile(hFind,&data));
    }
    FindClose(hFind);
}

int main( int argc, char* argv[] )
{
    vector<string>  files;

    // TODO: change directory below.
    GetFilesR( files, "C:\\Tests" );
    // Print collected files.
    for ( vector<string>::iterator i = files.begin() ; i != files.end() ; ++i )
        cout << *i << "\n";
    return 0;
}

如果使用矢量宽度std :: vector,字符串替换为std :: string的所有实例替换为std :: cout的cout,则可以删除使用命名空间std的实例.

using namespace std, can be removed if you replace all instances off vector width std::vector, string with std::string an cout with std::cout.

这篇关于如何获取文件夹中的所有文件及其文件夹的子文件夹名称和路径,以及C ++上的字符串数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆