Set a folder path to rename all files instead of folder pickup method

randaubienghoc

New Member
Joined
Apr 25, 2020
Messages
19
Office Version
  1. 2016
Platform
  1. Windows
Hi everyone,

Could you please help me to revise the code in the Excel file so that I can set a folder path and then all files in the folder can be renamed?: Rename-1.7z

In the file that I obtained from the Internet, the author used the method to manually navigate a folder and select multiple files in the folder to rename. The file purpose is to rename filenames to VBA supported characters.

I preferred to using a defined folder path but I do not figure it out. I hope you guys can help me. Thank you.
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Have a look into this, Renaming File
Hi @mehidy1437 I just hope to change this
"With Application.FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect = False
.Show......"

INTO
"...For all files in folder path: C:\test example....."

Code to rename files are already done (For example: "âbcđ" => "abcd") as you could see from my file but I have difficulties to only revise code to apply for a specific folder with all files inside. Hope you could understand me. Thanks.
 
Upvote 0
See below & try,
VBA Code:
Function LoaiDauFile(ByVal SourceFile As String, ByVal TargetFile As String)
    With CreateObject("Scripting.FileSystemobject")
        .MoveFile SourceFile, TargetFile
    End With
End Function
Sub Main()
On Error Resume Next
    Dim SourceFile As String, TargetFile As String
    Dim Path As String
    With Application.FileDialog(msoFileDialogFolderPicker)
        '.Show 'make inactive
        .AllowMultiSelect = True
        .InitialFileName = "F:\EXCEL FILE\New folder\" 'change the path
        If .Show = -1 Then 'add this 
        For i = 1 To .SelectedItems.Count
            Path = .SelectedItems(i)
            With CreateObject("Scripting.FileSystemobject").getfolder(Path)
                For Each Fi In .Files
                    SourceFile = Fi.Path
                    TargetFile = Path & "\" & SourceToDest(Fi.Name, 1, 5)
                    LoaiDauFile SourceFile, TargetFile
                Next
            End With
        Next
        End If 'add this
    End With
End Sub
 
Upvote 0
Hi @mehidy1437 I tried your code but a blank window popup with no files inside. Please help to check it :((
1599546069352.png

1599546119399.png
 
Upvote 0
Its okay, just press ok. It will do job.
 
Upvote 0
Its okay, just press ok. It will do job.
Yes but it does not work correctly if many files. I do not know why. Here in example, I have 15 files in the folder, 1 file name has not been renamed.
Anyway, do you know how to prevent pop-up window, it is quite inconvenient.
Thank you so much for your help.
1599550802217.png
 
Upvote 0
Hi @mehidy1437 I tried to close Excel file and reopen the file to run macro again, it works now. However, I still have to press OK button. Do you have any ideas to automatically press this button?
 
Upvote 0
No. I don't know, is this even possible or not? You may have to change the entire code to do that.
 
Upvote 0

Forum statistics

Threads
1,214,642
Messages
6,120,701
Members
448,980
Latest member
CarlosWin

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