1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
| Imports System.Drawing.Imaging
Imports System.Text
Public Class PicturesRenamer
Const Software As String = "PicturesRenamer"
Private Sub ButtonSource_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSource.Click
If FolderBrowserDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
TextBoxSource.Text = FolderBrowserDialog.SelectedPath & "\"
End If
End Sub
Private Sub ButtonDestination_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonDestination.Click
If FolderBrowserDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
TextBoxDestination.Text = FolderBrowserDialog.SelectedPath & "\"
End If
End Sub
Private Sub RenamePictures(ByVal SourceFolder As String, ByVal DestinationFolder As String, ByVal PictureName As String)
Dim MyPicture As Image
Dim MyProperty As PropertyItem
Dim Extension As String
Dim PropertyValue As String
Dim ShootingDate As DateTime
Dim FileName As String
Dim FileDate As String
Dim NumFiles As Integer
Dim Files() As String
Dim Position As Integer
Dim OldPictureName As String
Dim NewPictureName As String
Dim I As Integer
While PictureName.StartsWith(" ") = True And Len(PictureName) > 0
PictureName = Mid(PictureName, 2)
TextBoxName.Text = Mid(PictureName, 2)
End While
If Len(PictureName) = 0 Then
MsgBox("Please enter a name for your pictures.", vbOKOnly + vbInformation, Software)
TextBoxName.Focus()
Exit Sub
End If
If InStr(PictureName, "\") <> 0 Or InStr(PictureName, "/") <> 0 Or InStr(PictureName, ":") <> 0 Or _
InStr(PictureName, "*") <> 0 Or InStr(PictureName, "?") <> 0 Or InStr(PictureName, """") <> 0 Or _
InStr(PictureName, "<") <> 0 Or InStr(PictureName, ">") <> 0 Or InStr(PictureName, "|") <> 0 Then
MsgBox("\ / : * ? "" < > | chars are forbidden.", vbOKOnly + vbInformation, Software)
TextBoxName.Focus()
Exit Sub
End If
If Len(SourceFolder) = 0 Then
MsgBox("Please select a directory which contains your pictures.", vbOKOnly + vbInformation, Software)
ButtonSource.Focus()
Exit Sub
End If
If Len(DestinationFolder) = 0 Then
MsgBox("Please select a destination directory for your renamed pictures.", vbOKOnly + vbInformation, Software)
ButtonDestination.Focus()
Exit Sub
End If
If SourceFolder = DestinationFolder Then
MsgBox("Please select differents directories.", vbOKOnly + vbInformation, Software)
ButtonDestination.Focus()
Exit Sub
End If
FileName = Dir(SourceFolder)
If Len(FileName) = 0 Then
MsgBox("No file founded in source folder.", vbOKOnly + vbInformation, Software)
ButtonSource.Focus()
Exit Sub
End If
Cursor = Cursors.WaitCursor
Do While Len(FileName) > 0
If FileName <> "." And FileName <> ".." Then
Position = InStr(1, FileName, ".", vbTextCompare) + 1
Extension = Mid(FileName, Position)
If LCase(Extension) = "jpg" Then
NumFiles = NumFiles + 1
ReDim Preserve Files(0 To NumFiles)
MyPicture = Image.FromFile(SourceFolder & FileName)
MyProperty = MyPicture.GetPropertyItem(&H9003)
PropertyValue = Encoding.ASCII.GetString(MyProperty.Value, 0, MyProperty.Len).TrimEnd(Chr(0))
ShootingDate = DateTime.ParseExact(PropertyValue, "yyyy:MM:dd HH:mm:ss", Nothing)
FileDate = Format(ShootingDate, "yyyyMMddhhmmss")
Files(NumFiles) = FileDate & "*" & FileName
MyPicture.Dispose()
End If
End If
FileName = Dir()
Loop
QuickSort(Files, 1, NumFiles)
For I = 1 To NumFiles
Position = InStr(1, Files(I), "*", vbTextCompare) + 1
FileName = Mid(Files(I), Position)
OldPictureName = SourceFolder & FileName
NewPictureName = DestinationFolder & PictureName & " (" & Format(I, "###000") & ").jpg"
FileCopy(OldPictureName, NewPictureName)
Next I
Cursor = Cursors.Default
If NumFiles > 0 Then
MsgBox("Operation finished.")
Else
MsgBox("No pictures to rename founded.")
ButtonSource.Focus()
End If
End Sub
Private Sub QuickSort(ByVal List() As String, ByVal Min As Long, ByVal Max As Long)
Dim Median As String
Dim Hight As Long
Dim Low As Long
Dim i As Long
If Min >= Max Then Exit Sub
i = Int((Max - Min + 1) * Rnd() + Min)
Median = List(i)
List(i) = List(Min)
Low = Min
Hight = Max
Do
Do While List(Hight) >= Median
Hight = Hight - 1
If Hight <= Low Then Exit Do
Loop
If Hight <= Low Then
List(Low) = Median
Exit Do
End If
List(Low) = List(Hight)
Low = Low + 1
Do While List(Low) < Median
Low = Low + 1
If Low >= Hight Then Exit Do
Loop
If Low >= Hight Then
Low = Hight
List(Hight) = Median
Exit Do
End If
List(Hight) = List(Low)
Loop
QuickSort(List, Min, Low - 1)
QuickSort(List, Low + 1, Max)
End Sub
Private Sub ButtonRename_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonRename.Click
Call RenamePictures(TextBoxSource.Text, TextBoxDestination.Text, TextBoxName.Text)
End Sub
End Class |