IT Services & Technology Solution Services

IT Services YittBox
Create an account and receive a discount code for any future services!
Get Discount Code Now
MOVE A FILE FROM ONE DIRECTORY TO ANOTHER Question
                                        How can I move a file from one directory to another?
                                    
  • 1

  • 1

Answers (1)

admin September 29, 2019
Here is a possible solution:
 '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_MoveFile Error")
        End If
        FSO_MoveFile = 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_MoveFile Error")
        End If
        FSO_MoveFile = 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_MoveFile Error")
        End If
        FSO_MoveFile = 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_MoveFile = 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 occured" & 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 Occured!"
    Resume Error_Handler_Exit
End Function
 *
 *
 *
 

Leave a Reply

Your email address will not be published. Required fields are marked

 *
 *
 *
 
 
Let’s Work together

Start your Project

Loading