files auto move with same name of folder

shariq

New Member
Joined
Mar 10, 2022
Messages
30
Office Version
  1. 2010
Platform
  1. Windows
hello expert i need your help i have a thousand of file with diffrent extention like pdf jpg emails i want to a programe with auto move file in a folder for example .

my source folder
c:\users\employee.records\desktop\shariq
my destination folder
D:employee.records\desktop\back up by immad

file name
03496
11145
07896
00114
00095
with same name of file folder name like this
folder
03496
11145
07896
00114
00095

i want a programe file auto move in a same name of folder .
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Try this code:
VBA Code:
Sub CopyFiles()

'Declare variables
Dim FSO As Object
Dim SourceFolder As Object
Dim DestinationFolder As Object
Dim File As Object

'Set the source and destination folders
Set FSO = CreateObject("Scripting.FileSystemObject")
Set SourceFolder = FSO.GetFolder("c:\users\employee.records\desktop\shariq")
Set DestinationFolder = FSO.GetFolder("D:\employee.records\desktop\back up by immad")

'Loop through all the files in the source folder
For Each File In SourceFolder.Files
    'Extract the file name without the extension
    Dim FileNameWithoutExtension As String
    FileNameWithoutExtension = FSO.GetBaseName(File.Name)

    'If a folder with the same name as the file doesn't exist in the destination folder, create it
    If Not FSO.FolderExists(DestinationFolder & "\" & FileNameWithoutExtension) Then
        Set DestinationSubFolder = FSO.CreateFolder(DestinationFolder & "\" & FileNameWithoutExtension)
    Else
        'If the folder already exists, set it as the destination folder
        Set DestinationSubFolder = FSO.GetFolder(DestinationFolder & "\" & FileNameWithoutExtension)
    End If
    'Copy the file to the destination folder
    File.Copy DestinationSubFolder & "\" & File.Name
Next

End Sub
 
Upvote 0
hello swapnilk script working fine but i have a some problm please solve it i dnt not want if the folder doest exist it create i have already folder created not need to recreat and some other issue file name are like this .
03456 gondal associate
09477 massive
and folder name are like this
03456 john algo
09477 albert michel
i want just read 5 digit code not full name if the 5 digit code are match then move file into the folder hope u understand please help again.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,702
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