How to check if items of a list exist in another list in c#?

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

39 Replies - 1497 Views - Last Post: 16 October 2017 - 10:09 AM Rate Topic: -----

#1 Whateva_  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 28-August 16

How to check if items of a list exist in another list in c#?

Posted 10 October 2017 - 06:31 AM

I have some folders in a location in my D: drive say, D:\MyProjects and inside it are many folders like
123-101
210-254
301-210
...

Each of these folders have multiple layers of sub-folders and out of which 2 sub-folders namely xml and meta contains an *.xml file each of the same name i.e. if the *.xml file name is abc.xml in the sub-folder xml then *.xml file inside the meta sub-folder in the parent folder is also named abc.xml.
Now, I'm trying to modify some data in the xml files inside the folder xml after taking some data from the xml file in the meta in that same parent folder.
This is what I've done so far
string basePath = textBox1.Text;
			//get all *.xml files from the folders named xml and meta in a list
			var xmlFolders = Directory.GetDirectories(basePath, "xml", SearchOption.AllDirectories);
			var metaFolders = Directory.GetDirectories(basePath, "meta", SearchOption.AllDirectories);
			List<string> xmlFileList = new List<string>();
			List<string> metaFileList = new List<string>();

			foreach (var folder in xmlFolders)
			{
				string[] xmlFiles = Directory.GetFiles(folder, "*.xml");

				xmlFileList.AddRange(xmlFiles);
			}
			foreach (var folder in metaFolders)
			{
				string[] metaFiles = Directory.GetFiles(folder, "*.xml");

				metaFileList.AddRange(metaFiles);
			}
			//iterate through each xml file in xmlFileList
			foreach (var xmlFile in xmlFileList)
			{
				//get the full path of xmlFile so that I can open the file and modify if needed
				FileInfo file_info = new FileInfo(xmlFile);
				string fileLocation = file_info.FullName;
				string file_name = Path.GetFileName(fileLocation);
				
				/*now check whether the file with the same name exist in metaFileList
				and get its full path, open the file, do some stuff
				and if the file is not found, move on to next xmlFile and so on
				:HOW????*/
			}

Can anyone help?

Is This A Good Question/Topic? 0
  • +

Replies To: How to check if items of a list exist in another list in c#?

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13545
  • View blog
  • Posts: 54,057
  • Joined: 12-June 08

Re: How to check if items of a list exist in another list in c#?

Posted 10 October 2017 - 07:04 AM

I am a little lost - what is your question?
Was This Post Helpful? 0
  • +
  • -

#3 Whateva_  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 28-August 16

Re: How to check if items of a list exist in another list in c#?

Posted 10 October 2017 - 07:08 AM

View Postmodi123_1, on 10 October 2017 - 07:04 AM, said:

I am a little lost - what is your question?

I want to know how do I check whether the *.xml file represented by xmlFile in the last foreach loop exist in metaFileList and if yes how do I get its full path so that I can open and do read/write stuff..
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13545
  • View blog
  • Posts: 54,057
  • Joined: 12-June 08

Re: How to check if items of a list exist in another list in c#?

Posted 10 October 2017 - 07:11 AM

Why not compare the paths you have to the known pattern? Seems fairly straight forward assuming you are not stripping that information out for some reason.
Was This Post Helpful? 0
  • +
  • -

#5 JapanDave  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 366
  • Joined: 01-February 16

Re: How to check if items of a list exist in another list in c#?

Posted 10 October 2017 - 07:14 AM

If I am understanding your question, if you want to check if the file exists, you simply just use System.IO and File.Exists(),

using System.IO;

      if (!File.Exists(yourFilePath))
      {
          //do your stuff
      }

Was This Post Helpful? 0
  • +
  • -

#6 Whateva_  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 28-August 16

Re: How to check if items of a list exist in another list in c#?

Posted 10 October 2017 - 07:20 AM

View Postmodi123_1, on 10 October 2017 - 07:11 AM, said:

Why not compare the paths you have to the known pattern? Seems fairly straight forward assuming you are not stripping that information out for some reason.

I don't know how to do it?
Are you talking about something like
if (metaFileList.Contains(file_name))

But that does not work...

View PostJapanDave, on 10 October 2017 - 07:14 AM, said:

If I am understanding your question, if you want to check if the file exists, you simply just use System.IO and File.Exists(),

using System.IO;

      if (!File.Exists(yourFilePath))
      {
          //do your stuff
      }

