This documentation is archived and is not being maintained.

How to: Move the Contents of a Directory in Visual Basic 

Use the My.Computer.FileSystem.GetFiles Method to get the list of files in a folder, and use the My.Computer.FileSystem.MoveFile Method to move files between directories.

If the target structure does not exist when MoveFile is called, it will be created.

To move the contents of a directory

  • Use the GetFiles method to get the list of files in the directory, followed by the MoveFile method, supplying the source file and the directory to which to move it. This example moves all the files in the My Documents directory to the directory named StorageDir.

    For Each foundFile As String In My.Computer.FileSystem.GetFiles( _
        My.Computer.FileSystem.SpecialDirectories.MyDocuments, _
        FileIO.SearchOption.SearchAllSubDirectories, "*.*")
    
        My.Computer.FileSystem.MoveFile(foundFile, "C:\StorageDir")
    Next
    
    

Robust Programming

The following conditions may cause an exception:

See Also

Show: