Solved

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

Posted on 2011-02-18
8
608 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

752 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