Help with code...

Rothariger

Regular
Joined
Apr 7, 2004
Messages
66
Hello ive make this code, to rename the files with the "Picture Date Taken" property and i leave it a little ugly... someone can help me to clean up..

i use datatables, for the files, so i can use the unique property and the orderby...
but can make it other way... change it at like...


Visual Basic:
 Private Sub BDcmdReemplazar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BDcmdReemplazar.Click
        Dim file As String
        Dim pattern As String
        Dim i As Integer
        Dim Fecha As DateTime = DateTime.MinValue
        Dim myDr As DataRow
        Dim myDataTable As New DataTable
        Dim tmpdate As String

        If MsgBox("Se van a Renombrar " & System.IO.Directory.GetFiles(BDtxtPath.Text, cmbExt.SelectedValue).Length & " Archivos." & vbCrLf & "Desea Continuar", MsgBoxStyle.YesNo, "Confirmar") = MsgBoxResult.No Then
            Exit Sub
        End If

        myDataTable.Columns.Add(New DataColumn("Numero", GetType(System.Decimal)))
        myDataTable.Columns.Add(New DataColumn("Nombre", GetType(System.String)))
        myDataTable.Columns(1).Unique = True
        myDataTable.Columns.Add(New DataColumn("Path", GetType(System.String)))
        myDataTable.Columns.Add(New DataColumn("Fecha", GetType(System.DateTime)))
        myDataTable.Columns.Add(New DataColumn("Ext", GetType(System.String)))

        For Each file In System.IO.Directory.GetFiles(BDtxtPath.Text, cmbExt.SelectedValue)
            Dim myFile As New System.IO.FileInfo(file)
            myDr = myDataTable.NewRow
            myDr(1) = myFile.Name
            myDr(2) = myFile.Directory
            tmpdate = Propert(myFile.FullName)
            If Trim(tmpdate).CompareTo("0000:00:00 00:00:00") = 0 Then
                myDr(3) = Now
            Else
                myDr(3) = CType((tmpdate.Substring(0, 10)).Replace(":", "-"), Date)
            End If
            myDr(4) = myFile.Extension

            myDataTable.Rows.Add(myDr)
        Next
        i = 0
        For Each myDr In myDataTable.Select("", "Fecha")
            If Format(Fecha, "dd-MM-yyyy") <> Format(myDr(3), "dd-MM-yyyy") Then
                Fecha = myDr(3)
                i = 0
            End If
            Try
                System.IO.File.Move(myDr(2) & "\" & myDr(1), myDr(2) & "\" & Format(myDr(3), "dd-MM-yyyy") & IIf(i = 0, "", " - " & i) & myDr(4))
            Catch ex As IOException
                MsgBox(ex.Message, MsgBoxStyle.Critical, ex.ToString)
            Finally
                i += 1
            End Try
        Next

        myDr = Nothing
        Fecha = Nothing
    End Sub

    Private Function Propert(ByVal file As String) As String
        Dim img As Image = Image.FromFile(file)
        Dim x As String
        Try
            For Each iPI As Drawing.Imaging.PropertyItem In img.PropertyItems

                Select Case iPI.Id
                    Case "9003" 'Date
                        If Trim(ASCIIEncoding.ASCII.GetString(iPI.Value)) <> "" Then
                            Return ASCIIEncoding.ASCII.GetString(iPI.Value)
                        End If

                    Case 306 'PropertyTag DateTime
                        If Trim(ASCIIEncoding.ASCII.GetString(iPI.Value)) <> "" Then
                            Return ASCIIEncoding.ASCII.GetString(iPI.Value)
                        End If

                    Case 36867 'PropertyTag ExifDTOrig
                        If Trim(ASCIIEncoding.ASCII.GetString(iPI.Value)) <> "" Then
                            Return ASCIIEncoding.ASCII.GetString(iPI.Value)
                        End If

                    Case 36868 'PropertyTagExif DTDigitized
                        If Trim(ASCIIEncoding.ASCII.GetString(iPI.Value)) <> "" Then
                            Return ASCIIEncoding.ASCII.GetString(iPI.Value)
                        End If
                End Select
            Next
        Catch
        Finally
            img = Nothing
        End Try
    End Function

anything just ask...


PD: My english is every day worst..!!!



thanks and salute!!!
 
Clean up your code?

OK, let's get to cleaning it up.

Visual Basic:
        myDataTable.Columns.Add(New DataColumn("Numero", GetType(System.Decimal)))
        myDataTable.Columns.Add(New DataColumn("Nombre", GetType(System.String)))
        myDataTable.Columns(1).Unique = True
        myDataTable.Columns.Add(New DataColumn("Path", GetType(System.String)))
        myDataTable.Columns.Add(New DataColumn("Fecha", GetType(System.DateTime)))
        myDataTable.Columns.Add(New DataColumn("Ext", GetType(System.String)))
The third line looks like a rock in a bowl of peppermints.
Move it either to before those lines or afterwards.

Select Case iPI.Id
Id is probably a number so

Case "9003" 'Date
you need to take off the quotes.

Trim(ASCIIEncoding.ASCII.GetString(iPI.Value))
The value of this function call is used twice in the subroutine always trimmed
So instead, why not do
SomeString = Trim(ASCIIEncoding.ASCII.GetString(iPI.Value))
and use SomeString

myDr(3) = CType((tmpdate.Substring(0, 10)).Replace(":", "-"), Date)
DateTime.Parse instead of CType

Format(Fecha, "dd-MM-yyyy") <> Format(myDr(3), "dd-MM-yyyy") Then
I don't think formatting both dates are necessary here, when you can do a direct comparison.

IIf(i = 0, "", " - " & i)
IIfs are ugly IMO... make that into a full If statement.

If MsgBox("Se van a Renombrar " & System.IO.Directory.GetFiles(BDtxtPath.Text, cmbExt.SelectedValue).Length & " Archivos." & vbCrLf & "Desea Continuar", MsgBoxStyle.YesNo, "Confirmar") = MsgBoxResult.No Then
Exit Sub
End If

to kill the Exit Sub, check for MsgBoxResult.Yes instead and put the rest of the code inside.
 
at weekend, i take a look to your suggestions, and make a callback...


thanks!!!


Add: i also meant to change the code, i dont like the datatable thing, but i dont get other way working..


thanks once again!
 
Last edited:
Back
Top