Move Files to Master Folder Based on the Contract no e.g T1782, T98882

taimoor07

New Member
Joined
Apr 16, 2022
Messages
6
Office Version
  1. 2016
Platform
  1. Windows
Hi All. Any kind of help would be really appreciated. I have below VBA project which moves files from one folder to Master folder based on the specific identification number which is "T" and then numbers. For example. T98 Contract, T12984 Contract, T 92 Lease. below is the sample of file names,

T1525 FULLY SIGNED LEASE_9263pdf
T1525_FULLY SIGNED LEASE_9263pdf
T 1525 TEMP RENEWAL_8233pdf
eT920_Principal Lease Agreement_10542pdf
eT920_RENEWAL AGREEMENT_452TIF


The current code moves the file based on initial 13 character however i want to change it to move the files based on the contract number starting from T.

I think i need to only change the StrDestFolder Code. I tried to change the line to below but its not working.

" strDestFolder = strMasterFolder & "\" & LEFT(MID(objMyFile.Name,FIND(""T"",objMyFile.Name),FIND(""_"",objMyFile.Name)-FIND(""T"",objMyFile.Name)),6)"



Option Explicit
Sub MoveFiles()

'This Macro will move the files from SourcePath Folder to MasterFolderPath.

Dim setting_sh As Worksheet
Set setting_sh = ThisWorkbook.Sheets("Macro")

Dim SourceFolderPath As String
Dim MasterFolderPath As String


Dim objFSO As Object
Dim objMyFolder As Object
Dim objMyFile As Object
Dim strSourceFolder As String
Dim strDestFolder As String
Dim strMasterFolder As String

Application.ScreenUpdating = False

strSourceFolder = setting_sh.Range("C2").Value
strMasterFolder = setting_sh.Range("C3").Value

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objMyFolder = objFSO.GetFolder(strSourceFolder)

For Each objMyFile In objMyFolder.Files

strDestFolder = strMasterFolder & "\" & Trim(Left(objMyFile.Name, 13))



If Len(Dir(strDestFolder, vbDirectory)) = 0 Then
MkDir strDestFolder
End If
'Copy the *.pdf file to the 'strDestFolder' directory
FileCopy strSourceFolder & "\" & objMyFile.Name, strDestFolder & "\" & objMyFile.Name
'Delete the *.pdf (copying the file and then deleting it from its original location is the same as moving it)
Kill strSourceFolder & "\" & objMyFile.Name

Next objMyFile

Set objFSO = Nothing
Set objMyFolder = Nothing

Application.ScreenUpdating = True

MsgBox (" All the files have been successfully moved from Source Folder to Master Folder ")

End Sub
 

Attachments

  • Move Files.PNG
    Move Files.PNG
    10.2 KB · Views: 8

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Cross post

 
Upvote 0
Cross post

Hi. Thank you for your reply. I am trying to get help from both of the communities as i need help urgently.
 
Upvote 0
Welcome to the MrExcel Message Board!

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

If you have posted the question at more places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0
Hi All. Any kind of help would be really appreciated. I have below VBA project which moves files from one folder to Master folder based on the specific identification number which is "T" and then numbers. For example. T98 Contract, T12984 Contract, T 92 Lease. below is the sample of file names,

T1525 FULLY SIGNED LEASE_9263pdf
T1525_FULLY SIGNED LEASE_9263pdf
T 1525 TEMP RENEWAL_8233pdf
eT920_Principal Lease Agreement_10542pdf
eT920_RENEWAL AGREEMENT_452TIF


The current code moves the file based on initial 13 character however i want to change it to move the files based on the contract number starting from T.

I think i need to only change the StrDestFolder Code. I tried to change the line to below but its not working.

" strDestFolder = strMasterFolder & "\" & LEFT(MID(objMyFile.Name,FIND(""T"",objMyFile.Name),FIND(""_"",objMyFile.Name)-FIND(""T"",objMyFile.Name)),6)"



Option Explicit
Sub MoveFiles()

'This Macro will move the files from SourcePath Folder to MasterFolderPath.

Dim setting_sh As Worksheet
Set setting_sh = ThisWorkbook.Sheets("Macro")

Dim SourceFolderPath As String
Dim MasterFolderPath As String


Dim objFSO As Object
Dim objMyFolder As Object
Dim objMyFile As Object
Dim strSourceFolder As String
Dim strDestFolder As String
Dim strMasterFolder As String

Application.ScreenUpdating = False

strSourceFolder = setting_sh.Range("C2").Value
strMasterFolder = setting_sh.Range("C3").Value

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objMyFolder = objFSO.GetFolder(strSourceFolder)

For Each objMyFile In objMyFolder.Files

strDestFolder = strMasterFolder & "\" & Trim(Left(objMyFile.Name, 13))


If Len(Dir(strDestFolder, vbDirectory)) = 0 Then
MkDir strDestFolder
End If
'Copy the *.pdf file to the 'strDestFolder' directory
FileCopy strSourceFolder & "\" & objMyFile.Name, strDestFolder & "\" & objMyFile.Name
'Delete the *.pdf (copying the file and then deleting it from its original location is the same as moving it)
Kill strSourceFolder & "\" & objMyFile.Name

Next objMyFile

Set objFSO = Nothing
Set objMyFolder = Nothing

Application.ScreenUpdating = True

MsgBox (" All the files have been successfully moved from Source Folder to Master Folder ")

End Sub
Are you setting the path in the excel sheet?
Are all files needing to be moved in on parent folder for processing?
I don’t understand you file name structure.
From the sounds of it you would like to use the first 5 from the left? Or a mix between the file name?
Can you please give a screen shot of your folder structure to and from folder paths.
Do you want a folder create by lease number T12345 then move coinciding file to folder?
Is there multiple files housed into a master parent folder is the main question.
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,194
Members
449,072
Latest member
DW Draft

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