0

I'm very new to c# and am working on a project where I need to search through a directory for specific document types and record the names of all of them. With these document names I then need to check that they all start with an ID in the form xxxx-xxxx-xxxxxx-xxx-xxx and save only the ones that both of these statements are true.

I use :

            var filesToBeRead = Directory.GetFiles(FBD.SelectedPath, "*.*", SearchOption.AllDirectories)  
            .Where(s => s.EndsWith(".txt") || s.EndsWith(".docx") || s.EndsWith(".doc")|| s.EndsWith(".xls") || s.EndsWith(".docm") || s.EndsWith(".xlsm") || s.EndsWith(".xlsx")); 

To get all the files in my specified directory and save them to an IEnumerable string. I then copy this to a list because I think that a list would be easier to manipulate. (I'm not sure if they are actually just the same thing and I'm making extra work for myself).

            var allFiles = new List<string>();
            allFiles = filesToBeRead.ToList();

Then from here I'm not really sure what to do. My idea is: since xxxx-xxxx-xxxxxx-xxx-xxx is the format I'm looking for at the start of each document name and it is 24 chars long that I would search for the 25th char and copy everything in the string before that. I'm not entirely sure how to do this and more specifically I'm not sure how to loop through the list and copy that every time.

My idea is to use a foreach but I'm not sure how to specify that I'm looking for the 25th char and copy everything before that.

Any help or direction to a web page to read into how to do this would be greatly appreciated.

D Phillips
  • 13
  • 3
  • Are you asking how to get the filename without the extension by any chance? If you are, then see [this question](https://stackoverflow.com/questions/4804990/getting-file-names-without-extensions/4805012). – ProgrammingLlama Jun 03 '21 at 09:05

2 Answers2

0

This linq query retrive a List of all the file that ends with these extensions and have the 25th char equal to x. You can change charToSearch in order to find only the file that you need

var charToSearch = 'x';
var filesToBeRead = Directory.GetFiles(FBD.SelectedPath, "*.*", SearchOption.AllDirectories)
                                 .Where(s => (s.EndsWith(".txt") || s.EndsWith(".docx") || s.EndsWith(".doc") || s.EndsWith(".xls") ||
                                 s.EndsWith(".docm") || s.EndsWith(".xlsm") || s.EndsWith(".xlsx"))
                                 && s[25] == charToSearch) .ToList();
Stefano Cavion
  • 641
  • 8
  • 16
0

I'm not sure if they are actually just the same thing and I'm making extra work for myself

They might be, but without going into too much detail, you don't need to worry about it because since they implement IEnumerable they behave the same.

var allFiles = new List<string>();
allFiles = filesToBeRead.ToList();

The object returned by new List<string>() is never used as the pointer to it (allFiles) is immediately overwritten with the result of filesToBeRead.ToList(). You could have used .ToList() at the end of your original statement if you want to force the results to be a List<T>:

var filesToBeRead = 
   Directory.GetFiles(FBD.SelectedPath, "*.*", SearchOption.AllDirectories)  
            .Where(s => s.EndsWith(".txt") 
                        || s.EndsWith(".docx")
                        || s.EndsWith(".doc")
                        || s.EndsWith(".xls")
                        || s.EndsWith(".docm")
                        || s.EndsWith(".xlsm")
                        || s.EndsWith(".xlsx"))
            .ToList();

As for how to see if the filenames match your pattern, "match your pattern" is the clue. The desired pattern looks like a Guid to me, so if you use Path.GetFilenameWithoutExtension() to get the filename, and then use Regex to see if it matches the pattern of a Guid, you'll probably be sorted.

Neil Barnwell
  • 41,080
  • 29
  • 148
  • 220