MoveFile Method

Moves one or more files from one location to another.

Syntax

object.MoveFile ( source, destination )

Arguments:

  • Object: Required. The object is always the name of a FileSystemObject.
  • Source: Required. Character string file specification, which can include wildcard characters, for one or more files to be copied.
  • Destination: Required. Character string destination where the file or files from source are to be copied. Wildcard characters are not allowed.

Remarks

Wildcards are only allowed at the end of the source path. If you have passed any incorrect path, an error will pop up saying either the source files or target directories do not exist.

The following example illustrates the use of the MoveFile method:

Example 1:

Function MoveSingleFile(source, destination)
    Dim fso
    Set fso = CreateObject("Scripting.FileSystemObject")
    fso.MoveFile source, destination
End Function

' Usage
MoveSingleFile "C:\Path\To\Source\File.txt", "C:\Path\To\Destination\File.txt"

Example 2:

Note:

If you use a wildcard in the source path, the destination must already be an existing directory. Otherwise, you’ll get an error message saying the directory doesn’t exist.

Function MoveMultipleFiles(source, destination)
    Dim fso
    Set fso = CreateObject("Scripting.FileSystemObject")
    fso.MoveFile source, destination
End Function

' Usage
MoveMultipleFiles "C:\Path\To\Source\*.txt", "C:\Path\To\Destination\" 'Here Destination Directory must exists