2
Answers

Read Mobile Number in Text , Doc & Excel file .

Mari Karthi

Mari Karthi

9y
414
1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ReadExcelfilee
{
class Program
{
string dir = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
static void Main(string[] args)
{
string dir = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
string root = Directory.GetCurrentDirectory();
System.IO.Directory.CreateDirectory(root + "\\ReadFiles");
System.IO.Directory.CreateDirectory(root + "\\MobileNumbers");
string fileResult = dir + @"\MobileNumbers\\MobileNumerList_Text.xls";
string fileResult1 = dir + @"\MobileNumbers\\MobileNumerList_Doc.xls";
FileStream fs = new FileStream(fileResult, FileMode.OpenOrCreate, FileAccess.Write);
FileStream fs1 = new FileStream(fileResult1, FileMode.OpenOrCreate, FileAccess.Write);
fs.Close();
fs1.Close();
try
{
if (Directory.Exists(dir))
{
Program obj = new Program();
string[] files = Directory.GetFiles(dir);
DirectoryInfo dinfo = new DirectoryInfo(dir);
FileInfo[] txtfile = dinfo.GetFiles("*.txt");
obj.WriteFiles(txtfile, fileResult);
FileInfo[] Docfile = dinfo.GetFiles("*.doc");
obj.WriteFiles(Docfile, fileResult1);
FileInfo[] Excelfile = dinfo.GetFiles("*.xls");
obj.WriteFiles(Excelfile, fileResult1);
//FileInfo[] totfile = txtfile.Concat(Docfile).ToList();
//List<FileInfo> file = new List<FileInfo>();
//files.AddRange(d1.GetFiles());
//files.AddRange(d2.GetFiles());
}
}
catch (Exception ex)
{
ex.ToString();
}
finally
{
// sw.Close();
}
}
public void WriteFiles(FileInfo[] txtfile, string fileResult)
{
string root = Directory.GetCurrentDirectory();
string[] files = Directory.GetFiles(dir);
// fs.Close();
StreamWriter sw = new StreamWriter(fileResult);
FileInfo[] ReadFiles = txtfile;
try
{
int num = -1;
for (int f = 0; f < ReadFiles.Length; f++)
{
string file = Convert.ToString(ReadFiles[f]);
using (StreamReader reader = new StreamReader(file))
{
var lineCount = File.ReadLines(file).Count();
string[] ListLines = new string[lineCount];
for (int j = 1; j < lineCount; j++)
{
ListLines[j] = reader.ReadLine();
string line = ListLines[j];
string b = string.Empty;
int ct = 0, ws = 0;
for (int i = 0; i < line.Length; i++)
{
// character count
if (Char.IsDigit(line[i]))
{
if ((((line[i]) == '0') || ((line[i]) == '1') || ((line[i]) == '2') || ((line[i]) == '3') || ((line[i]) == '4') || ((line[i]) == '5') || ((line[i]) == '6')) && (b == ""))
{
b = "";
}
else
b += line[i];
}
else if (char.IsWhiteSpace(line[i]))
{
ws++;
}
else
{
ct++;
b = "";
}
if (ws >= 2)
{
b = "";
ws = 0;
}
if (b.Length == 10)
{
num++;
if (num == 1)
{
}
if (num == 10)
{
sw.Write("\n");
num = 0;
sw.Write(b);
sw.Write("\t");
b = "";
}
else
{
sw.Write(b);
sw.Write("\t");
b = "";
}
}
}
}
// System.IO.File.Move(root + file, root + "\\OutPutFiles" + file);
}
// var pt1 = file;
var pt2 = root + "\\ReadFiles\\" + ReadFiles[f];
System.IO.File.Move(file, pt2);
}
}
catch (Exception ex)
{
ex.ToString();
}
finally
{
sw.Close();
}
}
}
}


Answers (2)