PDA

View Full Version : Move files from different folders to single folder and delete the original files



Raja_1983
06-07-2020, 10:04 AM
Hello,

I have more than 100 different folders in a common folder and inside which there are PDF are stored.

The list of file names stored inside each folder is known and name of the folders are also known.

I want to get back PDF files and delete original files inside the folders.

Need your help to VBA code for the above scenario.I hereby attach the excel file for your reference.

26797

mana
06-14-2020, 02:50 AM
Sub test()
Dim r As Range
Dim k As Long
Dim src As String, dst As String, fn As String

Set r = Cells(1).CurrentRegion

For k = 2 To r.Rows.Count
fn = r.Cells(k, 2).Value
src = r.Cells(k, 1).Value & "\" & fn
dst = r.Cells(k, 3).Value & "\" & fn

If (Dir(src) <> "") * (Dir(dst) = "") Then
Name src As dst
r.Cells(k, 4).Value = "Moved"
End If
Next

End Sub




マナ

Raja_1983
06-14-2020, 03:08 AM
Dear Mana,
Thanks for the support.
If the source folder have different files((file names),then it is working fine.
But if one or more source folders having same files(file names),then it is not working
Pls help..

Raja_1983
06-14-2020, 03:15 AM
Dear Mana,
Sorry now the code is working well
I create seperate folder in destination to retrive the files from source folders
Now files having same filename also moved to destination
Thanks for your exreme support.

snb
06-14-2020, 03:54 AM
@mana

No need for an object variable: an array suffices.
No further slow interaction with the sheet required.
.


Sub test()
sn = Cells(1).CurrentRegion

For j = 2 To ubound(sn)
sn(j,4)=sn(j,1) & "\" & sn(j,2)
sn(j,3)=sn(j,3) & "\" & sn(j,2)
If (Dir(sn(j,1)) <> "") * (Dir(sn(j,3)) = "") Then Name sn(j,1) As sn(j,3)
Next

Cells(1).CurrentRegion = sn
End Sub

mana
06-14-2020, 04:52 AM
Thanks for the useful advice.


@mana

No need for an object variable: an array suffices.
No further slow interaction with the sheet required.
.