ProblemYou need to move a directory from one location to another. SolutionUse the My.Computer.FileSystem.MoveDirectory() method to relocate an existing directory from one place to another. DiscussionThe basic syntax of the MoveDirectory() method is: My.Computer.FileSystem.MoveDirectory( _ sourceDirectory, destDirectory) 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 destDirectory argument is also an absolute or relative path, although it may not appear hierarchically within the source directory. For example: My.Computer.FileSystem.MoveDirectory( _ "C:\WorkFiles", "C:\PlayFiles") If the destination directory does not exist, MoveDirectory( ) creates it, including any nonexistent path components between the specified root and the final directory. If all but the final directory component of the source and destination directories are the same, MoveDirectory() acts like a simple directory rename operation. The My.Computer.FileSystem.RenameDirectory() method may provide a clearer method of renaming directories within the same parent directory. The movement of the source directory is complete, moving all subordinate files and directories to any depth. Also, you can move a directory between different logical disk drives. The MoveDirectory() method creates the target directory if it does not yet exist. If the destination directory is already present, any existing files at the destination remain intact, and new files are moved in amongst them, resulting in a merged destination directory. If one of the files to be moved already exists at the destination, MoveDirectory() generates an exception. If you want it to overwrite any matching files at the destination silently, use the optional third argumentthe overwrite argumentpassing a value of TRue: My.Computer.FileSystem.MoveDirectory( _ sourceDirectory, destDirectory, True) A variation of MoveDirectory() uses a different set of arguments to control the display of on-screen prompts and status notifications during the directory move: My.Computer. FileSystem.MoveDirectory( _ sourceDirectory, destDirectory, _ showUI [, onUserCancel]) The showUI argument accepts one of the following Microsoft.VisualBasic.FileIO.UIOption enumeration values:
If you include the fourth onUserCancel argument, you provide it one of the following Microsoft.VisualBasic.UICancelOption enumeration values:
See AlsoRecipe 12.4 shows how to copy an existing directory instead of moving it. To rename a directory without moving it to another parent directory, see Recipe 12.6. |