Check if folder contains images

Hello all! I have a folder with alot of subfolders, inside each subfolder there its a pdf file and “sometimes” images. Is it possible to loop trough all subfolders and check who has images and who dont?

Yes. You can use two for each loop one for folder and one for subfolder and then use a if condition to check if filename contains image

for ex:-
for each subfolder in folder
{
for each file in subfolder
{
if(file.contains(".png"or".jpeg"))
{
write line (“image exist”)
}
}
}

Please mark this as solution of your query is resolved

works almost, the problem is that it finds a pdf file first so it think that it dont contains a img in that folder but it does it just have to skip the pdf file

is it possible to just check if the folder contains more than 1 file ?

use if loop as i mentioned earlier
if(image.contains("pdf"or “.jpeg”))
Then - image exist
else - image doesnt exist

int a = Directory.getfiles(FolderPath).count

This will give you count . Folder path is variable which contains path of your folder
Use this in For each loop and print a outside the loop

Let’s try the following

for each subfolder in folder
{
boolean isImage = Directory.GetFiles(subfolder).Any(Function(x) x.Contains(“jpeg”)
}

This will check if “subfolder” contains “any” file which name contains “jpeg”

Cheers

1 Like

so i need 2 for each ? one that holds subfolder and one that holds files ? i assume “x” its files

worked :smiley: ! thanks m8

This approach is only applicable in case you just need to check if the “subfolder” contains “any” image file (but not the particular file name) - then you need just one “for each” - just loop through “subfolders”.

The “x” represents every file name in the “subfolder”.
The function (x.Contains(“jpeg”)) is so called “lambda expression” and will be called for every element (file name) returned by GetFiles method

Lear more about it here
https://linqsamples.com/linq-to-objects/quantifiers/Any-lambda-vb

Cheers

thanks!

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.