This documentation is archived and is not being maintained.

Rename Function

Renames a disk file, directory, or folder.

Public Sub Rename( _
   ByVal OldPath As String, _
   ByVal NewPath As String _


Required. String expression that specifies the existing file name and location. OldPath may include the directory or folder, and drive, of the file.
Required. String expression that specifies the new file name and location. NewPath may include directory or folder, and drive of the destination location. The file name specified by NewPath can't already exist.


Exception type Error number Condition
ArgumentException 5 Pathname is invalid .
FileNotFoundException 53 OldPath file does not exist.
IOException 58 NewPath file already exists.
IOException 75 Access is invalid.
IOException 74 Cannot rename to different device.


The Rename function renames a file and moves it to a different directory or folder, if necessary. The Rename function can move a file across drives, but it can only rename an existing directory or folder when both NewPath and OldPath are located on the same drive. Name cannot create a new file, directory, or folder.

Using the Rename function on an open file produces an error. You must close an open file before renaming it. Rename arguments cannot include multiple-character (*) and single-character (?) wildcards.


This example uses the Rename function to rename a file. For purposes of this example, assume that the directories or folders that are specified already exist.

Dim OldName, NewName As String
OldName = "OLDFILE"
NewName = "NEWFILE" ' Define file names.
Rename(OldName, NewName)   ' Rename file.

Rename(OldName, NewName)   ' Move and rename file.

Smart Device Developer Notes

This function is not supported.


Namespace: Microsoft.VisualBasic

Module: FileSystem

Assembly: Microsoft Visual Basic .NET Runtime (in MicrosoftVisualBasic.dll)

See Also

Kill Function | ArgumentException Class | FileNotFoundException Class | IOException Class