3 Replies - 2457 Views - Last Post: 18 February 2009 - 03:57 PM Rate Topic: -----

#1 ahmedo047   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-February 09

how can I apply all txt files in the folder this code?

Posted 18 February 2009 - 10:19 AM

how can I apply to all txt files in the folder. (save to a different folder the same name all the txt files).
this code is only working for a txt file but I want to apply all txt files in a folder its.
sample D:\\A I want to call the A folder.
in A:
1.txt
5
4
9
2.txt
8
7
12
...
After I put in process the A folder I want to save a different folder( sample D:\\B) the results.
in B:
1.txt
18
2.txt
27
...
I have following code:
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;

class MainClass
{
	public static void Main(string[] args)
	{
		string inFile, filePath;
		string line;


		DirectoryInfo di = new DirectoryInfo(@"D:\a\");
		FileInfo[] files = di.GetFiles("*.txt");
		Console.WriteLine("Input file name to open!");
		Console.WriteLine("Sample: d:\\filename.txt");
		inFile = Convert.ToString(Console.ReadLine());
		StreamReader sr = File.OpenText(inFile);
		line = sr.ReadLine();
		foreach (FileInfo fi in files)
		{
			inFile = fi.FullName;
		   
			Console.WriteLine();
			Console.WriteLine("Successful file open!...");
			Console.WriteLine();
			Console.WriteLine("Input file name to save!");
			Console.WriteLine("sample: d:\\filename.txt");
			filePath = Console.ReadLine();
			FileStream file = new FileStream(filePath, FileMode.Create);
			StreamWriter sw = new StreamWriter(file);
			List<string> lines = new List<string>();

			while ((line = sr.ReadLine()) != null)
			{
				lines.Add(line);
			}
			try
			{
				int numberOne = Int32.Parse(lines[0]);
				int numberTwo = Int32.Parse(lines[1]);
				int numberThree = Int32.Parse(lines[2]);
				int x = numberOne + numberTwo + numberThree;
				sw.WriteLine(x);
			}
			catch { }
			sw.Close();
			sr.Close();
		}

	}
}
		


Is This A Good Question/Topic? 0
  • +

Replies To: how can I apply all txt files in the folder this code?

#2 djkitt   User is offline

  • D.I.C Head

Reputation: 29
  • View blog
  • Posts: 192
  • Joined: 22-May 08

Re: how can I apply all txt files in the folder this code?

Posted 18 February 2009 - 12:05 PM

So, you have a number of issues here.

1) You no longer need to ask for an input file name. You have correctly loaded the 'files' array with all the text files in the D:\a\ directory. For now just comment out the lines that deal with this:
		//Console.WriteLine("Input file name to open!");
		//Console.WriteLine("Sample: F:\\filename.txt");
		//inFile = Convert.ToString(Console.ReadLine());



2) You set inFile correctly to fi.FileName inside your foreach loop, but you have already created a StreamReader instance (sr) outside the loop using the manually assigned filename mentioned above. Move the code to create 'sr' inside the loop and after you are assigning fi.FullName to inFile:

		//StreamReader sr = File.OpenText(inFile); //Move these lines
		// line = sr.ReadLine();
		foreach (FileInfo fi in files)
		{
			inFile = fi.FullName;
			StreamReader sr = File.OpenText(inFile);
			line = sr.ReadLine();



3) You are reading a line from sr and not assigning it to one of your variables. Unless you have a blank line or some sort of comment or title at the top of each of your text files you should remove this line.
		foreach (FileInfo fi in files)
		{
			inFile = fi.FullName;
			StreamReader sr = File.OpenText(inFile);
			// line = sr.ReadLine(); 




Now your program should run and ask you for a 'filename to save!' for each 'fi' in 'files'. (I would recommend asking for a save-path before you enter your foreach loop instead)


Hope this helps,

Kitt
Was This Post Helpful? 1

#3 ahmedo047   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-February 09

Re: how can I apply all txt files in the folder this code?

Posted 18 February 2009 - 12:30 PM

wonderful...
A thousand times thank you!
Was This Post Helpful? 0
  • +
  • -

#4 ahmedo047   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-February 09

Re: how can I apply all txt files in the folder this code?

Posted 18 February 2009 - 03:57 PM

using System;
using System.IO;
using System.Text;
using System.Collections.Generic;

class MainClass
{
	public static void Main(string[] args)
	{
		string inFile;
		string line;

		const string inDir = "D:\\data\\A\\";
		const string outDir = "D:\\data\\B\\";
		DirectoryInfo di = new DirectoryInfo(inDir);
		FileInfo[] files = di.GetFiles("*.txt");
		foreach (FileInfo fi in files)
		{
			inFile = fi.FullName;
			StreamReader sr = File.OpenText(inFile);
			List<string> lines = new List<string>();

			while ((line = sr.ReadLine()) != null)
			{
				lines.Add(line);
			}
			
				int numberOne = Int32.Parse(lines[0]);
				//int numberTwo = Int32.Parse(lines[1]);
				int numberThree = Int32.Parse(lines[2]);
				//int x = numberOne + numberTwo + numberThree;
				int x = numberOne + numberThree;

			sr.Close();
			string destFile = Path.Combine(outDir, fi.Name);
			File.WriteAllText(destFile, Convert.ToString(x));
		}

	}
}
 

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1