You need to rename a directory, but it's not really a directory move because the renamed directory will stay in the same parent directory.
Use the My.Computer. FileSystem.RenameDirectory() method to change the name of an existing directory.
The basic syntax of the RenameDirectory() method is:
My.Computer.FileSystem.RenameDirectory( _ sourceDirectory, newName)
The sourceDirectory argument contains an absolute UNC-based or drive-letter-based path, or a relative path based on the current directory from the application's point of view. The newName argument includes only the new name of the final directory component; you cannot supply an absolute or relative path for this argument. The following statement is valid:
My.Computer.FileSystem.MoveDirectory( _ "C:\WorkFiles", "PlayFiles")
This statement is not:
' ----- This statement will fail. My.Computer.FileSystem.MoveDirectory( _ "C:\WorkFiles", "C:\PlayFiles")
If a directory already exists with the new name, RenameDirectory( ) generates an exception, even if that target directory is empty.
Visual Basic includes an intrinsic function, Rename( ), which can also rename directories. Its syntax is slightly different because its second argument accepts either a new non-path name or any valid path:
' ----- Both of these statements will work. Rename("C:\WorkFiles", "PlayFiles") Rename("C:\WorkFiles", "C:\PlayFiles")
The Rename( ) function also moves a directory to another existing directory tree if requested:
Rename("C:\Temp\Important\LogFiles\OldLogs", _ "C:\Temp\Archive\LogFiles")
Generally, the features exposed through the My namespace enhance features already found elsewhere. However, this is one of those times when the older feature provides a more flexible interface. Still, for consistency in new code, you will probably want to use RenameDirectory( ).
To move a directory to a different parent directory, see Recipe 12.5.