Here is a function to move a file from one folder to another.
'---------------------------------------------------------------------------------------
' Procedure : FSO_File_Move
' Author : Daniel Pineault, CARDA Consultants Inc.
' Based off of an original Sub (moveCurrent) by Hans Vogelaar
' Website : http://www.cardaconsultants.com
' Purpose : Move a file from one directory to another
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
' (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-sa/4.0/
' Req'd Refs: Uses Late Binding, so none required
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sFile : File with extension to move
' sPathSource : Path where the file is currently housed (Source path)
' sPathDest : Path where you would like to move the file to (Destination path)
' bAutomaticOverwrite : If the file already exists in the Destination folder, should
' it automatically be overwritten?
' bDisplayErrMsg: Whether or not the function should display errors or silently fail
'
' Usage:
' ~~~~~~
' FSO_File_Move("vbs.vbs", "C:\Test\From\", "C:\Test\To\")
' FSO_File_Move("vbs.vbs", "C:\Test\From\", "C:\Test\To\", False, False)
'
' Revision History:
' Rev Date(yyyy/mm/dd) Description
' **************************************************************************************
' 1 2015-11-28 Initial Release
' 2 2019-01-07 Added function header
' Updated error handler
' Added bDisplayErrMsg input variable
' 3 2021-11-28 Name bug fix thanks to Blair Baker
'---------------------------------------------------------------------------------------
Public Function FSO_File_Move(ByVal sFile As String, _
ByVal sPathSource As String, _
ByVal sPathDest As String, _
Optional bAutomaticOverwrite As Boolean = True, _
Optional bDisplayErrMsg As Boolean = True) As Boolean
On Error GoTo Error_Handler
Dim oFSO As Object
Dim sSourceFile As String
Dim sDestFile As String
Dim sMsg As String
Dim answer As Integer
'Ensure properly formatted paths were supplied, adjust as req'd
If Right(sPathSource, 1) <> "\" Then sPathSource = sPathSource & "\"
If Right(sPathDest, 1) <> "\" Then sPathDest = sPathDest & "\"
'Build fully qualified path\filenames for the move process
sSourceFile = sPathSource & sFile
sDestFile = sPathDest & sFile
Set oFSO = CreateObject("Scripting.FileSystemObject")
'Ensure the supplied paths (Source/Destination) actually exist
If oFSO.FolderExists(sPathSource) = False Then
If bDisplayErrMsg = True Then
sMsg = "Source path '" & sPathSource & "' does not exist."
Debug.Print sMsg
Call MsgBox(sMsg, vbInformation Or vbOKOnly, "FSO_File_Move Error")
End If
FSO_File_Move = False
GoTo Error_Handler_Exit
End If
If oFSO.FolderExists(sPathDest) = False Then
If bDisplayErrMsg = True Then
sMsg = "Destination path '" & sPathDest & "' does not exist."
Debug.Print sMsg
Call MsgBox(sMsg, vbInformation Or vbOKOnly, "FSO_File_Move Error")
End If
FSO_File_Move = False
GoTo Error_Handler_Exit
End If
'Ensure the File to be copied actually exists
If oFSO.FileExists(sSourceFile) = False Then
If bDisplayErrMsg = True Then
sMsg = "Source file '" & sSourceFile & "' does not exist."
Debug.Print sMsg
Call MsgBox(sMsg, vbInformation Or vbOKOnly, "FSO_File_Move Error")
End If
FSO_File_Move = False
GoTo Error_Handler_Exit
End If
'Check to see if the file already exist in the Destination folder
If oFSO.FileExists(sDestFile) = True Then
If bAutomaticOverwrite = False Then
GoTo Error_Handler_Exit
Else
Kill sDestFile
End If
End If
oFSO.MoveFile sSourceFile, sDestFile
FSO_File_Move = True
Error_Handler_Exit:
On Error Resume Next
If Not oFSO Is Nothing Then Set oFSO = Nothing
Exit Function
Error_Handler:
MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Source: FSO_File_Move" & vbCrLf & _
"Error Description: " & Err.Description & _
Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
, vbOKOnly + vbCritical, "An Error has Occurred!"
Resume Error_Handler_Exit
End Function
Thank you very much .. Those were very helpful ..
Thank you for posting this and all of your hard work !!!
If I have a query called q_Move_PDFs, with sPathSource and sPathDest shown in the query results, can you tell me how I would do this… I am not very experienced in VBA :/
Thanks again for the code !
There is a minor bug in your code:
You need to change “FSO_MoveFile” to “FSO_File_Move”
Thanks for providing this!
Thank you for pointing this out. I’ve updated the code.
That work absolutely prefect… I change some code to match my needs, but otherwise perfect!!! Thanks so much.