Loop over folders

Dec 14, 2016 at 12:24am
In my problem I have set of folders called Job1, Job2, Job3,......Jobn. In every Job folder there's another set of sub folders called Run1, Run2, .....Runm. Evrey "Run" folder has a file called "myFile.dat", which is the file I want to read and process.

I want to write a code to read every myFile.dat files. Simply I want to read "myFile.dat" in "Run1" folder in the "Job1" folder, then "myFile.dat" file in the "Run2" folder in the "Job1" folder and so on.

If anyone could help figure it out that would be appreciated
Dec 14, 2016 at 1:01am
closed account (4NqL8vqX)
What is your assignment all about?
Dec 14, 2016 at 1:16am
Hi Kabane,

I have to do a simulation. Simulation will process set of output files for different inputfiles. For a one set of parameters I want to run the simulation for "m" times. Therefore there are "m: number of "Run" folders which contains the outputfile I want (myFile.dat).

And I want to run the simulation for "n" number of paramaters. Therefore there are "n" number of Jobs.

I want to analyse output data. Before analysing I want to calculate averages of some variables in the "myFile.dat" file and copy all the data into a one ouput file. And want to record corresponding job number and run number as well.

I'm new to c++. Please help me to solve this.
Dec 14, 2016 at 1:51am
Hi, here is an example of how to read integers from a file.

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
#include<iostream> // Input/Output stream header.
#include<fstream> // File stream header.
// Namespace standard
using namespace std;
// Main function.
int main()
{
	// Variable declarations
	int i;
	ifstream infile; // Declare an input file variable called infile.
	infile.open("file_name.type"); // Open the file in the same path as executable
	// File test.
	if (infile.fail())
	{
	 	cout << "Input file did not open." << endl;
	 	exit(-1);
	}
	// Reads the first integer in the file.
	infile >> i;
	// Read loop.
	while(infile)
	{
		// Stuff goes here
		infile >> i; // Read again
	}
	// Closes the file once it is done reading.
	infile.close();
	
	return 0;
}


I would make extensive use of loops in your case. Other than read such and such file as shown above.
Last edited on Dec 14, 2016 at 2:00am
Dec 14, 2016 at 2:09am
Something along these lines, perhaps:

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
#include <iostream>
#include <string>
#include <fstream>
#include <experimental/filesystem> // use microsoft's standard C++ library
// #include <boost/filesystem.hpp> // use the boost filesystem library.
                                   // link with libboostfilesystem, libboostsystem

// http://en.cppreference.com/w/cpp/experimental/fs
// https://msdn.microsoft.com/en-us/library/hh874694.aspx
// http://www.boost.org/doc/libs/1_62_0/libs/filesystem/doc/index.htm

namespace fs = std::experimental::filesystem ; // microsoft
// namespace fs = boost::filesystem ; // boost

bool dir_exists( std::string path_to_dir )
{ return fs::exists(path_to_dir) && fs::is_directory(path_to_dir) ; }

void process_run( std::string job_dir, int run )
{
    const std::string run_dir = job_dir + "/run" + std::to_string(run) ;

    if( dir_exists(run_dir) )
    {
        const std::string path_to_file =  run_dir + "/myFile.dat" ;

        // TO DO: process_file(path_to_file) ;
    }
}

void process_job( int job, int num_runs )
{
    // assumes that the job directories are under the current working directory
    const std::string job_dir = "job" + std::to_string( job ) ;

    if( dir_exists(job_dir) )
    {
        for( int run = 0 ; run < num_runs ; ++run ) process_run( job_dir, run ) ;
    }
}

int main()
{
    const int num_jobs = 12 ; // number of simulations
    const int num_runs = 17 ; // number of parameters

    for( int job = 0 ; job < num_jobs ; ++job ) process_job( job, num_runs ) ;
}
Dec 14, 2016 at 2:13am
That's probably closer than what I have.
Dec 14, 2016 at 2:27am
Thanks a lot...
Topic archived. No new replies allowed.