Solved

VB.NET, delete non-animated GIF files, recurse through folders\subfolders; Out Of Memory Exception

Posted on 2011-02-18
8
604 Views
Last Modified: 2012-05-11
I have a huge file repository of images that has tens of thousands of files and probably close to as many folders.  I thought I'd write a quick script or app that will recurse through the folders and subfolders, check to see if the image is an animation, and if not, delete the file.  It was easy with the 1 or 2 test files but I'm getting an "Out Of Memory" exception when I try to do it with a folder that contains more than 200 files. I've attached my vb.NET form1.vb file.
The form only has 1 "Start" button to kick the process off.

Form1.vb

0
Comment
Question by:skipper68
  • 4
  • 3
8 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34930077
Would this not be simpler?
Dim gifs() As String = System.IO.Directory.GetFiles("C:\your\top\level\dir", "*.gif", System.IO.SearchOption.AllDirectories)

For Each strFileName As String
    FileProcessing(strFileName)
Next

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34930094
Whoops!  I forgot to finish a line  : \


Dim gifs() As String = System.IO.Directory.GetFiles("C:\your\top\level\dir", "*.gif", System.IO.SearchOption.AllDirectories)

For Each strFileName As String In gifs
    FileProcessing(strFileName)
Next

Open in new window

0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 450 total points
ID: 34930581
Just as an exercise in learning...you original approach done more conventionally:
*This should really be MULTI-THREADED...you could use the BackgroundWorker*
Imports System.IO
Public Class Form1

    Friend WithEvents FolderBrowserDialog1 As New System.Windows.Forms.FolderBrowserDialog

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        With FolderBrowserDialog1
            .RootFolder = Environment.SpecialFolder.Desktop
            .SelectedPath = "C:\Documents and Settings\"
            .Description = "Select the source directory"
        End With

        If FolderBrowserDialog1.ShowDialog = DialogResult.OK Then
            Dim ListOfFiles As New List(Of String)
            GetFilesRecursive(FolderBrowserDialog1.SelectedPath, "*.gif", ListOfFiles)
            For Each File As String In ListOfFiles
                FileProcessing(File)
            Next
        End If
    End Sub

    Public Shared Sub GetFilesRecursive(ByVal Path As String, ByVal Filter As String, ByVal FileList As List(Of String))
        Try
            ' Add only files matching the filter
            FileList.AddRange(Directory.GetFiles(Path, Filter))

            ' Loop through all subdirectories 
            For Each SubDirectory As String In Directory.GetDirectories(Path)
                GetFilesRecursive(SubDirectory, Filter, FileList)
            Next
        Catch ex As Exception
        End Try
    End Sub

    Public Shared Sub FileProcessing(ByVal imgPath As String)
        Try
            ' Load Image without locking the file:
            Dim img As Image = Nothing
            Using fs As New FileStream(imgPath, FileMode.Open, FileAccess.Read)
                img = Image.FromStream(fs)
            End Using

            'Check if It's an Animation
            If Not IsNothing(img) AndAlso Not System.Drawing.ImageAnimator.CanAnimate(img) Then
                'Delete the static file
                System.IO.File.Delete(imgPath)
            End If
        Catch ex As Exception
            MessageBox.Show("File: " & imgPath & vbCrLf & vbCrLf & ex.ToString, "Error Processing Image", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

End Class

Open in new window

0
 
LVL 9

Author Comment

by:skipper68
ID: 34939877
Thanks I'll give this a shot tomorrow.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 9

Author Comment

by:skipper68
ID: 34943312
Idle_Mind,
I tried your solution and I'm still getting the out of memory error on this line:
" If System.Drawing.ImageAnimator.CanAnimate(System.Drawing.Image.FromFile(imgPath)) = False Then"
 "out of memory" error
kaufmed, I have yet to try your solution.
0
 
LVL 9

Author Comment

by:skipper68
ID: 34943317
Points increased
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 50 total points
ID: 34943600
You didn't implement all of the logic provided by Idle_Mind. See his version of FileProcessing.
0
 
LVL 9

Author Closing Comment

by:skipper68
ID: 34944420
Really strange, I could have sworn I used the "Select All" button then copied and pasted.  

I see what you mean by the multithreaded being more efficient and that will be phase 2 of this if this is something I'm going to use more often.  

Thank you.  You saved me a good chuck of time and taught me something along the way which is why I love E.E.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now