Hi, at the moment i have a program that examines a file, does some processing and then outputs the results in a file.

Is it posible to examinemultiple files stored in a folder all at once, then output the report files in a seperate folder without having to run my program each time for each file?


thanks

Recommended Answers

All 13 Replies

Member Avatar for iamthwee

Yes...

Just open each one in your program.

i did think that i could possibly go down this route but there are 0ver 2000 files which would be a nightmare to open each one in my code.

Is there not like an open folder and read all files function :) .......i say hopefully

You could use findfirst() and findnext() to get the names of all the files in a derectory, then just use a loop to open and process them.

[edit] Here's some samplecode

hi guys, thanks again for your great help. The example that you provided below is exactly what i am after. Instead of printing i would like to access each file individually so i can preform the same task on each file in my code. To do this, i am thinking that instead of printing the directory name i could save the file path to a variable and pass this to my program each time a new file is found so that i can preform my task on it?


im sorry to ask for a tad more advice but how can i change "printf("%s\n", dir->d_name);"
into saving the file name in a variable path so i can pass the file name to my other function?

/*
 * This program displays the names of all files in the current directory.
 */

#include <dirent.h> 
#include <stdio.h> 

int main(void)
{
  DIR           *d;
  struct dirent *dir;
  d = opendir(".");
  if (d)
  {
    while ((dir = readdir(d)) != NULL)
    {
      printf("%s\n", dir->d_name);
    }

    closedir(d);
  }

  return(0);
}

thanks everyone

You could store the name that was found in a char array and pass the array to a function

what i have done is store each path in a string called file name. I am trying to pass this variable to an ifstream now but im not sure if it will work as im getting a lot of errors.

cout << "Enter the name and path of the FOLDER: ";
    string folder;
    getline( cin, folder );
string folder1 = "\\";

  DIR           *d;
  struct dirent *dir;
  d = opendir(folder.c_str());


  if (d)
  {
    while ((dir = readdir(d)) != NULL)
    {


     string g = ("%s\n", dir->d_name);
 
cout << g << endl;

string filename = folder + folder1 + g;

cout << filename<<endl;


    }

    closedir(d);
  }




ifstream infile (filename.c_str(), ios::binary);    // errors here

if i pass infile my string 'filename' then pass my function the stream will it pass every instance of a new file name? would i need to put my ifstream delaration in the loop.


on a more simple scenario, say i had a folder with 10 txt files in it. Am i close to being able to read the first line of each file and write the first line of each file into an overal txt file? its mainly the handling of an infile for each file in the directory im stuck with

thanks


thanks

Member Avatar for iamthwee

Yeah you just need to open the files within the loop, see bit in red

/*
 * This program displays the names of all files in the current directory.
 */

#include <dirent.h> 
#include <stdio.h> 
#include <string>
#include <iostream>

using namespace std;

int main(void)
{
  DIR           *d;
  struct dirent *dir;
  
  //enter directory path here
  d = opendir("c:/Program Files/");
  if (d)
  {
    while ((dir = readdir(d)) != NULL)
    {
      string g =  dir->d_name;
      cout << g << endl;
      
      //open the files here
    }

    closedir(d);
  }

  getchar();
  return(0);
}

as string g contains the directory name, how can i use that string in my file stream to specify a path. I thought i could do the following but its returning lots of errors

ifstream infile (g.c_str(), ios::binary);    // g being the path string
Member Avatar for iamthwee
#include <dirent.h>
#include <stdio.h>
#include <string>
#include <iostream>
#include <fstream>

using namespace std;

int main ( void )
{
  DIR           *d;
  struct dirent *dir;

  //enter directory path here
  string g_1 = "c:/Program Files/";
  d = opendir ( g_1.c_str() );
  if ( d )
  {
    while ( ( dir = readdir ( d ) ) != NULL )
    {
      string g =  dir->d_name;
      cout << g << endl;

      string tmp = g_1 + g; //concat the paths
      ifstream read ( tmp.c_str() );

      string line;
      while ( getline ( read, line, '\n' ) )
      {
        //read each line
        cout << line << endl;
      }
      read.close();

      //open the files here
    }
    closedir ( d );
  }

  getchar();
  return ( 0 );
}

thank you for your help. Im getting there!! just one last question. is it possible to pass each value of temp (the path) to and ifstream outside of the loop? i know it mite sound random but its the way my prog is setup

Member Avatar for iamthwee

It is possible yes, but it doesn't make sense using the example you have described... You might as well do it all within the loop.

i was using what i thought would be a gud example but possibly not. My program requests user input for the folder which contains the files for examination then i have two further functions to process the files and output into a new file. The processing function picks up the input file currently from main when my user inputs it. I would need to get my infile path out of the loop each time it reads a new file path and into main so that i can pass it to my processing function to read for every file. I really hope that made sense

i could post my code if it would help but i think a lot of it does not apply to this question. In my eyes, and im not a good programmer but the solution to me seems to be passing the path string to an ifstream in main each time so that i can use it.

is this possible from you code?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.