How do I merge text files in different sub-folders with path in it?

as tile~

so far, I did output a dir.txt that listed all the targets with this CMD code:

D:\>dir /s /b Parent\*.txt >D:\dir.txt


inside dir.txt:

D:\Parent\Sub1\Fbs.txt
D:\Parent\Sub2\2-1\Mgr.txt
D:\Parent\Sub2\2-2\Rdr.txt
D:\Parent\Sub2\2-2\Std.txt
................



but i need the form like this(with # mark and file content):

#### D:\Parent\Sub1\Fbs.txt ####
Fbs: 78952
no comment
#### D:\Parent\Sub2\2-1\Mgr.txt ####
Mgr: 78965
comment: pass
#### D:\Parent\Sub2\2-2\Rdr.txt ####
Rdr: 45852
comment: fail
#### D:\Parent\Sub2\2-2\Std.txt ####
Std: 45857
comment: pass
................



How can I do it with CMD code or bat file?

THX!
std::filesystem::recursive_directory_iterator (C++17)
http://en.cppreference.com/w/cpp/filesystem/recursive_directory_iterator

Visual Studio:
std::experimental::filesystem::recursive_directory_iterator (filesystem TS)
http://en.cppreference.com/w/cpp/experimental/fs

GNU (MinGW):
boost::filesystem::recursive_directory_iterator (if neither of the above is available)
http://www.boost.org/doc/libs/1_63_0/libs/filesystem/doc/reference.html#Class-recursive_directory_iterator

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <vector>
#include <string>
#include <cctype>
#include <fstream>

#include <experimental/filesystem>
namespace fs = std::experimental::filesystem ;

// boost filesystem
// see: http://www.boost.org/doc/libs/1_63_0/libs/filesystem/doc/index.htm
// #include <boost/filesystem.hpp>
// namespace fs = boost::filesystem ;


std::vector<std::string> get_all_text_files( const std::string& path_to_dir )
{
    std::vector<std::string> file_names ;

    using iterator = fs::recursive_directory_iterator ;
    for( iterator iter(path_to_dir) ; iter != iterator {} ; ++iter )
    {
        std::string name = iter->path().string() ;
        // convert name to all lower case. this is ok for NTFS or FAT
        for( char& c : name ) c = std::tolower(c) ;

        if( iter->path().extension() == ".txt" )
            file_names.push_back(name) ;
    }

    return file_names ;
}

int main()
{
    const std::string dir_name = "D:\\Parent" ;
    const std::string out_file_name = "marked_contents.txt" ;

    const std::vector<std::string> file_list = get_all_text_files(dir_name) ;

    std::ofstream out_file( out_file_name, std::ios::app ) ;

    for( const auto& fn : file_list )
    {
        out_file << "#### " << fn << " ####\n" ; // mark
        out_file << std::ifstream(fn).rdbuf() << '\n' ; // file contents
    }
}
Topic archived. No new replies allowed.