Well, I want to check whether the file exist in a certain sub-folder in the user given path, namely, meta and then get its full path to open it.
Was This Post Helpful? 0
  • +
  • -

#7 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13545
  • View blog
  • Posts: 54,057
  • Joined: 12-June 08

Re: How to check if items of a list exist in another list in c#?

Posted 10 October 2017 - 07:36 AM

Take a step back and consider how you would find a partial string in a string. There's the tried and true REGEX. There's an option for checking 'index of'. Then there's the issue of casing so you may want to throw both into upper or lower..

https://msdn.microso...(v=vs.110).aspx
https://msdn.microso...(v=vs.110).aspx
Was This Post Helpful? 1
  • +
  • -

#8 JapanDave  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 366
  • Joined: 01-February 16

Re: How to check if items of a list exist in another list in c#?

Posted 10 October 2017 - 08:37 AM

OK, in that case, bust out some LINQ, maybe something like this?

// Get all .xml files from "xml" directory
List<string[]> xmlFolders = Directory.GetDirectories(basePath, "xml", SearchOption.AllDirectories).Select(item => Directory.GetFiles(item, "*.xml")).ToList();
// Get all .xml files from "meta" directory

List<string[]> metaFolders = Directory.GetDirectories(basePath, "meta", SearchOption.AllDirectories).Select(item => Directory.GetFiles(item, "*.xml")).ToList();
//find the files that have the same name and extention

List<string> dupFiles = (from item in metaFolders.SelectMany(x => x) let sx = xmlFolders.SelectMany(s => s).Any(s => Path.GetFileName(s) == Path.GetFileName(item)) where sx select item).ToList();

      //Then loop through each of the files in meta directory that exist in both meta and xml directories
      //item will hold the full filepath
      foreach (var item in dupFiles)
      {
         //do what you need with complete filepaths here using 'item'
      }

This post has been edited by JapanDave: 10 October 2017 - 08:53 AM

Was This Post Helpful? 0
  • +
  • -

#9 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5921
  • View blog
  • Posts: 20,243
  • Joined: 05-May 12

Re: How to check if items of a list exist in another list in c#?

Posted 10 October 2017 - 05:29 PM

I suspect that code above will suffer from false positives given the following tree structure:
/
 123/
     xml/
         abc.xml
     data/
          def.xml
 456/
     xml/
         def.xml
     data/
          abc.xml


Was This Post Helpful? 1
  • +
  • -

#10 JapanDave  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 366
  • Joined: 01-February 16

Re: How to check if items of a list exist in another list in c#?

Posted 10 October 2017 - 05:34 PM

@Skydiver, I agree, but with how the questions stands, it should be able to serve the OP's basic needs and if the OP needs something more robust, the idea is there for them to tweak the code a little.
Was This Post Helpful? 0
  • +
  • -

#11 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5921
  • View blog
  • Posts: 20,243
  • Joined: 05-May 12

Re: How to check if items of a list exist in another list in c#?

Posted 10 October 2017 - 05:39 PM

Yeah, I'm just trying to read between the lines based on his two C# threads and his various VB.NET threads.
Was This Post Helpful? 0
  • +
  • -

#12 JapanDave  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 366
  • Joined: 01-February 16

Re: How to check if items of a list exist in another list in c#?

Posted 10 October 2017 - 07:21 PM

Ahh, did not see his other threads. I suspect he will need to look at Modi's links as well to fully get this working, but nothing more easier than dealing with strings. Thanks.

This post has been edited by JapanDave: 10 October 2017 - 07:27 PM

Was This Post Helpful? 0
  • +
  • -

#13 Whateva_  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 28-August 16

Re: How to check if items of a list exist in another list in c#?

Posted 11 October 2017 - 04:33 AM

View PostJapanDave, on 10 October 2017 - 08:37 AM, said:

OK, in that case, bust out some LINQ, maybe something like this?

// Get all .xml files from "xml" directory
List<string[]> xmlFolders = Directory.GetDirectories(basePath, "xml", SearchOption.AllDirectories).Select(item => Directory.GetFiles(item, "*.xml")).ToList();
// Get all .xml files from "meta" directory

List<string[]> metaFolders = Directory.GetDirectories(basePath, "meta", SearchOption.AllDirectories).Select(item => Directory.GetFiles(item, "*.xml")).ToList();
//find the files that have the same name and extention

