0

How can I check all the files inside a directory that contains the (.jpg,.jpeg,.png and .pdf) file format and then would only proceed to store the filenames inside a variables if those files exist? I tried using this code but it does not work. Reason why I said it does not work is because the process I put inside is not being initiated. Is there something wrong with my code? Please enlighten me or lead me to the proper way. All help is appreciated!

if(Directory.GetFiles(directory).All(x=> string.Compare(Path.GetExtension(x),"*.pdf", StringComparison.CurrentCultureIgnoreCase) == 0) && Directory.GetFiles(directory).All(x=> string.Compare(Path.GetExtension(x),"*.jpg", StringComparison.CurrentCultureIgnoreCase))
{
    // insert process here of getting the file names that has the extension of .jpg,.jpeg,.png and .pdf
}
devbbg
  • 17
  • 7
  • "it does not work" why not? Does it throw an exception, if so which? Does it silently fail, if so is there log output? Does it not find any files? Please [edit] your question to include *why* it's not working – MindSwipe May 20 '20 at 06:33
  • That being said, I see you're using [this](https://learn.microsoft.com/en-us/dotnet/api/system.string.compare?view=netcore-3.1#System_String_Compare_System_String_System_String_System_StringComparison_) overload of the `string.Compare` method, which does not accept a regex, instead two strings to compare. So your comparing the extension of "fooBar.png" (so ".png") to "*.png", which is obviously not the same – MindSwipe May 20 '20 at 06:36
  • It is unclear do you want to process files if **all** or **any** files in the directory of the specified extensions? – Iliar Turdushev May 20 '20 at 06:37
  • I want to process all the files that contains the said extensions. @MindSwipe I have in my other code Directory.GetFiles but with a zip file and used " *.zip " and it worked , so I assumed that using " *.jpg" would work as well – devbbg May 20 '20 at 06:43
  • Yes, `Directory.GetFiles("...", "*.zip")` works, because of [this](https://learn.microsoft.com/en-us/dotnet/api/system.io.directory.getfiles?view=netcore-3.1#System_IO_Directory_GetFiles_System_String_System_String_) overload. Whoever `string.Compare` has no similar overload. I'm currently writing an answer, hold tight – MindSwipe May 20 '20 at 06:46
  • @devbbg Please, refer this link https://stackoverflow.com/q/163162/12833205. Does it answer your question? – Iliar Turdushev May 20 '20 at 06:54
  • @IliarTurdushev im sorry it doesn't :( I tried the code and tried to output them using foreach then it returned me this one System.Linq.Enumerable+WhereEnumerableIterator`1[System.String] – devbbg May 20 '20 at 07:14

1 Answers1

1

The overload of the string compare method you're using does not accept a pattern to compare to, instead a second string to compare the first to. Which means if you have a file "fooBar.png" in your directory, your eventually comparing it's extension (so ".png") to "*.png", which is not the same.

You also said you want to get all file names that end with one of a number of specified extensions, but your using .All(...), which only returns true if all items inside the enumeration match the given expression. So

All(x=> string.Compare(Path.GetExtension(x),"*.pdf", StringComparison.CurrentCultureIgnoreCase) == 0)

would only return true if all files inside the directory were pdf files.

There is also not necessarily a problem, but something sub-optimal in your code: Your reading the same content from disk multiple times, which is as said, sub-optimal.

That being said, here is some updated code to fix your problem:

var acceptedFileTypes = new List<string>
{
    ".png",
    ".pdf",
    ...
};

// Get all files in the specified directory
var filesInDirectory = Directory.GetFiles(directory);

// Only select those with accepted file extensions
// Explicit type for better understanding
List<string> supportedFiles = filesInDirectory.Where(
    file => acceptedFileTypes.Contains(Path.GetExtension(file))
).ToList();

// Do something with the supportedFiles
// e.g print them to the console:
foreach (var file in supportedFiles)
{
    Console.WriteLine($"Found supported file: {file}");
}

You can do whatever you want with this, put it in a method and swap acceptedFileTypes for a static member, or put this in a static class of its own etc.
As well you can add new file types easily by appending the List

MindSwipe
  • 7,193
  • 24
  • 47
  • ill check on this codes now, thank you so much for the help and the explanations as well! Will get back on you once done. – devbbg May 20 '20 at 07:58
  • it worked perfectly however I have minor changes, and would like you to edit it out. it should be Path.GetExtension and in the Console.WriteLine I did it like this Console.WriteLine("File name: {0}", file) – devbbg May 20 '20 at 08:25
  • I just edited the question to use `Path.GetExtension` (brain fart moment). But I wont be editing the `Console.WriteLine` to use formatting, as string interpolation (the `$` and `{...}`) get compiled as `string.Format(...)` as it is simply syntactic sugar and makes no difference (although it isn't available in C# < 6 and .NET Framework < 4.5) – MindSwipe May 20 '20 at 08:52