Solved

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

Posted on 2011-02-18
8
606 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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

831 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