Solved

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

Posted on 2011-02-18
8
610 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 86

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
Automating Terraform w Jenkins & AWS CodeCommit

How to configure Jenkins and CodeCommit to allow users to easily create and destroy infrastructure using Terraform code.

 
LVL 9

Author Comment

by:skipper68
ID: 34939877
Thanks I'll give this a shot tomorrow.
0
 
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

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…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

624 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