List<string> dupFiles = (from item in metaFolders.SelectMany(x => x) let sx = xmlFolders.SelectMany(s => s).Any(s => Path.GetFileName(s) == Path.GetFileName(item)) where sx select item).ToList();

      //Then loop through each of the files in meta directory that exist in both meta and xml directories
      //item will hold the full filepath
      foreach (var item in dupFiles)
      {
         //do what you need with complete filepaths here using 'item'
      }

Wouldn't item will hold only the full filepath of the files in meta directory but I also need the full filepath of the files in xml directory also, as my goal is to open the file in meta directory, then get some data from it, then open the file with the same name in xml directory and paste that in.
BTW, could you mind explaining the dupFiles part of your code as I'm not that good at LINQ.
Thanks in advance. :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#14 JapanDave  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 366
  • Joined: 01-February 16

Re: How to check if items of a list exist in another list in c#?

Posted 11 October 2017 - 05:11 AM

You don't need to quote my whole post. :balloon:/>

Also, the LINQ is explained as best I could,
Also, I created a little test on my PC, and buried the two identical named pdf files in directories named meta and xml inside several nested directories to show what the file paths output will look like, and if you also need the same files in both directories, just create two lists for each directory.


//Create a basepath to search in
            string basePath = @"C:\Users\DaveB\Desktop";

            //Get all paths in folders named xml
            List<string[]> xmlFolders = Directory.GetDirectories(basePath, "xml", SearchOption.AllDirectories).Select(item => Directory.GetFiles(item, "*.pdf")).ToList();

            //Get all paths in folder named meta
            List<string[]> metaFolders = Directory.GetDirectories(basePath, "meta", SearchOption.AllDirectories).Select(item => Directory.GetFiles(item, "*.pdf")).ToList();

            //Get dup paths for files in meta directory
            List<string> metaDupFiles = (from item in metaFolders.SelectMany(x => x) //flatten the array of paths with SelectMany, item will represent each path in metafolders
                                         let sx = xmlFolders.SelectMany(s => s).Any(s => Path.GetFileName(s) == Path.GetFileName(item)) //test and see if dup paths exists in metafolders , item is the path in meta directoriy and s is the path in the xml directory
                                                                                                                                        //Path.GetFileName gets the filname and extension and that is what I am testing
                                                                                                                                        //to see if a dup exists
                                         where sx select item).ToList(); //Now the dup paths are identified, load into a list

            //Get dup paths for files in xml directory
            List<string> xmlDupFiles = (from item in xmlFolders.SelectMany(x => x) let sx = metaFolders.SelectMany(s => s).Any(s => Path.GetFileName(s) == Path.GetFileName(item)) where sx select item).ToList();

            //loo through the collected paths and show output
            foreach (var item in xmlDupFiles)
            {
                Debug.Print(item);
            }

            foreach (var item in metaDupFiles)
            {
                Debug.Print(item);
            }


And the output,

First for loop
C:\Users\DaveB\Desktop\testFolderOne\testFolderTwo\testFolderThree\xml\DNA N'Xon.pdf
C:\Users\DaveB\Desktop\testFolderOne\testFolderTwo\testFolderThree\xml\DNA Poltergeist.pdf

Second for loop
C:\Users\DaveB\Desktop\testFolderOne\testFolderTwo\testFolderThree\meta\DNA N'Xon.pdf
C:\Users\DaveB\Desktop\testFolderOne\testFolderTwo\testFolderThree\meta\DNA Poltergeist.pdf


OK, I have added some more comments. As pointed out by Skydiver, the first two lists will get every file in a directory named xml or meta. You may need some more advanced filtering if that is the case. Give it a go and if you get stuck come back for more help.

This post has been edited by JapanDave: 11 October 2017 - 05:14 AM

Was This Post Helpful? 0
  • +
  • -

#15 andrewsw  Icon User is online

  • say what now
  • member icon

Reputation: 6403
  • View blog
  • Posts: 25,880
  • Joined: 12-December 12

Re: How to check if items of a list exist in another list in c#?

Posted 11 October 2017 - 05:11 AM

I cannot explore this currently myself, so this is a bit of speculation, but I think I'd explore Enumerable.Intersect with an IEqualityComparer that compares filenames.

Just a suggestion, though, as there is absolutely nothing wrong with the suggested LINQ query approach :)

(It also just occurred to me that this might require calling Intersect twice if information is needed for both locations.)

Anyway, just an idea ;)
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3