i have 4 text files in 1 folder , pattern.txt
compare these text files..in pattern.txt
have
insert update delete drop
i need compare text file 4 text files , if these patterns matches line in text files have write lines in log file...i had read files using linq..i need compare files , write in text file line number..here code
var foldercontent = directory.getfiles(patha) .select(filename => file.readalltext(filename)) .aggregate(new stringbuilder(), (sb, s) => sb.append(s).append(environment.newline), sb => sb.tostring()); var pattern = file.readalllines(pathb).aggregate(new stringbuilder(), (sb, s) => sb.append(s).append(environment.newline), sb => sb.tostring()); using (var dest = file.appendtext(path.combine(_logfolderpath, "log.txt"))) { //dest.writeline("lineno : " + counter.tostring() + " : " + "" + line); }
edit have used c# compare 2 text files need in linq
while ((line = file.readline()) != null) { if (line.indexof(line2, stringcomparison.currentcultureignorecase) != -1) { dest.writeline("lineno : " + counter.tostring() + " : " + " " + line.trimstart()); } counter++; } file.basestream.seek(0, seekorigin.begin); counter = 1;
there might simpler solution, @ least working if want use linq:
var foldercontent = directory.getfiles(patha) .select(filename => new { filename = filename, lines = file.readalllines(filename) }) .selectmany(file => file.lines.select((line, idx) => new { linenumber = idx + 1, text = line, filename = file.filename })); var pattern = file.readalllines(pathb); var result = fileline in foldercontent pattern.any(p => fileline.text.indexof(p, stringcomparison.currentcultureignorecase) != -1) select fileline; foreach (var match in result) { system.diagnostics.debug.writeline("file: {0} lineno: {1}: text: {2}", match.filename, match.linenumber, match.text); }
or if want, can combine 1 linq query (but thats not readable think):
var result = fileline in (directory.getfiles(patha) .select(filename => new { filename = filename, lines = file.readalllines(filename) }) .selectmany(file => file.lines.select((line, idx) => new { linenumber = idx + 1, text = line, filename = file.filename }))) file.readalllines(pathb).any(p => fileline.text.indexof(p, stringcomparison.currentcultureignorecase) != -1) select fileline;
Comments
Post a Comment