Return to VBA Code Examples

Move Files with VBA FileSystemObject (MoveFile)

This tutorial will demonstrate how to use the MoveFile method of the FileSystemObject.

Move Files with VBA FileSystemObject

The MoveFile method moves one or more files from one location to another.

Set VBA Reference

First, when using FileSystemObject, you may have to set a reference to the VB script run-time library: open the Visual Basic Editor (ALT + F11), select Tools > References from the drop-down menu and tick the check-box of ‘Microsoft Scripting Runtime’.

vba references window

FileSystemObject

Second, you must create the FileSystemObject:

Dim FSO As New FileSystemObject
Set FSO = CreateObject("Scripting.FileSystemObject") 

Now you have access to MoveFile, and the other FileSystemObject Methods.

Move One File

To move a single file, you can use the simple syntax of FSO.MoveFile( source, destination ).

FSO.MoveFile "C:\Src\TestFile.txt", "C:\Dst\ModTestFile.txt"

As mentioned above, first you need to create the FileSystemObject:

Sub FSOMoveFile()
    Dim FSO As New FileSystemObject
    Set FSO = CreateObject("Scripting.FileSystemObject") 

    FSO.MoveFile "C:\Src\TestFile.txt", "C:\Dst\ModTestFile.txt"

End Sub

Move Multiple Files

You can move multiple files with same name parts:

FSO.MoveFile "C:\Src\TestFile*.txt", "C:\Dst\"

Or you can move multiple files with the same extension:

FSO.MoveFile "C:\Src\ *.xlsx", "C:\Dst\"

Or simply all files from a folder:

FSO.MoveFile "C:\Src\*", "C:\Dst\"

Notice, here we make use of the * wildcard character.

Instead of using the * wildcard, you can move all files in a folder using a For Each loop.

Sub FSOMoveAllFiles ()
    Dim FSO As New FileSystemObject 
    Dim FromPath As String
    Dim ToPath As String
    Dim FileInFromFolder As Object

    FromPath = "C:\Src\"   
    ToPath = "C:\Dst\"  
   
    Set FSO = CreateObject("Scripting.FileSystemObject")

    For Each FileInFromFolder In FSO.GetFolder(FromPath).Files
    	FileInFromFolder.Move ToPath
    Next FileInFromFolder

End Sub

Move File to a New Folder

You can move the file(s) into a newly created folder as well. To do so, add the command

MkDir "C:\Dst\"

before declaring the destination path.

Sub FSOMoveAllFiles ()
    Dim FSO As New FileSystemObject 
    Dim FromPath As String
    Dim ToPath As String
    Dim FileInFromFolder As Object

    FromPath = "C:\Src\"  
    MkDir "C:\Dst\"
    ToPath = "C:\Dst\" 
    
    Set FSO = CreateObject("Scripting.FileSystemObject")

    For Each FileInFromFolder In FSO.GetFolder(FromPath).Files
    	FileInFromFolder.Move ToPath
    Next FileInFromFolder

End Sub

VBA Coding Made Easy

Stop searching for VBA code online. Learn more about AutoMacro - A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users! automacro

Learn More!!

Move Folders

You can use the analogue method MoveFolder for moving folders.

Sub FSOMoveFolder()
    Dim FSO As New FileSystemObject
    Set FSO = CreateObject("Scripting.FileSystemObject") 

    FSO.MoveFolder "C:\OldFolder", "C:\Dst\NewFolder"

End Sub