Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Comparing files in VS 2013 - console application

Posted on 2014-03-04
5
Medium Priority
?
304 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 12

Expert Comment

by:jmcmunn
ID: 39904217
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 64

Expert Comment

by:Fernando Soto
ID: 39904294
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 64

Expert Comment

by:Fernando Soto
ID: 39904348
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
ID: 39907603
C#
0
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 39907780
// 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

How to Create Failover DNS Record Sets in Route 53

Route 53 has the ability to easily configure DNS record sets specifically for failover scenarios. These failover record sets can be configured to failover to full-blown deployments in other regions or to a static HTML page that informs your customers of the issue.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

704 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