Hi, I want to process multiple text files within a directory and then after processing i want to store the processed files within another directory.
Below is the code which i have written. It is accessing multiple text files within a directory, applying some processing on each text file (i.e., reading 2nd value of first 10 lines). But i am not able to separately store the processed result in another directory.

static void Main(string[] args)
        {
            DirectoryInfo di = new DirectoryInfo(@"F:\abc\");

        FileInfo[] files = di.GetFiles("*.txt");


        foreach (FileInfo fri in files)
        {

            using (StreamReader streamReader = fri.OpenText())
            {
                string line;
               for (int count = 0; count < 10; count++)
                {
                    line = streamReader.ReadLine();
                    string[] items = line.Split(' ');
                    float myInteger = float.Parse(items[1]);
//Problem is here now, which path should i provide in stream writer?                
 StreamWriter file = new StreamWriter(@"F:\test.txt", true);
                    file.WriteLine(myInteger);
                    file.Close();
                }
            }
        }

        }

Recommended Answers

All 2 Replies

You have to use StringBuilder class to write into it 1st, then when you are done with reading files, you do the Write thing.
So your code shoud look (in sepearate methods):

class Program
    {
        static void Main(string[] args)
        {
            string readFrom = @"F:\abc\";
            string writeTo = @"F:\test.txt";
            StringBuilder sb = Read(readFrom);
            Write(writeTo, sb);
        }

        private static StringBuilder Read(string filePath)
        {
            StringBuilder sb = new StringBuilder();
            DirectoryInfo di = new DirectoryInfo(filePath);
            FileInfo[] files = di.GetFiles("*.txt");
            foreach (FileInfo fri in files)
            {
                using (StreamReader streamReader = fri.OpenText())
                {
                    string line;
                    for (int count = 0; count < 10; count++)
                    {
                        line = streamReader.ReadLine();
                        string[] items = line.Split(' ');
                        float myInteger = float.Parse(items[1]);
                        sb.AppendLine(myInteger.ToString()); //this will write line byline (you can use Append() method only to write in a single line
                        //but you will neeed separator then (I think)
                    }
                }
            }
            return sb;
        }

        private static void Write(string filePath, StringBuilder sb)
        {
            using (StreamWriter file = new StreamWriter(filePath, true))
            {
                file.WriteLine(sb.ToString());
                file.Close();
            }
        }
    }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] files = Directory.GetFiles(@"D:\abc\", "*.svm", SearchOption.AllDirectories);

            foreach (string fri in files)
            {
                StringBuilder result = new StringBuilder();

                using (StreamReader streamReader = new StreamReader(fri))
                {
                    string line;
                    for (int i = 0; i < 1; i++)
                    {
                        streamReader.ReadLine();
                    }

                    for (int count = 0; count < 157; count++)
                    {
                        line = streamReader.ReadLine();
                        string[] items = line.Split(' ');
                        float myInteger = float.Parse(items[1]);
                        result.AppendLine(myInteger.ToString());
                    }

                    streamReader.Close();

                    StreamWriter streamWriter = new StreamWriter(fri);

                    streamWriter.Write(result);
                    streamWriter.Close();

                }
            }
        }

    }
}
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.