本文介绍了如何比较两个文件并在另一个文件中写入增量数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试这样做,将File1与File2进行比较,并将增量数据写入另一个文件。但重点是在这里我想在比较时排除标题行。我试图用C#
下面是我试过的代码,我在SSIS脚本任务中使用这段代码。
立即注意将被批评.. !!
预付款谢谢
Pradeep Kumar T
我尝试了什么:
I am trying to do something Like this, Comparing File1 with File2 and write Incremental data into another file. But the point is here I want to exclude Header row while Comparing. I trying to do this in C#
Below is the code I have tried, I am using this code in SSIS Script task.
Immediate attention will be aprreciated..!!
Thanks in Advance
Pradeep Kumar T
What I have tried:
foreach (string CFile in Directory.EnumerateFiles(CurrentWkFile))
{
WriteLogFile("Step 2 : Checking " + CFile + " from path " + CurrentWkFile);
//var pFile = Directory.GetFiles(PreviousWkFile, CFile, SearchOption.AllDirectories).FirstOrDefault();
String[] Prevwk = File.ReadAllLines(Path.Combine(PreviousWkFile, Path.GetFileName(CFile)));
Prevwk.Skip(0);
String[] CurWk = File.ReadAllLines(Path.Combine(CurrentWkFile, Path.GetFileName(CFile)));
CurWk.Skip(0);
if (File.Exists(PreviousWkFile + "\\" + Path.GetFileName(CFile)))
{
WriteLogFile("Step 3 : Comparing file" + Path.GetFileName(CFile) + "with previous week file");
IEnumerable<string> Exprevwk = Prevwk.Skip(0);
IEnumerable<string> ExCurrwk = CurWk.Skip(0);
IEnumerable<string> CurrWkOnly = (ExCurrwk.Skip(0).Except(Exprevwk.Skip(0))).Skip(0);
WriteLogFile("Step 4 : File Compared ready to write incremental record in the file " + CFile);
File.WriteAllLines(Path.Combine(ToProcessFile, Path.GetFileName(CFile)), CurrWkOnly.Skip(0));
WriteLogFile("step 5 :Incremental data written in the file " + CFile + "placed in the folder path" + ToProcessFile);
}
else
{
IEnumerable<string> CurrWeek = CurWk;
WriteLogFile("Crazy step :In Prevoius week " + Path.GetFileName(CFile) + "not exists in the path" + PreviousWkFile);
File.WriteAllLines(Path.Combine(ToProcessFile, Path.GetFileName(CFile)), CurrWeek);
}
}
推荐答案
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
namespace ComparingOfFiles
{
class Program
{
static void Main(string[] args)
{
Write("Welcome to file compare program utility, it will generate a file and add some lines, then it will copy it and add more. Finally compare the two and write the added content to a new file.\r\n");
try
{
var parts = Assembly.GetExecutingAssembly().Location.Split(@"\".ToCharArray());
parts[parts.GetUpperBound(0)] = "";
var basePath = string.Join(@"\", parts);
string fileOne = Path.Combine(basePath, "fileone.txt");
string fileTwo = Path.Combine(basePath, "filetwo.txt");
string fileResult = Path.Combine(basePath, "fileresult.txt");
int columns = 8;
string valuerange = "abcdefghijklmnopqrstuvwxyz";
string seperator = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ListSeparator;
var rnd = new Random((int) DateTime.Now.Ticks);
Write("Deling files if existing");
foreach(var file in new []{fileOne, fileTwo, fileResult})
if(File.Exists(file))
File.Delete(file);
Write("Making log file one");
using (var fs = new FileStream(fileOne, FileMode.Create))
{
var writer = new StreamWriter(fs, Encoding.UTF8);
for (int x = 0; x < 10; x++)
AddColumnsOfRandomWords(columns, rnd, valuerange, seperator, writer);
writer.Flush();
writer.Close();
}
Write("Copying to logfile two and adding some lines");
File.Copy(fileOne, fileTwo);
using (var fs = new FileStream(fileTwo, FileMode.Append))
{
var writer = new StreamWriter(fs, Encoding.UTF8);
for (int x = 0; x < 5; x++)
AddColumnsOfRandomWords(columns, rnd, valuerange, seperator, writer);
writer.Flush();
writer.Close();
}
Write("Comparing the two files");
var sbDifference = new StringBuilder();
var fileOneContentRows = new List<string>();
var fileTwoContentRows = new List<string>();
using (var fs = new FileStream(fileOne, FileMode.Open, FileAccess.Read))
{
var reader = new StreamReader(fs, Encoding.UTF8);
while(!reader.EndOfStream)
fileOneContentRows.Add(reader.ReadLine());
reader.Close();
}
using (var fs = new FileStream(fileTwo, FileMode.Open, FileAccess.Read))
{
var reader = new StreamReader(fs, Encoding.UTF8);
while (!reader.EndOfStream)
fileTwoContentRows.Add(reader.ReadLine());
reader.Close();
}
using (var fs = new FileStream(fileResult, FileMode.CreateNew))
{
int idx = 1;
while (idx < fileOneContentRows.Count())
{
if (fileOneContentRows[idx].Equals(fileTwoContentRows[idx]))
{
idx++;
continue;
}
throw new DataException("OMFG! the logs from last weeks have been changed this week!");
}
if (fileOneContentRows.Count() >= fileTwoContentRows.Count())
{
sbDifference.AppendLine("Log error, the content of the logfile appears to have decreased between one: " + fileOne + " and two: " + fileTwo);
} else
{
for (int z = idx; z < fileTwoContentRows.Count();z++ )
{
sbDifference.AppendLine(fileTwoContentRows[z]);
}
}
var writer = new StreamWriter(fs, Encoding.UTF8);
writer.Write(sbDifference.ToString());
writer.Flush();
writer.Close();
}
Write("Done");
} catch (Exception ex)
{
Write("Exception occured:\r\n" + ex.ToString());
}
Write("\r\nPress ENTER to exit");
Console.ReadLine();
}
private static void AddColumnsOfRandomWords(int columnsToAdd, Random rnd, string valuerange, string seperator, StreamWriter writer)
{
for (int i = 0; i < columnsToAdd; i++)
{
int length = rnd.Next(1, 20);
var word = "";
for (int y = 0; y < length; y++)
word += valuerange.Substring(rnd.Next(valuerange.Length - 1), 1);
if (i < (columnsToAdd - 1))
word += seperator;
writer.Write(word);
}
writer.Write("\r\n");
}
private static void Write(string message)
{
Console.WriteLine(message);
}
}
}
这篇关于如何比较两个文件并在另一个文件中写入增量数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!