Rename file after move to subfolder

Nguyen Anh Dung

Board Regular
Joined
Feb 28, 2020
Messages
180
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!!!
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
How about doing both at once

VBA Code:
Name sourceFolder & oldFileName As foundDestinationFolder & newFileName
 
Upvote 0
It works - I tested it
Post your amended code in full
 
Upvote 0
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!!!
 
Upvote 0
Please start a new thread for your new question
 
Upvote 0

Forum statistics

Threads
1,214,599
Messages
6,120,453
Members
448,967
Latest member
grijken

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