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
Wildcard characters can only be used in the last path component of the source argument. If you have passed any incorrect path, source files or destination directories does not exists error will be thrown.
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 have used wildcard in source path it is expected that destination should be existing directory. If the directory does not exists error will be thrown
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