Sixem

.NET Check For Duplicated Files

Aug 31st, 2013
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 2.34 KB | None | 0 0
  1. 'MR_List listbox should contain the filenames, Path is the path to the files
  2. 'Doesn't work for files under 0.09 kb can be changed (99 is the minimum bytes it will check for match)
  3.         Dim Container As String = "$"
  4.         Dim Path As String = Current_Path_MR
  5.         Dim Duplicates As Integer = 0
  6.         Dim Duplicates_Container = Nothing
  7.         For i As Integer = 0 To CInt(MR_List.Items.Count) - 1
  8.             Dim CurrentFile As String = String.Format("{0}\{1}", Path, MR_List.Items(i).ToString)
  9.             Dim CurrentInfo As System.IO.FileInfo
  10.             CurrentInfo = My.Computer.FileSystem.GetFileInfo(CurrentFile)
  11.             Dim FileSize As Long = CurrentInfo.Length
  12.             If Container.Contains(CStr(FileSize)) = True Then
  13.                 Dim Container_Match As String = Regex.Split(Container, String.Format(":{0}", CStr(FileSize))).GetValue(0)
  14.                 Dim SplitContainer() As String = Regex.Split(Container_Match, ">")
  15.                 Dim SplitMatch As String = (CStr(SplitContainer.GetValue(SplitContainer.Length - 2)).Split("*").GetValue(0))
  16.                 If GetBase64(CurrentFile, 99) = GetBase64(SplitMatch, 99) = True Then
  17.                     Duplicates += 1
  18.                     Duplicates_Container &= String.Format("{0}*", SplitMatch)
  19.                 End If
  20.             End If
  21.             Container &= String.Format("{0}*{1}>", CurrentFile, FileSize)
  22.         Next
  23.         Try
  24.             MessageBox.Show(String.Format("Found {0} Duplicates", Duplicates))
  25.             Dim Results() As String = Duplicates_Container.split("*")
  26.             Dim Message As String = Nothing
  27.             For Each x As String In Results
  28.                 Message &= x & Environment.NewLine
  29.             Next
  30.             MessageBox.Show(Message)
  31.         Catch ex As Exception
  32.         End Try
  33.     Private Function GetBase64(ByVal Filename As String, ByVal Chars As Integer)
  34.             Dim Temp As String = Nothing
  35.             Using BinaryFile As FileStream = New FileStream(Filename, FileMode.Open)
  36.                 Dim BinRead As BinaryReader = New BinaryReader(BinaryFile)
  37.                 Dim BinBytes As Byte() = BinRead.ReadBytes(CInt(BinaryFile.Length))
  38.             Temp = Convert.ToBase64String(BinBytes).Substring(0, Chars)
  39.                 BinaryFile.Close()
  40.             End Using
  41.             Return Temp.Substring(0, Chars)
  42.     End Function
Advertisement
Add Comment
Please, Sign In to add comment