I have multiple text files .each file is named as s100041.asd.txt ,s100044.asd.txt ....... . Now each file contain two fields such as
Wavelength S100141.asd
325 0.015082292401682
326 0.011690540571175
327 1.06575414031349E-02
328 1.30298010496599E-02
329 0.014743817273513
325 0.015082292401682
326 0.011690540571175
327 1.06575414031349E-02
328 1.30298010496599E-02
329 0.014743817273513
another file
Wavelength S100144.asd
325 1.35059549289737E-02
326 0.010981114226419
327 1.22348778870862E-02
328 1.63555124470031E-02
329 1.57389782659161E-02
325 1.35059549289737E-02
326 0.010981114226419
327 1.22348778870862E-02
328 1.63555124470031E-02
329 1.57389782659161E-02
likewise i have lot of files in which wavelength is common and only .asd field differ.
I want to put one opendialog control through which I can select multiple files. as I select files all those files get combined and result in one txt file and output appears like this
| S100144.asd | S100143.asd | S100142.asd | S100141.asd | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 0.013506 | 0.016867 | 0.011569 | 0.015082 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 0.010981 | 0.015599 | 0.009434 | 0.011691 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 0.012235 | 0.015259 | 0.010794 | 0.010658 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 0.016356 | 0.016275 | 0.014576 | 0.013030 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 0.015739 | 0.016134 | 0.013878 | 0.014744 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 0.014514 | 0.016259 | 0.012032 | 0.015209 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 0.015142 | 0.017304 | 0.011341 | 0.014354 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 0.015259 | 0.017318 | 0.012004 | 0.014129 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 0.014385 | 0.016048 | 0.012976 | 0.013645 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 0.012086 | 0.013314 | 0.012666 | 0.011062 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 0.012186 | 0.013723 | 0.012504 | 0.011287 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 0.014508 | 0.016509 | 0.012791 | 0.013942 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 0.015285 | 0.015257 | 0.013379 | 0.013823 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 0.014972 | 0.013261 | 0.013592 | 0.012550 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 0.014264 | 0.012519 | 0.013243 | 0.011364 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 0.016256 | 0.013304 | 0.013790 | 0.012205 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 0.018061 | 0.014753 | 0.014364 | 0.013313 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 0.015868 | 0.015758 | 0.013750 | 0.012321 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 0.014737 | 0.015407 | 0.013885 | 0.012425 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 0.014850 | 0.014171 | 0.014401 | 0.013287 |
i'm new to c# please i need code for it .......
RobbinsonPosted Dec 26, 2013, 6:49 AM
The best way is to do it in chunk. Below is sample code snippet to perform merge operations for the files in C#.
const int chunkSize = 2 * 1024; // 2KB
var inputFiles = new[] { "file1.txt", "file2.txt", "file3.txt" };
using (var output = File.Create("output.txt"))
{
foreach (var file in inputFiles)
{
using (var input = File.OpenRead(file))
{
var buffer = new byte[chunkSize];
int bytesRead;
while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, bytesRead);
}
}
}
}
Hope, this will help you out. Feel free to connect in case of any query.
Please don't forget to mark as answer if this post helps you.
Meenakshi Kumari SyalPosted Jan 13, 2014, 4:55 AM
Meenakshi Kumari SyalPosted Jan 6, 2014, 1:23 AM
The best overloaded method match for 'System.IO.File.OpenRead(string)' has some invalid argumentsC:\Hyperspectral_Tool\Hyperspectral_Tool\merge.cs7337Hyperspectral_Tool
Error2Argument '1': cannot convert from 'object' to 'string'C:\Hyperspectral_Tool\Hyperspectral_Tool\merge.cs7351Hyperspectral_Tool
RobbinsonPosted Jan 6, 2014, 12:00 AM
This error is obevious as you are trying to iterate over listbox. just change it to [listbox].items.
public void saveFileDialog1_FileOk( object sender, System.ComponentModel.CancelEventArgs e)
{
const int chunkSize = 16 * 1024*1024;
string temp = null;
using (var output = File.Create(saveFileDialog1.FileName)){
foreach (var file in lstfile.Items)
{
using (var input = File.OpenRead(file))
{
var buffer = new byte[chunkSize];
int bytesRead;
while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, bytesRead);
}
}
}
}
string prompt = null;
prompt = string.Concat(lbl2.Text, " files merged succesfully");
Interaction.MsgBox(prompt, MsgBoxStyle.Information, "Merge");
lstfile.Items.Clear(); lbl1.Text = "0";
}
Hope, this will help you out. Please feel free to connect in case of any query.
Please don't forget to mark as answer if this post helps you.
Meenakshi Kumari SyalPosted Jan 5, 2014, 11:35 PM
"foreach statement cannot operate on variables of type 'System.Windows.Forms.ListBox' because 'System.Windows.Forms.ListBox' does not contain a public definition for 'GetEnumerator"
public void saveFileDialog1_FileOk( object sender, System.ComponentModel.CancelEventArgs e)
{
const int chunkSize = 16 * 1024*1024;
string temp = null;
using (var output = File.Create(saveFileDialog1.FileName)){
foreach (var file in lstfile)
{
using (var input = File.OpenRead(file))
{
var buffer = new byte[chunkSize];
int bytesRead;
while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, bytesRead);
}
}
}
}
string prompt = null;
prompt = string.Concat(lbl2.Text, " files merged succesfully");
Interaction.MsgBox(prompt, MsgBoxStyle.Information, "Merge");
lstfile.Items.Clear(); lbl1.Text = "0";
}
RobbinsonPosted Jan 3, 2014, 7:43 AM
I have updated your code make it working. Actually you have to use different StreamReader everytime or need to close or dispose after every iteration of your loop.
I have updated your code in better manner. Please consider it to update for any compilation error as i have not tested it on Visual studio.
public void saveFileDialog1_FileOk( object sender, System.ComponentModel.CancelEventArgs e)
{
string temp = null;
using (var output = File.Create(saveFileDialog1.FileName)){
foreach (var file in lstfile)
{
using (var input = File.OpenRead(file))
{
var buffer = new byte[chunkSize];
int bytesRead;
while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, bytesRead);
}
}
}
}
string prompt = null;
prompt = string.Concat(lbl2.Text, " files merged succesfully");
Interaction.MsgBox(prompt, MsgBoxStyle.Information, "Merge");
lstfile.Items.Clear(); lbl1.Text = "0";
}
Hope, this will help you out. Feel free to connect in case of any query.
Please don't forget to mark as answer if this post helps you.
Meenakshi Kumari SyalPosted Jan 3, 2014, 4:24 AM
i'm using one button control on its click event opendialog will open where i set multiselect property =true,
all those text files path get add to list box.and then used again one button to merge those text files to one text file and save them using savedialog box.but when i want to save i got one error
public void saveFileDialog1_FileOk( object sender, System.ComponentModel.CancelEventArgs e)
{
StreamReader Reader = default(StreamReader);
int i = 0;
string temp = null;
for (i = 0; i <= lstfile.Items.Count - 1; i++)
{
Reader = File.ReadAllText(temp);
Reader = File.OpenText(lstfile.Items.Add(i));
temp = Reader.ReadToEnd();
File.AppendAllText(saveFileDialog1.FileName, temp);
}
string prompt = null;
prompt = string.Concat(lbl2.Text, " files merged succesfully");
Interaction.MsgBox(prompt, MsgBoxStyle.Information, "Merge");
lstfile.Items.Clear(); lbl1.Text = "0";
}
the problem is there in these coloured line ...pls help me to solve my problem.
RobbinsonPosted Dec 27, 2013, 4:25 AM
Can you please paste your code here which makes very easy to understand the problem ?
The simplest solution i am having in my mind is to convert above code to function which takes file names as parameter.
public void MergeFiles(string[] inputFiles,string mergeFileName)
{
const int chunkSize = 2 * 1024; // 2KB
using (var output = File.Create(mergeFileName))
{
foreach (var file in inputFiles)
{
using (var input = File.OpenRead(file))
{
var buffer = new byte[chunkSize];
int bytesRead;
while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, bytesRead);
}
}
}
}
}
Call above this method where you select the list of files.
var inputFiles = new[] { "file1.txt", "file2.txt", "file3.txt" };
MergeFiles(inputFiles,"OutPut.txt");
Hope, this will help you out. Feel free to connect in case of any query.
Please don't forget to mark as answer if this post helps you.
Meenakshi Kumari SyalPosted Dec 27, 2013, 4:15 AM
Meenakshi Kumari SyalPosted Dec 27, 2013, 1:38 AM
Meenakshi Kumari SyalPosted Dec 27, 2013, 1:35 AM
Dhirendra MisraPosted Dec 26, 2013, 10:21 PM
You can try this code as well. There is class containing 2 methods for reading and writing the output.
using System.Collections.Specialized;
using System.IO;
.................
.................
class ReadMergeTextFiles
{
string path = @"C:\Dhirendra\Learning\Corner\SampleFiles\";
public void MergeFiles(string targetFile)
{
StringCollection mergedValuesCollection = new StringCollection();
//Get files from openFileDialog
string[] arrFiles = { "S100141.asd.txt", "S100144.asd.txt" }; //Lets take file names
foreach (string fname in arrFiles)
{
mergedValuesCollection = ReadFile(fname, mergedValuesCollection);
}
//Write to file
using (StreamWriter file = new StreamWriter(path + targetFile))
{
foreach (string line in mergedValuesCollection)
{
file.WriteLine(line);
}
}
}
public StringCollection ReadFile(string fileName, StringCollection mergedValues)
{
int i = 0;
string filePath = path + fileName;
try
{
using (StreamReader sr = new StreamReader(filePath))
{
String line = string.Empty;
while ((line = sr.ReadLine()) != null) //Assuming there is white space seperator for each line.
{
line = line.Substring(line.IndexOf(' '), line.Length - line.IndexOf(' ')).Trim();
if (i != 0)
{
double val = Double.Parse(line);
line = string.Format("{0, 8:F6}", val);
}
else
line = string.Format("{0, 11}", line);
if (mergedValues.Count > i && mergedValues[i] != null)
mergedValues[i] += "\t" + line;
else
mergedValues.Add(line);
i++;
}
}
}
catch (Exception e)
{
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
return mergedValues;
}
}
//Calling from main method.
static void Main(string[] args)
{
ReadMergeTextFiles objReadMergeTextFiles = new ReadMergeTextFiles();
objReadMergeTextFiles.MergeFiles("mergedOutput.txt");
}
The output will be stored in the passed file name in the location which you specify in "path" global variable.
mergedOutput.txt
S100141.asd S100144.asd
0.015082 0.013506
0.011691 0.010981
0.010658 0.012235
0.013030 0.016356
0.014744 0.015739
Thanks
VulpesPosted Dec 26, 2013, 2:55 PM
Balakrishnan PPosted Dec 26, 2013, 6:31 AM
by Creating a New File and write those using StreamWriter.