Rename multiple files with VB6 database data

I have a table with the following columns ID, Cod and Nome, I have a folder with hundreds of images, these images are with the beginning of the name equal to Column Cod of the table, IE:

Cod        Imagem
ao0001 ->  ao0001_1.jpg
           ao0001_2.jpg
           ao0001_3.jpg

I need to rename all images to be equal to column Name, I tried to do as follows:

Dim RenameArquivo As String

Set PrS = New ADODB.Recordset
SQL = "Select * From produto"
PrS.Open SQL, gConexao, adOpenStatic, adLockOptimistic, adCmdText

With PrS     

        RenameArquivo = Procura_Arquivo("c:\imagens\", PrS.Fields("cod") & "*")
        produto.Text = "c:\imagens\" & PrS.Fields("nome") & ".jpg"
        produto.Text = Replace(produto.Text, " ", "-")

           Dim FileName As String
           Dim NewFileName As String
           On Error Resume Next

           FileName = RenameArquivo
           NewFileName = produto.Text
           Name FileName As NewFileName

End With

Function Procura_Arquivos:

    Public Function Procura_Arquivo(Caminho As String, NomeArquivo As String) As String
    Dim lNullPos As Long
    Dim lResultado As Long
    Dim sBuffer As String

    On Error GoTo Procura_Arquivo_Error

    'Aloca espaco para a string sBuffer
    sBuffer = Space(MAX_PATH * 2)
    'inicia busca do arquivo
    lResultado = SearchTreeForFile(Caminho, NomeArquivo, sBuffer)

    ' Se houver um caracter Nulo , remove
    If lResultado Then
       lNullPos = InStr(sBuffer, vbNullChar)
        If Not lNullPos Then
           sBuffer = Left(sBuffer, lNullPos - 1)
        End If
       'Retorna o nome do arquivo encontrado
        Procura_Arquivo = sBuffer

    Else
        'nao achou nada
        Procura_Arquivo = vbNullString
    End If

    Exit Function
    Procura_Arquivo_Error:
        Procura_Arquivo = vbNullString
    End Function

It brings the variable RenameArquivo and brings the name of the file, however, it does not rename.

What's wrong? Is it possible to do this in a loop ?

Author: bfavaretto, 2016-01-22

1 answers

Would make it simpler, first check if the file exists and then run the command to rename.

if Dir("c:\Temp\0001_1.jpg", vbArchive) <> "" then
    Name "c:\Temp\0001_1.jpg" As "c:\Temp\novonome.jpg"
End if
 0
Author: davidterra, 2016-01-27 18:12:22