document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. Imports System.IO
  2. Imports System.Text
  3.  
  4. Module Module1
  5.  
  6.     Dim log As New StringBuilder
  7.     Dim counter As Integer = 0
  8.     Dim NonDestructive As Boolean = False
  9.     Dim OverWrite As Boolean = True
  10.  
  11.     Private Extensions As New List(Of Ext)
  12.     Sub Main()
  13.         Dim maindir As String = System.Reflection.Assembly.GetExecutingAssembly().Location
  14.         maindir = System.IO.Path.GetDirectoryName(maindir)
  15.         Dim meh As String() = Environment.GetCommandLineArgs
  16.         If meh.Length < 2 Then
  17.             Console.WriteLine(vbCrLf & _
  18.                               " ExtensionBySignature by TizzyT" & vbCrLf & vbCrLf & _
  19.                               " Arguments:" & vbCrLf & _
  20.                               vbTab & "/n , /N = NonDestructive (does not rename the file)" & vbCrLf & _
  21.                               vbTab & "/o , /O = Do not overWrite log file (Create a new one)")
  22.         Else
  23.             Dim dire As String = maindir & "\\sig.txt"
  24.             If IO.File.Exists(dire) Then
  25.                 Dim readExt() As String = IO.File.ReadAllLines(dire)
  26.                 If readExt.Length = 0 Then GoTo missing
  27.                 For Each e As String In readExt
  28.                     Dim p() As String = e.Split(New String() {"="}, StringSplitOptions.RemoveEmptyEntries)
  29.                     Dim signa As String = p(1).Replace(" ", "").Trim
  30.                     Dim temp As Ext = New Ext(p(0), ReadHexStringToByte(signa))
  31.                     If p.Length = 2 Then
  32.                         If Not temp.Sig Is Nothing Then
  33.                             Extensions.Add(temp)
  34.                         Else
  35.                             Console.WriteLine("Extension (" & p(0) & ") has an invalid signature")
  36.                             log.AppendLine("Extension (" & p(0) & ") has an invalid signature")
  37.                         End If
  38.                     End If
  39.                 Next
  40.                 For Each Dir As String In meh
  41.                     Select Case Dir.ToLower
  42.                         Case "/n"
  43.                             NonDestructive = True
  44.                         Case "/o"
  45.                             OverWrite = False
  46.                         Case Else
  47.                             Try
  48.                                 Dim files() As String = Directory.GetFiles(Dir, "*", SearchOption.AllDirectories)
  49.                                 RenameBySignature(files)
  50.                             Catch ex As Exception
  51.                                 Console.WriteLine("Error on directory: " & Dir & vbCrLf & _
  52.                                                   "Error: " & ex.Message)
  53.                             End Try
  54.                     End Select
  55.                 Next
  56.                 Console.WriteLine("Renamed " & counter & " Files")
  57.                 log.AppendLine("Renamed " & counter & " Files")
  58.                 Dim logging As String = maindir & "\\log.txt"
  59.                 If Not OverWrite Then
  60.                     Dim logint As Integer = 0
  61.                     While File.Exists(logging)
  62.                         logint += 1
  63.                         logging = maindir & "\\log (" & logint & ").txt"
  64.                     End While
  65.                 End If
  66.                 IO.File.WriteAllText(logging, log.ToString)
  67.             Else
  68. missing:        Console.WriteLine("ExtensionBySignature by TizzyT")
  69.                 Console.WriteLine("Must define signatures in (sig.txt) file")
  70.                 Console.WriteLine("Format example: zip=504B0304")
  71.                 IO.File.WriteAllText(dire, String.Empty)
  72.             End If
  73.         End If
  74.         Console.ReadKey()
  75.     End Sub
  76.  
  77.     Private Sub RenameBySignature(ByVal files() As String)
  78.         For Each f As String In files
  79.             Console.WriteLine("File:    " & f)
  80.             log.AppendLine("File:    " & f)
  81.             Try
  82.                 Using sr As New FileStream(f, FileMode.Open, FileAccess.Read)
  83.                     Dim n As Boolean = False
  84.                     For i = 0 To Extensions.Count - 1
  85.                         Dim sig() As Byte = Extensions(i).Sig
  86.                         Dim newExt As String = Extensions(i).Ext
  87.                         For t = 0 To sig.Length - 1
  88.                             Dim r As Byte = sr.ReadByte
  89.                             If Not sig(t) = r Then
  90.                                 sr.Position = 0
  91.                                 Exit For
  92.                             Else
  93.                                 If t = sig.Length - 1 AndAlso sig(t) = r Then
  94.                                     If Not f.ToLower.EndsWith("." & newExt) Then
  95.                                         Dim newname As String = f
  96.                                         Dim count As Integer = 1
  97.                                         Dim newfile As String = f & "." & newExt
  98.                                         While IO.File.Exists(newfile)
  99.                                             newfile = f & " (" & count & ")." & newExt
  100.                                         End While
  101.                                         sr.Close()
  102.                                         Console.WriteLine("Renamed: " & newfile & vbCrLf)
  103.                                         log.AppendLine("Renamed: " & newfile & vbCrLf)
  104.                                         If Not NonDestructive Then IO.File.Move(f, newfile)
  105.                                         counter += 1
  106.                                         n = True
  107.                                     Else
  108.                                         Console.WriteLine( _
  109.                                             "File already has the correct extension (" & newExt & ")" & vbCrLf)
  110.                                         log.AppendLine( _
  111.                                             "File already has the correct extension (" & newExt & ")" & vbCrLf)
  112.                                         n = True
  113.                                     End If
  114.                                 ElseIf t = sig.Length - 1 Then
  115.                                     sr.Close()
  116.                                 End If
  117.                             End If
  118.                         Next
  119.                         If n Then
  120.                             Exit For
  121.                         ElseIf i = Extensions.Count - 1 Then
  122.                             Console.WriteLine("File Signature Undefined" & vbCrLf)
  123.                             log.AppendLine("File Signature Undefined" & vbCrLf)
  124.                         End If
  125.                     Next
  126.                     sr.Dispose()
  127.                 End Using
  128.             Catch ex As Exception
  129.                 Console.WriteLine("Failed: " & ex.Message & vbCrLf)
  130.                 log.AppendLine("Failed: " & ex.Message & vbCrLf)
  131.             End Try
  132.         Next
  133.     End Sub
  134.  
  135.     Public Structure Ext
  136.         Dim Ext As String
  137.         Dim Sig() As Byte
  138.         Sub New(ByVal ext As String, ByVal bytes() As Byte)
  139.             Me.Ext = ext
  140.             Sig = bytes
  141.         End Sub
  142.     End Structure
  143.  
  144.     Public Function ReadHexStringToByte(ByVal Input As String) As Byte()
  145.         Dim ValidChar As String = "0123456789abcdef"
  146.         Dim FixedString As String = String.Empty
  147.         For Each character As Char In Input
  148.             If ValidChar.Contains(character.ToString.ToLower) Then FixedString &= character
  149.         Next
  150.         If Not FixedString.Length = 0 And FixedString.Length Mod 2 = 0 Then
  151.             Dim ResultBytes((FixedString.Length / 2) - 1) As Byte
  152.             Dim index As Integer = 0
  153.             For i = 0 To FixedString.Length - 1 Step 2
  154.                 Dim hexString As String = FixedString(i) & FixedString(i + 1)
  155.                 ResultBytes(index) = Convert.ToByte(hexString, 16)
  156.                 index += 1
  157.             Next
  158.             Return ResultBytes
  159.         Else
  160.             Return Nothing
  161.         End If
  162.     End Function
  163. End Module
');