PDA

View Full Version : [SOLVED] Vba code in excel to move doc files from one folder to another.



Jagdev
02-25-2015, 04:23 AM
Hi Experts
I have find the below code which is moving excel files from one folder to another. I have same requirement, but the files are word docs can we use the same for it.

Sub movefile2()
oldpath = "E:\test1\"
newpath = "E:\test2\"
Fname = Dir(oldpath & "*.xls*")
Do While Fname <> ""
Name oldpath & Fname As newpath & Fname
Fname = Dir
Loop
End Sub

If yes, I have main folder with four sub-folders in it and the files are saved in all the sub-folders. Is it possible to move all the files from these sub-folders into one folder only.
Regards,
JD

p45cal
02-25-2015, 04:52 AM
I would think so; change .xls* to .doc*, run the macro 4 times changing the line oldpath = "E\test1\subfolder1" each time. Should be OK as long as there are not the same file names appearing in the different subfolders.

Jagdev
02-25-2015, 06:52 AM
Is it possible to avoid running it 4 times and the entire task gets done in a go

p45cal
02-25-2015, 07:05 AM
try:
Sub blah()
PathArray = Array("E:\test1\Subfolder1\", "E:\test1\Subfolder2\", "E:\test1\Subfolder3\", "E:\test1\Subfolder4\")
For Each oldpath In PathArray
newpath = "E:\test2\"
Fname = Dir(oldpath & "*.doc*")
Do While Fname <> ""
Name oldpath & Fname As newpath & Fname
Fname = Dir
Loop
Next oldpath
End Sub

Jagdev
02-25-2015, 11:16 PM
Hi P45cal

The code works fine! Thanks for the code.

Regards,
JD