move files to folder

JAVEDR

Board Regular
Joined
Sep 17, 2019
Messages
79
Office Version
  1. 2019
Platform
  1. Windows
  2. Mobile
  3. Web
Hello,

Able to create folder and move files from below code but if folder exist unable to move that file-


From Folder (Col A)Move to Folder (Col B)
\\abc\folder1\\\abc\Main1\
\\abc\folder2\\\abc\Main1\
\\abc\folder3\\\abc\Main2\
\\abc\folder4\\\abc\Main2\
\\abc\folder5\\\abc\Main2\

VBA Code:
Sub Move_Files_To_NewFolder()
'http://www.rondebruin.nl/folder.htm


'This example move all Excel files from FromPath to ToPath.
'Note: It will create the folder ToPath for you


Dim FSO As Object
Dim FromPath As String, ToPath As String
Dim FileExt As String, FNames As String
Dim LR As Long, i As Long


LR = Cells(Rows.Count, 2).End(xlUp).Row


For i = 2 To LR
    FromPath = Range("A" & i).Value
    ToPath = Range("B" & i).Value
    FileExt = "*.*" '<< Change / You can use *.* for all files or *.doc* for word files
    If Right(FromPath, 1) <> "" Then FromPath = FromPath & ""
    FNames = Dir(FromPath & FileExt)
    If Len(FNames) = 0 Then MsgBox "No files in " & FromPath
    Set FSO = CreateObject("scripting.filesystemobject")
    FSO.CreateFolder (ToPath)
    FSO.MoveFile Source:=FromPath & FileExt, Destination:=ToPath
Next

End Sub
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Hi,
if the folder already exists, no need to create a folder. So change code like this.
VBA Code:
If Dir(ToPath, vbDirectory) = "" Then FSO.CreateFolder (ToPath)
 
Upvote 0
Solution
Can be done with:

VBA Code:
Name oldPath as newPath
 
Upvote 0
Hi,
if the folder already exists, no need to create a folder. So change code like this.
VBA Code:
If Dir(ToPath, vbDirectory) = "" Then FSO.CreateFolder (ToPath)
thanks sir for your reply, where should I add or change above code can you please guide.
 
Upvote 0
Hi,
if the folder already exists, no need to create a folder. So change code like this.
VBA Code:
If Dir(ToPath, vbDirectory) = "" Then FSO.CreateFolder (ToPath)
Thank you sir its working 🤝🤘✌️
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,912
Members
452,366
Latest member
TePunaBloke

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