Rename file after move to subfolder

Nguyen Anh Dung

Board Regular
Joined
Feb 28, 2020
Messages
133
Office Version
  1. 2016
Platform
  1. Windows
i have code as below. after move file to subfolder. Help me rename file.

1601440917251.png

example: i move list file to folder 20200921_22_014_TPThuanAn_BD_GS103997
20200921_22_014_TPThuanAn_BD_GS103997004.jpg
20200921_22_014_TPThuanAn_BD_GS103997003.jpg
20200921_22_014_TPThuanAn_BD_GS103997002.jpg
20200921_22_014_TPThuanAn_BD_GS103997001.jpg
20200921_22_014_TPThuanAn_BD_GS103997000.jpg

after move finish i want rename file as below picture:

1601441097873.png

Code:
Public Sub Move_Files()

    Dim sourceFolder As String, fileName As String
    Dim destinationFolder As String, foundDestinationFolder As String
    Dim missingFolders As String
    On Error Resume Next
    'sourceFolder = "D:\Vidu\"
    sourceFolder = Application.InputBox("Nhap duong dan: ")
    If Right(sourceFolder, 1) <> "\" Then sourceFolder = sourceFolder & "\"
    
    'Loop through *.xls files in source folder
    
    missingFolders = ""
    fileName = Dir(sourceFolder & "*.jpg")
    While fileName <> vbNullString
        If Right(fileName, 4) = ".jpg" Then
            destinationFolder = Left(fileName, InStrRev(fileName, "GS") + 7)
            'destinationFolder = Left(fileName, Len(fileName) - 7)
            foundDestinationFolder = Find_Subfolder(sourceFolder, destinationFolder)
            If foundDestinationFolder <> "" Then
                Name sourceFolder & fileName As foundDestinationFolder & fileName
            Else
                missingFolders = missingFolders & vbCrLf & destinationFolder
            End If
        End If
        fileName = Dir
    Wend
    
    If missingFolders = "" Then
        MsgBox "Tat Ca Folder exist.  All files moved to their respective destination folder"
    Else
         MsgBox "Folder Khong Ton Tai" & vbCrLf & _
                missingFolders
    End If

End Sub



Private Function Find_Subfolder(folderPath As String, subfolderName As String) As String

    Static FSO As Object
    Dim FSfolder As Object, FSsubfolder As Object
    
    'Traverse subfolders from a folder path and return when matching folder name found
    
    If FSO Is Nothing Then Set FSO = CreateObject("Scripting.FileSystemObject")
    
    Set FSfolder = FSO.GetFolder(folderPath)
     
    Find_Subfolder = ""
    For Each FSsubfolder In FSfolder.SubFolders
        If UCase(FSsubfolder.Name) = UCase(subfolderName) Then
            Find_Subfolder = FSsubfolder.path & "\"
        Else
            Find_Subfolder = Find_Subfolder(FSsubfolder.path, subfolderName)
        End If
        If Find_Subfolder <> "" Then Exit For
    Next
    
End Function

Thanks all and Best regards!!!
 

Some videos you may like

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
6,977
Office Version
  1. 365
Platform
  1. Windows
How about doing both at once

VBA Code:
Name sourceFolder & oldFileName As foundDestinationFolder & newFileName
 

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
6,977
Office Version
  1. 365
Platform
  1. Windows
It works - I tested it
Post your amended code in full
 

Nguyen Anh Dung

Board Regular
Joined
Feb 28, 2020
Messages
133
Office Version
  1. 2016
Platform
  1. Windows

ADVERTISEMENT

How about doing both at once

VBA Code:
Name sourceFolder & oldFileName As foundDestinationFolder & newFileName

i change code run ok!!!
Code:
NewFileName = Mid(fileName, InStr(fileName, "GS"), Len(fileName))
Name sourceFolder & fileName As foundDestinationFolder & NewFileName
Thanks you and best regards!!!
 

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
6,977
Office Version
  1. 365
Platform
  1. Windows
Please start a new thread for your new question
 

Watch MrExcel Video

Forum statistics

Threads
1,127,499
Messages
5,625,138
Members
416,075
Latest member
TechJosh

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
Top