ProblemYou need to copy or move an existing file from one location to another. SolutionUse the My.Computer.FileSystem. MoveFile() method to move a file from its current location to another place in the file system. Use the related CopyFile() method to copy the file instead of moving it. The basic syntax is: ' ---- To move the file. My.Computer.FileSystem.MoveFile( _ sourceFile, destFile[, overwriteFlag]) ' ---- To copy the file. My.Computer.FileSystem.CopyFile( _ sourceFile, destFile[, overwriteFlag]) Because destFile is a filename and not a directory name, you can effectively rename the file at the same time you move or copy it. When moving the file, you can keep the file in the same directory and just give it a new name, although using the RenameFile() method would be clearer. The optional overwriteFlag is a Boolean that indicates whether any existing file at the target should be replaced silently by the source file. It defaults to False. DiscussionA variation of both MoveFile() and CopyFile() uses a different set of arguments to control the display of on-screen prompts and status notifications during the move or copy: ' ----- MoveFile( ) syntax. My.Computer. FileSystem.MoveFile( _ sourceFile, destFile, _ showUI [, onUserCancel]) ' ----- CopyFile( ) syntax. My.Computer.FileSystem.CopyFile( _ sourceFile, destFile, _ 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 AlsoRecipes 12.4 and 12.5 show you how to move and copy whole directories instead of just files. |