How to: Move the Contents of a Directory in Visual Basic
Use the GetFiles method to get the list of files in a folder, and use the 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
Robust Programming
The following conditions may cause an exception:
The path is not valid for one of the following reasons: it is a zero-length string, it contains only white space, it contains invalid characters, or it is a device path (starts with \\.\) (ArgumentException).
The path is not valid because it is Nothing (ArgumentNullException).
directory does not exist (DirectoryNotFoundException).
directory points to an existing file (IOException).
The path exceeds the system-defined maximum length (PathTooLongException).
A file or directory name in the path contains a colon (:) or is in an invalid format (NotSupportedException).
The user lacks necessary permissions to view the path (SecurityException).
The user lacks necessary permissions (UnauthorizedAccessException).
See Also
Tasks
How to: Rename a File in Visual Basic
How to: Move a File in Visual Basic
How to: Create a Copy of a File in a Different Directory in Visual Basic
How to: Parse File Paths in Visual Basic
How to: Move a Directory in Visual Basic
Troubleshooting: Reading from and Writing to Text Files (Visual Basic)
How to: Move a Collection of Files in Visual Basic