Middlemarch,
My intent by showing Daniel's article was only that he had info indicating fso.movefolder did not work.
I have found a few links that show fso.movefolder - none works?? All give error
https://analystcave.com/vba-filesyst...ba-movefolder/
https://www.devguru.com/content/tech...ovefolder.html
Other people had issues/couldn't get MoveFolder to work
see this stackoverflow thread
The code below did move all files from my source to my destination folder.
**Note: This was a few files from "c:\test" to "c:\dest".
I separately created c:\dest so commented the MkDir within the code.
The files were moved to dest folder and removed from test folder -- and no errors.
I realize this does not handle subfolders, but
Code:
Sub FSOMoveAllFiles()
Dim FSO As New FileSystemObject
Dim FromPath As String
Dim ToPath As String
Dim FileInFromFolder As Object
FromPath = "C:\test\"
' MkDir "C:\Dst\"
ToPath = "C:\Dest\"
Set FSO = CreateObject("Scripting.FileSystemObject")
For Each FileInFromFolder In FSO.GetFolder(FromPath).files
Debug.Print "Moving " & FileInFromFolder.name
FileInFromFolder.Move ToPath
Next FileInFromFolder
End Sub
UPDATE: After running the code above, I went back to Daniel's sample code to copy then delete folder.
I changed the source and destination folders as below. The files were moved to test and dest was deleted.
No errors!! Which is confusing???
Code:
' Procedure : MoveFolder
' Author : CARDA Consultants Inc.
' Website : http://www.cardaconsultants.com
' Purpose : Move a folder
' Better version of the FSO's MoveFolder method which is basically a "rename"
' method, hence it only works if the source and destination reside on
' the same volume (same as move.exe under WinXP) and typically returns
' a permission denied error.
' Copyright : The following may be altered and reused as you wish so long as the
' copyright notice is left unchanged (including Author, Website and
' Copyright). It may not be sold/resold or reposted on other sites (links
' back to this site are allowed).
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sFolderSource Folder to move
' sFolderDestination Folder to move the folder to
' bOverWriteFiles Whether to overwrite file(s) if the folder already exists
'
' Usage Example:
' ~~~~~~~~~~~~~~~~
' MoveFolder("C:\Temp", "D:\Development\New")
'
' Revision History:
' Rev Date(yyyy/mm/dd) Description
' **************************************************************************************
' 1 2010-Nov-14 Initial Release
'---------------------------------------------------------------------------------------
Function MoveFolder(sFolderSource As String, sFolderDestination As String, _
bOverWriteFiles As Boolean) As Boolean
On Error GoTo Error_Handler
Dim fs As Object
MoveFolder = False
Set fs = CreateObject("Scripting.FileSystemObject")
fs.CopyFolder sFolderSource, sFolderDestination, bOverWriteFiles
fs.DeleteFolder sFolderSource, True
MoveFolder = True
Error_Handler_Exit:
On Error Resume Next
Set fs = Nothing
Exit Function
Error_Handler:
If Err.Number = 76 Then
MsgBox "The 'Source Folder' could not be found to make a copy of.", _
vbCritical, "Unable to Find the Specified Folder"
Else
MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Source: MoveFolder" & vbCrLf & _
"Error Description: " & Err.Description, _
vbCritical, "An Error has Occurred!"
End If
Resume Error_Handler_Exit
End Function
Switched source and destination for this attempt.
Code:
Sub danielMove()
Dim src As Folder
Dim dest As Folder
MoveFolder "c:\dest", "c:\test", True
End Sub
Further: I then recreated c:\dest, switched the source and destinaton folders and ran Daniel's code again.
All files moved to c:Test and c
est was deleted --No errors???
Then, to test with a subfolder to confirm that my findings were real.
I then added a subfolder DestSub1 to c:\Dest and added a few files to the subfolder. I again separately created c:\Test.
Then reversed the source and destination folder names and ran Daniel's code again.
Code:
Sub danielMove()
Dim src As Folder
Dim dest As Folder
MoveFolder "c:\dest", "c:\test", True
End Sub
And it worked without errors. Moved folder and subfolder and all files to c:\test and c:\test\destsub1
****My only suggestion is to try Daniel's code again, and let us know your result.