Home > Back-end >  Folder enumeration to identify a specific file leads to nothing
Folder enumeration to identify a specific file leads to nothing

Time:07-18

The below code does not identify the filepath and store it in the variable "compare_conv_filepath". This is a bug.

The code is written to enumerate files in a folder and identify a file if the filename is identical to "1.xlsx" and then store the entire filepath in a string variable.

The folder it enumarates will always ONLY have 1 file identical to "1.xlsx", but the folder might NOT have a file identical to "1.xlsx". Then the foreach should do nothing or a null value be given. The folder will always have ONE file called "orgFile_[some_filename]" and this filename could be "orgFile_1.xlsx".

Can you help?

using System.IO.Enumeration;

                // Identify converted spreadsheet in folder
                var conv_file = from file in
                Directory.EnumerateFiles(folder)
                            where file.Equals("1.xlsx")
                            select file;
                foreach (var file2 in conv_file)
                {
                    compare_conv_filepath = file2.ToString();

                    // Inform user of comparison
                    Console.WriteLine(compare_conv_filepath);
                    Console.WriteLine($"--> Comparing to: {compare_org_filepath}");
                }

CodePudding user response:

You're comparing the file path with it's name. try isolating the name using Path.GetFileName():

 where Path.GetFileName(file).Equals("1.xlsx")
  • Related