Avatar of LuckyLucks
LuckyLucks

asked on 

Comparing files in VS 2013 - console application

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
.NET ProgrammingC#

Avatar of undefined
Last Comment
Fernando Soto
Avatar of jmcmunn
jmcmunn
Flag of United States of America image

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.
Avatar of Fernando Soto
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

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.
Avatar of LuckyLucks
LuckyLucks

ASKER

C#
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo