Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 26
Default Move all files from one directory to another

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.

Thank you
Prema
  #2   Report Post  
Posted to microsoft.public.excel.programming
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.


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 26
Default Move all files from one directory to another

Robert

Do I have to do something special to activate any reference to the Microsoft
Scripting Runtime library?

Prema

"Robert Bruce" wrote:

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.



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 108
Default Move all files from one directory to another

Roedd <<Prema wedi ysgrifennu:

Do I have to do something special to activate any reference to the
Microsoft Scripting Runtime library?


In the VBE menus:Tools | References - In the resulting dialog, scroll down
to the entry for Microsoft Scripting Runtime. Check it and then click OK.
The project now has a reference set to the library.

--
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.


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 26
Default Move all files from one directory to another

Thank you very much. It works perfectly.

Prema

"Robert Bruce" wrote:

Roedd <<Prema wedi ysgrifennu:

Do I have to do something special to activate any reference to the
Microsoft Scripting Runtime library?


In the VBE menus:Tools | References - In the resulting dialog, scroll down
to the entry for Microsoft Scripting Runtime. Check it and then click OK.
The project now has a reference set to the library.

--
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.





  #6   Report Post  
Posted to microsoft.public.excel.programming
smh smh is offline
external usenet poster
 
Posts: 1
Default Move all files from one directory to another


Rob,

First and Foremost, thank you for helping Prema with this post. I wa
looking for a similar situation and your code helped me get over th
hump.

However, your code is easier to understand and works better than mine
(i am still learning VBA).

I have a question.

How would I set up VBA to where the user can select a directory to mov
and its new destination?

Any help is GREATLY appreciated. Thank you in advance.

SM

--
sm
-----------------------------------------------------------------------
smh's Profile: http://www.excelforum.com/member.php...fo&userid=2524
View this thread: http://www.excelforum.com/showthread.php?threadid=37800

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Files in a directory? Greg B Excel Discussion (Misc queries) 5 May 11th 05 09:46 PM
move all files in a directory into another [email protected] Excel Programming 1 January 24th 05 02:53 PM
Move files from one directory to another: if they are there Michael McClellan Excel Programming 12 June 28th 04 12:42 AM
macro to move contents of directory Harvey[_3_] Excel Programming 0 February 9th 04 07:36 PM
Check if directory empty OR no of files in directory. Michael Beckinsale Excel Programming 2 December 4th 03 10:12 PM


All times are GMT +1. The time now is 09:31 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"