Solved

Comparing files in VS 2013 - console application

Posted on 2014-03-04
5
284 Views
Last Modified: 2014-03-10
Hello EEE,

   I am trying this after a long while...created a new project in VS 2013 below. Now I need to read in two files from C:\logs. One is called All.txt, the other is called other.txt. Both have a listing of full paths for files under C:\logs\All and C:logs\Other.

I would like to compare both text files (just file names with the leading directories stripped) and find which ones are not in both.

How can I do that?


Module Module1

    Sub Main()

    End Sub

End Module
0
Comment
Question by:LuckyLucks
  • 3
5 Comments
 
LVL 12

Expert Comment

by:jmcmunn
Comment Utility
Psuedocode...

Open both files
   var line=ReadLineFromFile1
   while(not eof)
      add stripped file name to Firstlist<string>

   line = ReadLineFromFile2
   while(not eof)
      if stripped file is in list, remove from Firstlist
      else add to Secondlist<string>

Close both files
now, all of the files in Firstlist are in the first file, but not the second one
and, all of the files in Secondlist are in the second file, not the first

if you need help on any specific part of that, feel free to post here or ask a new question.  It is all very easily Googled.
0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
Hi LuckyLucks;

The code snippet below should give you what you are looking for.

'Read all the file names and remove directory part and store in array
Dim allFiles As String() = File.ReadAllLines("C:\Working Directory\All.txt") _
                           .Select(Function(f) Path.GetFileName(f)).ToArray()

Dim OtherFiles As String() = File.ReadAllLines("C:\Working Directory\Other.txt") _
                             .Select(Function(f) Path.GetFileName(f)).ToArray()

' And array of all files in All.txt that are NOT in Other.txt
Dim allNotInOthers As String() = allFiles.Except(OtherFiles).ToArray()
' And array of all files in Other.txt that are NOT in All.txt
Dim otherNotInAll As String() = OtherFiles.Except(allFiles).ToArray()

Open in new window

0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
Hi LuckyLucks;

In your question you have VB code snippet but in the question tag you have .Net and C#. If you need to have the code translated to C# please let me know.
0
 

Author Comment

by:LuckyLucks
Comment Utility
C#
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
Comment Utility
// Read all the file names and remove directory part and store in array
string[] allFiles  = File.ReadAllLines(@"C:\logs\All.txt")
                         .Select(f => Path.GetFileName(f)).ToArray();

string[] OtherFiles = File.ReadAllLines(@"C:\logs\Other.txt")
                          .Select(f => Path.GetFileName(f)).ToArray();

// And array of all files in All.txt that are NOT in Other.txt
string[] allNotInOthers = allFiles.Except(OtherFiles).ToArray();
// And array of all files in Other.txt that are NOT in All.txt
string[] otherNotInAll = OtherFiles.Except(allFiles).ToArray();               

Open in new window

0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now