Search Files on the Basis of Names Only (Not Extension) c#

Code Snippets 4 U
var ListContainingData = new ExcelDataReader(filePath,1).ReadExcelColumns();
                // there may file titles which are same ... So, find those titles
                var filesInStoragePath = Directory.GetFiles(storagePath);
                ListContainingData.ForEach(l =>
                {
                    if(ListContainingData.Where(ll=>String.Compare(ll.Title,l.Title, true) == 0).Count() > 1)
                    {
                        // remove the file with that title
                        var fileToRemove = filesInStoragePath.Where(f => String.Compare(Path.GetFileNameWithoutExtension(f), l.Title, true) == 0).FirstOrDefault();
                        File.Delete(fileToRemove);
                    }
                });

So, in above code ‘ListContainingData ‘ contains file titles only and not extension. Now, I have same titles many times in ‘ListContainingData ‘ but that has to be unique. So, I had downloaded files with those titles but few of them were overwritten due to same title. So, to remove those files I had to search for files which could be overwritten.

Leave a Reply

Your email address will not be published. Required fields are marked *

+ sixty = 70