0

I'm trying to enumerate and import a number of image files in a folder. My current code for counting the images is as follows -

Dim fullpath As String
fullpath = TxtPath.Text + "\"


Dim FileDirectory As New IO.DirectoryInfo(fullpath)
Dim FileJpg As IO.FileInfo() = FileDirectory.GetFiles("*.jpg")
Dim FileJpeg As IO.FileInfo() = FileDirectory.GetFiles("*.jpeg")
Dim FileGif As IO.FileInfo() = FileDirectory.GetFiles("*.gif")
Dim FileBmp As IO.FileInfo() = FileDirectory.GetFiles("*.bmp")
Dim FilePng As IO.FileInfo() = FileDirectory.GetFiles("*.png")

Dim count As Integer = 0

For Each File As IO.FileInfo In FileJpg
    count += 1
Next
For Each File As IO.FileInfo In FileJpeg
    count += 1
Next
For Each File As IO.FileInfo In FileGif
    count += 1
Next
For Each File As IO.FileInfo In FileBmp
    count += 1
Next
For Each File As IO.FileInfo In FilePng
    count += 1
Next

Is there a more efficient way to do this in one For loop, rather than the 5 separate ones - can you send an array of file extensions to GetFiles?

I'm also planning to use this code to import these images into a database, so having one loop would be much more efficient there too.

Thanks!

Optimaximal
  • 545
  • 1
  • 5
  • 28

2 Answers2

3

Not the best solution, but I don't have time for LINQ right now. Try this:

Dim extensions As New List(Of String)
extensions.Add("*.png")
' And so on, until all are in...

Dim fileCount As Integer
For i As Integer = 0 To extensions.Count - 1
  fileCount += Directory.GetFiles(txtPath.Text, extensions(i), SearchOption.AllDirectories).Length
Next
Dominic B.
  • 1,897
  • 1
  • 16
  • 31
1

It has been a while since I looked at VB.NET, but in C# you could do something like this:

    static void Main(string[] args)
    {
        string filePath = @"I:\Archive\2009.12.21c";

        List<string> extensions = new List<string>{
            ".jpg",
            ".jpeg",
            ".png",
        };

        string[] countImages = System.IO.Directory.GetFiles(filePath);

        foreach (string file in countImages)
        {
            if (extensions.Contains(System.IO.Path.GetExtension(file).ToLowerInvariant()))
            {
                //This is where you can add to your count for found files
            }
        }
    }

These are all .NET classes, and should be easy enough to translate back to VB.NET.


Edit for @Trade

@Trade OK, was able to get to it. Ran your code (converted to C#) and my code, 6 loops mine then yours, then 6 more yours then mine. The 6 times yours followed by 6 runs through mine, then flipped them (All to make sure not a JIT warm up) and the results: The difference is more than an order of magnitude in preference of the approach I used:

Example: Yours is enum2, mine was enum1

Found 37 with dir enum1 in 609
Found 37 with dir enum1 in 469
Found 37 with dir enum1 in 462
Found 37 with dir enum1 in 455
Found 37 with dir enum1 in 448
Found 37 with dir enum1 in 406
Found 37 with dir enum2 in 6314
Found 37 with dir enum2 in 6888
Found 37 with dir enum2 in 5439
Found 37 with dir enum2 in 5614
Found 37 with dir enum2 in 5824
Found 37 with dir enum2 in 6342

Here is the converted code for your method:

private static void enum2(string filePath, List<string> extensions)
{
    System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
    sw.Start();
    int countCount = 0;
    for (int i = 0; i < extensions.Count(); i++)
    {
        string curExt = extensions[i];
        countCount += System.IO.Directory.GetFiles(filePath, "*" +
            curExt, System.IO.SearchOption.TopDirectoryOnly).Length;
    }
    sw.Stop();

    Console.WriteLine("Found {0} with dir enum2 in {1}",
        countCount, sw.ElapsedTicks.ToString());
}
Austin T French
  • 5,022
  • 1
  • 22
  • 40
  • 1
    Why not simply take the Length-property from Directory.GetFiles with using its overload to set an extension? Also countImages as a string[] could be a bit confusing. – Dominic B. Apr 17 '14 at 11:38
  • @Trade on the name of countImages you are right, it could be. On the count method (its still early mind you, so brain a little fuzzy) it seems that would be slower as the disk would have to be enumerated more times. I will attempt to stop watch both routes later though. – Austin T French Apr 17 '14 at 11:50
  • 1
    @Trade, does that help at all or satisfy any amount of curiosity? – Austin T French Apr 18 '14 at 01:41
  • 1
    It's interesting to see, so yes. – Dominic B. Apr 18 '14 at 08:19