PDA

View Full Version : Solved: Moving files from different source folder to different destination folders



aloy78
08-02-2012, 11:10 PM
Dear Vb-ers,

Is there a way to move file/files from different source folders to different destination folders? I've attached a file with their respective source and respective destination folders to move to.

mancubus
08-03-2012, 01:14 AM
hi.

this an adoption from http://www.rondebruin.nl/folder.htm

test first on sample files and folders...

'Note: It will create the folder ToPath for you


Sub Move_Certain_Files_To_New_Folder()
'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, 1).End(xlUp).Row

For i = 2 To LR
FromPath = Cells("A" & i).Value
ToPath = Cells("B" & i).Value
FileExt = "*.xl*" '<< 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

aloy78
08-05-2012, 01:38 AM
Hi Mancubus,

I tried the module but it gave me an error on here:


For i = 2 To LR
FromPath = Cells("A" & i).Value ' I get an error in this section


It's highlighted in yellow

mancubus
08-05-2012, 03:01 PM
ooops...

sorry, it would be:

FromPath = Cells(i, "A").Value

or
FromPath = Range("A" & i).Value


change also ToPath accordingly...

aloy78
08-06-2012, 10:49 PM
Hi Mancubus,

Thanks. Its working now :)

mancubus
08-06-2012, 10:57 PM
you're wellcome...

but credits must be given to http://www.rondebruin.nl/folder.htm