ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Move Folder Contents (https://www.excelbanter.com/excel-programming/297661-re-move-folder-contents.html)

Dave Peterson[_3_]

Move Folder Contents
 
How about:

Option Explicit
Sub testme()

Dim FinalFolderName As String
Dim CurrentFolderName As String

Dim FSO As Scripting.FileSystemObject

Dim FinalFolder As Scripting.Folder
Dim CurrentFolder As Scripting.Folder
Dim myFile As Scripting.File

FinalFolderName = "C:\Closing"
CurrentFolderName = "C:\Assets"

Set FSO = New Scripting.FileSystemObject

If FSO.FolderExists(CurrentFolderName) = False _
Or FSO.FolderExists(FinalFolderName) = False Then
MsgBox "where are they???"
Exit Sub
End If

Set CurrentFolder = FSO.GetFolder(CurrentFolderName)
Set FinalFolder = FSO.GetFolder(FinalFolderName)

For Each myFile In CurrentFolder.Files
myFile.Copy Destination:=FinalFolder.Path & "\" & myFile.Name, _
overwritefiles:=True
myFile.Delete
Next myFile

End Sub

This code requires a reference to the "Microsoft Scripting Runtime"
(tools|References inside the VBE).

If you're new to macros, you may want to read David McRitchie's intro at:
http://www.mvps.org/dmcritchie/excel/getstarted.htm

Ryan wrote:

I am trying to set up a way to move files from one folder to another. I found on this board an excellent way to move a file from one folder to another using the "Name" function, but this doesn't exactly work for my situation. I am not going to know the name of all of the files in a certain folder, I will only know the folder names. Is there a way to move all of the contents from the "Assets" file to the "Closing" file without just changing the name of the file?

Ryan


--

Dave Peterson


Dana DeLouis[_3_]

Move Folder Contents
 
Maybe just another way:

Sub Demo()
Const FromFolder As String = "D:\Assets\*.*"
Const ToFolder As String = "D:\Closing\"

On Error Resume Next
With CreateObject("Scripting.FileSystemObject")
.CopyFile FromFolder, ToFolder, True
.DeleteFile FromFolder, True
End With
End Sub

--
Dana DeLouis
Using Windows XP & Office XP
= = = = = = = = = = = = = = = = =


"Dave Peterson" wrote in message
...
How about:

Option Explicit
Sub testme()

Dim FinalFolderName As String
Dim CurrentFolderName As String

Dim FSO As Scripting.FileSystemObject

Dim FinalFolder As Scripting.Folder
Dim CurrentFolder As Scripting.Folder
Dim myFile As Scripting.File

FinalFolderName = "C:\Closing"
CurrentFolderName = "C:\Assets"

Set FSO = New Scripting.FileSystemObject

If FSO.FolderExists(CurrentFolderName) = False _
Or FSO.FolderExists(FinalFolderName) = False Then
MsgBox "where are they???"
Exit Sub
End If

Set CurrentFolder = FSO.GetFolder(CurrentFolderName)
Set FinalFolder = FSO.GetFolder(FinalFolderName)

For Each myFile In CurrentFolder.Files
myFile.Copy Destination:=FinalFolder.Path & "\" & myFile.Name, _
overwritefiles:=True
myFile.Delete
Next myFile

End Sub

This code requires a reference to the "Microsoft Scripting Runtime"
(tools|References inside the VBE).

If you're new to macros, you may want to read David McRitchie's intro at:
http://www.mvps.org/dmcritchie/excel/getstarted.htm

Ryan wrote:

I am trying to set up a way to move files from one folder to another. I

found on this board an excellent way to move a file from one folder to
another using the "Name" function, but this doesn't exactly work for my
situation. I am not going to know the name of all of the files in a certain
folder, I will only know the folder names. Is there a way to move all of
the contents from the "Assets" file to the "Closing" file without just
changing the name of the file?

Ryan


--

Dave Peterson




ryan

Move Folder Contents
 
Dana

That worked perfectly. Thank you very much

Ryan


All times are GMT +1. The time now is 03:48 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com