View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Robert Bruce[_2_] Robert Bruce[_2_] is offline
external usenet poster
 
Posts: 108
Default Move all files from one directory to another

Roedd <<Prema wedi ysgrifennu:

I would like to know what code I can use in my macro to move all .xls
files from P:\new-journals to P:\processed-journals.


This requires a reference to the Microsoft Scripting Runtime library.

Sub test()
MoveAllFiles "P:\new-journals", "P:\processed-journals"
End Sub

Sub MoveAllFiles(Source As String, Dest As String)

Dim objFS As New FileSystemObject
Dim objSource As Folder
Dim objDest As Folder
Dim objFile As File

On Error GoTo MoveAllFiles_Err:

If objFS.FolderExists(Source) Then

If objFS.FolderExists(Dest) Then

Set objSource = objFS.GetFolder(Source)
Set objDest = objFS.GetFolder(Dest)

For Each objFile In objSource.Files
objFile.Move (objDest.Path & _
Application.PathSeparator & objFile.Name)

Next
Else
Err.Raise vbObjectError + 1001, "", _
"Folder " & Source & " not found."
End If

Else

Err.Raise vbObjectError + 1001, "", _
"Folder " & Source & " not found."

End If

Exit Sub
MoveAllFiles_Err:
With Err
.Raise .Number, "[MoveAllFiles]" & .Source, _
.Description, .HelpFile, .HelpContext
End With
End Sub

--
Rob

http://www.asta51.dsl.pipex.com/webcam/

This message is copyright Robert Bruce and intended
for distribution only via NNTP.
Dissemination via third party Web forums with the
exception of Google Groups and Microsoft Communities
is strictly prohibited and may result in legal action.