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

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
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.
 

enerks

New Member
Joined
Sep 20, 2006
Messages
13
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
 

enerks

New Member
Joined
Sep 20, 2006
Messages
13
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
 

Forum statistics

Threads
1,144,155
Messages
5,722,818
Members
422,460
Latest member
VBA_Noob01

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
Top