Rename multiple files with VB6 Bank data

2

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 the column Cod of the table,

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

I need to rename all the images to equal the Name column, I tried to do this 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 carries the name of the file, however, it does not rename.

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

    
asked by anonymous 22.01.2016 / 13:47

1 answer

0

It would make it simpler, first check if the file exists and then execute the command to rename.

if Dir("c:\Temp
if Dir("c:\Temp%pre%01_1.jpg", vbArchive) <> "" then
    Name "c:\Temp%pre%01_1.jpg" As "c:\Temp\novonome.jpg"
End if
01_1.jpg", vbArchive) <> "" then Name "c:\Temp%pre%01_1.jpg" As "c:\Temp\novonome.jpg" End if
    
27.01.2016 / 19:12