move multiple files

enerks

New Member
Joined
Sep 20, 2006
Messages
13
I already coded what I think should do the trick but it doesn't seem to work.

Lfiles = Dir("C:\SomeFolder\")
Do Until Lfiles = ""
OldFilePath = "C:\Folder1\" & Lfiles
NewFilePath = "C:\Folder2\" & Lfiles
If Dir(NewFilePath) = Dir(OldFilePath) Then Kill (NewFilePath)
Name OldFilePath As NewFilePath
Lfiles = Dir()
Loop

It only works for the first files it find in DIR("C:\SomeFolder\") then is stops. Can someone point me in the right direction to solve this problem?
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
When you call Dir with no arguments it uses the last used pathname. I think in your case it would be NewFilePath rather than "C:\SomeFolder\".

Check out the FileSearch object in VBA Help. That will return an array of file names (FoundFiles) that you can loop around.
 
Upvote 0
Thanks Andrew,

I change the code from Lfiles = dir() to Lfiles = dir("c:\SomeFolder\")
and added if Lfiles = "" Then End

Now it works. I jus needed your hint that it uses the last used pathname!

Thanks again.

Rene
 
Upvote 0
The complete correct code was posted earlier last week:

Dim strSrcDir As String
Dim strDstDir As String
Dim I As Long

strSrcDir = "folder1"
strDstDir = "folder2"

With Application.FileSearch
.NewSearch
.LookIn = strSrcDir
.FileType = msoFileTypeAllFiles
.Execute
For I = 1 To .FoundFiles.Count
Name (.FoundFiles(I)) As strDstDir & Dir(.FoundFiles(I))
Next I
End With
 
Upvote 0

Forum statistics

Threads
1,214,792
Messages
6,121,612
Members
449,039
Latest member
Mbone Mathonsi

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