Export (0) Print
Expand All

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.

    Dim fileList = My.Computer.FileSystem.GetFiles( _
        My.Computer.FileSystem.SpecialDirectories.MyPictures, _
        Microsoft.VisualBasic.FileIO.SearchOption.SearchAllSubDirectories, "*.*")
    
    For Each foundFile In fileList
        My.Computer.FileSystem.MoveFile(foundFile, _
            "C:\StorageDir\" & My.Computer.FileSystem.GetFileInfo(foundFile).Name)
    Next
    

The following conditions may cause an exception:

Community Additions

ADD
Show:
© 2015 Microsoft