Question

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!

Était-ce utile?

La solution

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

Autres conseils

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());
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top