Rename Images to Newfolder

CharlesH

Active Member
Joined
Apr 23, 2005
Messages
467
Hi,

I'm trying to rename the file format for images in a folder on my directory.
I have a code that will loop thru the file. But, I have not had success in renaming the file.
The following is a code that I found and was trying to modify it.
Code:
Sub LoopThroughFiles()
    Dim MyObj As Object, MySource As Object, file As Variant
   file = Dir("c:\testfolder\")
   While (file <> "")
     ' If InStr(file, "test") > 0 Then
         MsgBox "found " & file
        file.SaveAs Filename:="C:\NewImagefolder.jpg"
    '     Exit Sub
    '  End If
     file = Dir
  Wend
End Sub

The Image in the folder my be ".Png" or "jpg". I want to be able to save the Image to a new Image folder.

Any suggestion would be grateful.

Charles
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Try this:
Code:
Public Sub Move_Image_Files()

    Dim file As String
    Dim sourceFolder As String, destinationFolder As String
    
    sourceFolder = "C:\testfolder\"
    destinationFolder = "C:\NewImagefolder\"
    
    If Right(sourceFolder, 1) <> "\" Then sourceFolder = sourceFolder & "\"
    If Right(destinationFolder, 1) <> "\" Then destinationFolder = destinationFolder & "\"
    
    file = Dir(sourceFolder)
    While file <> ""
        If Right(file, 4) = ".png" Or Right(file, 4) = ".jpg" Then
            MsgBox "Move " & sourceFolder & file & " to " & destinationFolder & file
            Name sourceFolder & file As destinationFolder & file
        End If
        file = Dir
    Wend
    
End Sub
 
Upvote 0
John,

Thanks for your reply. Your code worked as designed. However, I should have explained my situation clearer.
In my sample code you will noticed that I was trying to do a "SaveAs". The reason I was trying to do this I'm working on a file for "FileMaker". When I created the file I need to have my images load to it.
When I inserted my Images "FileMaker" was slow to respond and it and gave the error "FileMaker" not responding. I post a question on the "FileMaker" forum . And the file that I proved to them worked.(Only a few Images)
Longer story shorter. The Images that I was using I converted the "File" type (As your code does) programmatically. This failed. I then selected several Images and tested them in my FileMaker" file. This attempted also failed as "Not Responding".
I took those same Images edited them to change the file type to "".jpg". Saved them and the inserted them to my filemaker file. When testing the "Filemaker" file worked as designed. I did not receive the "Not Responding" error.
This lead me to believe that fr some reason "FileMaker" did not like
 
Upvote 0
Sorry, I have no knowledge of FileMaker, so can't help further.

My code doesn't convert the file type; it moves the file from one folder to another folder.
 
Upvote 0
John,

Thanks for your help. FileMaker aside. What I wanted to do is take each Image change the file type from ".png" to ".jpg". Even if the file type is already ".jpg" still do a "SaveAs".
As mentioned I manually edited several Images and Saved them as ".jpg".
However I'm not sure if this can be done. As mention I can use a Excel program that will do what I want. It's just for some reason Filemaker does not like it.

Charles
 
Upvote 0
Changing the file extension from ".png" to ".jpg" can be done with this line changed in my code:
Code:
                 Name sourceFolder & file As destinationFolder & Replace(file, ".png", ".jpg", 1, , vbTextCompare)
However that will not change the internal binary file type; a .png image file is still a .png image file if it has a .jpg extension.

You could try using IrfanView to convert image files. It can convert a batch of files and has a command line so it can be run from Excel using the VBA Shell function.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top