Move Files with VBA FileSystemObject (MoveFile)

Written by

Editorial Team

Reviewed by

Steve Rynearson

Last updated on August 10, 2022

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

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

 

vba-free-addin

VBA Code Examples Add-in

Easily access all of the code examples found on our site.

Simply navigate to the menu, click, and the code will be inserted directly into your module. .xlam add-in.

(No installation required!)

Free Download

Return to VBA Code Examples