Solved

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

Posted on 2011-02-18
8
603 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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks I'll give this a shot tomorrow.
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 9

Author Comment

by:skipper68
Comment Utility
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
Comment Utility
Points increased
0
 
LVL 74

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 50 total points
Comment Utility
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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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 …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video discusses moving either the default database or any database to a new volume.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

772 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

12 Experts available now in Live!

Get 1:1 Help Now