Solved

vb.net - moving duplicated files

Posted on 2011-03-02
3
196 Views
Last Modified: 2012-05-11
Hello there,
I am using this code to create directories based on the date that the picture was taken. it looks for all of the images in a directory and then it moves them.
I have 2 directories with 500 images each, and two images are duplicates.. when I use the code bellow I get one error saying "cannot move the file already exists"
what I would like to do is to check its file size and overwrite if the same.. or if its different file size add a number at the end of the name..
Imports System.IO
Imports System.Text
Imports System.Drawing.Imaging
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If FolderBrowserDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            ProcessFiles(FolderBrowserDialog1.SelectedPath, "*.jpg")
        End If
    End Sub

    Private Sub ProcessFiles(ByVal Folder As String, ByVal Pattern As String)
        Dim di As New DirectoryInfo(Folder)
        For Each fi As FileInfo In di.GetFiles(Pattern, SearchOption.AllDirectories)
            Dim values() As String = FindDateTaken(fi.FullName).Split(":")
            If values.Length >= 2 Then
                values(2) = values(2).Split(" ")(0)
                Try
                    Dim dt As New DateTime(values(0), values(1), values(2))
                    Dim SubDi As New DirectoryInfo(Path.Combine(di.FullName, dt.Year))
                    If Not SubDi.Exists() Then
                        SubDi.Create()
                    End If
                    SubDi = New DirectoryInfo(Path.Combine(SubDi.FullName, dt.Month.ToString("00")))
                    If Not SubDi.Exists() Then
                        SubDi.Create()
                    End If
                    SubDi = New DirectoryInfo(Path.Combine(SubDi.FullName, dt.Day.ToString("00")))
                    If Not SubDi.Exists() Then
                        SubDi.Create()
                    End If

                    Dim NewFileName As String = Path.Combine(SubDi.FullName, fi.Name)
                    fi.MoveTo(NewFileName)
                Catch ex As Exception

                End Try
            End If
        Next
    End Sub

    Private Function FindDateTaken(ByVal strPicture As String) As String
        Const DATE_TAKEN As Integer = &H9003
        Dim dateTaken As String = ""
        Using img As Image = Image.FromFile(strPicture)
            If img.PropertyIdList.Contains(DATE_TAKEN) Then
                Dim pitem As PropertyItem = img.GetPropertyItem(DATE_TAKEN)
                dateTaken = Encoding.UTF8.GetString(pitem.Value, 0, pitem.Value.Length)
            End If
        End Using
        Return dateTaken
    End Function

End Class

Open in new window

0
Comment
Question by:XK8ER
3 Comments
 
LVL 27

Expert Comment

by:planocz
ID: 35023781
In your Catch ex area you need to add the code to check size and/or nodifiy date.
Then Do something with the file or just exit the try catch.
With being in the for next loop it should continue to the next file.
0
 
LVL 1

Author Comment

by:XK8ER
ID: 35023786
ok, but how exactly do I do that?
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 35023853
Change this:
Dim NewFileName As String = Path.Combine(SubDi.FullName, fi.Name)
                    fi.MoveTo(NewFileName)

Open in new window


To:  
Dim NewFileName As String = Path.Combine(SubDi.FullName, fi.Name)
                    Dim NewFI As New FileInfo(NewFileName)
                    If NewFI.Exists Then
                        If fi.Length = NewFI.Length Then
                            ' overwrite
                            fi.CopyTo(NewFileName, True)
                            fi.Delete()
                        Else
                            ' rename
                            Dim counter As Integer = 1
                            Dim BaseName As String = Path.GetFileNameWithoutExtension(NewFileName)
                            Dim Extension As String = Path.GetExtension(NewFileName)
                            NewFileName = Path.Combine(SubDi.FullName, BaseName & "_" & counter & Extension)
                            While File.Exists(NewFileName)
                                counter = counter + 1
                            End While
                            fi.MoveTo(NewFileName)
                        End If
                    Else
                        fi.MoveTo(NewFileName)
                    End If

Open in new window

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

792 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