if the file name match with folder name file auto move into the folder

Status
Not open for further replies.

shariq

New Member
Joined
Mar 10, 2022
Messages
30
Office Version
  1. 2010
Platform
  1. Windows
Public Sub Move_Files_To_Matching_Folder()

Dim sourceFolder As String, destMainFolder As String, destSubfolder As String
Dim FSO As Object
Dim FSfile As Object
Dim FSsourceFolder As Object

sourceFolder = Environ$("USERPROFILE") & "\Downloads\"
destMainFolder = Environ$("USERPROFILE") & "\Pictures\"

If Right(sourceFolder, 1) <> "\" Then sourceFolder = sourceFolder & "\"
If Right(destMainFolder, 1) <> "\" Then destMainFolder = destMainFolder & "\"

Set FSO = CreateObject("Scripting.FileSystemObject")

Set FSsourceFolder = FSO.GetFolder(sourceFolder)
For Each FSfile In FSsourceFolder.Files
destSubfolder = destMainFolder & Left(FSfile.Name, InStrRev(FSfile.Name, ".") - 1) & "\"
If FSO.FolderExists(destSubfolder) Then
If FSO.FileExists(destSubfolder & FSfile.Name) Then FSO.DeleteFile destSubfolder & FSfile.Name, True
FSfile.Move destSubfolder
End If
Next

End Sub

this script give me john this is perfctly working but i have a some problm i want to change the desination folder how i can chage beacuse i'll try like this destMainFolder = Environ$("USERPROFILE") & "\Employee file\" but this not working any body help me
 

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)
I answered in your original thread -


Admins - please close this thread.
 
Upvote 0
i really thank full to you john your solve my problem thanks alot all vba script are prefctly worrking many many thankful to you and mr excel borad
 
Upvote 0
In future, please do not post the same question multiple times. Per Forum Rules (#12), posts of a duplicate nature will be locked or deleted.
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,223,885
Messages
6,175,183
Members
452,615
Latest member
bogeys2birdies

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