vysakh
vysakh

Reputation: 266

Reading contents from several files and writing to one file

In my application there is a situation like this.Before creating a file, my application search for files in a directory under a particular filename. If any file/files found, then it should read each files contents and write these contents(of each file) to a new file. I have googled many and tried some like this:

string temp_file_format = "ScriptLog_" + DateTime.Now.ToString("dd_MM_yyyy_HH");

string[] files = Directory.GetFiles(path,temp_file_format);
foreach (FileAccess finfo in files)
{
 string text = File.ReadAllText(finfo);
}

and

System.IO.DirectoryInfo dir = new DirectoryInfo(path);
System.IO.FileInfo[] files = dir.GetFiles(temp_file_format);
foreach (FileInfo finfo in files)
{
  finfo.OpenRead();
}

But all these failed..Can anyone show me an alternative for this?

Is there anything wrong in my temp_file_format string?

It will be nice if I could prepend these contents to the new file. Else also, no worries..

any help would be really appreciated..

Upvotes: 1

Views: 1964

Answers (4)

Rune FS
Rune FS

Reputation: 21742

Here's a short snippet that reads all the files and out puts them to the path outputPath

var lines = from file in  Directory.GetFiles(path,temp_file_format)
            from line in File.ReadAllLines(file)
            select line;
File.WriteAllLines(outputPath, content);

The problem you are having with your code is not really related to reading files but simply trying to use an object as a type it's not. Directory.GetFiles returns an array of string and File.ReadXXX and File.OpenRead expects the path as a string. So you simply need to pass each of the strings returned as the path argument to the appropriate method. The above is one such example. Hope it helps both solve your problem and explain the actually issue with your code

Upvotes: 1

sehe
sehe

Reputation: 393064

This is a compete working implementation that does all of that

  • without reading everything in memory at one time (which doesn't work for large files)
  • without keeping any files open for more than the required time
using System.IO;
using System.Linq;

public static class Program { 
    public static void Main()
    {
        var all = Directory.GetFiles("/tmp", "*.cpp")
             .SelectMany(File.ReadAllLines);

        using (var w = new StreamWriter("/tmp/output.txt"))
            foreach(var line in all)
                w.WriteLine(line);
    }
}

I tested it on mono 2.10, and it should work on any .NET 4.0+ (for File.ReadAllLines which is a lazy linewise enumerable)

Upvotes: 2

Damith
Damith

Reputation: 63065

using (var output = File.Create(outputPath))
{
    foreach (var file in Directory.GetFiles(InputPath,temp_file_format))
    {
        using (var input = File.OpenRead(file))
        {
            input.CopyTo(output);
        }
    }
}

Upvotes: 0

Ivan Sander de Jong
Ivan Sander de Jong

Reputation: 835

try this:

foreach (FileInfo finfo in files)
{
try
    {

        using (StreamReader sr = new StreamReader("finfo "))
        {
            String line = sr.ReadToEnd();
            Console.WriteLine(line);
        }
    }
    catch (Exception e)
    {
        Console.WriteLine("The file could not be read:");
        Console.WriteLine(e.Message);
    }
 }

Upvotes: 0

Related Questions