Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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...

 

 

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!!!

  • Leaders
Posted

Clean up your code?

 

OK, let's get to cleaning it up.

 

       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.

Iceplug, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

Posted (edited)

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!

Edited by Rothariger

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...