Visual Basic Language Reference  

Rename Function

Renames a disk file, directory, or folder.

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

Parameters

OldPath
Required. String expression that specifies the existing file name and location. OldPath may include the directory or folder, and drive, of the file.
NewPath
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.

Exceptions/Errors

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.

Remarks

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.

Example

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.

OldName = "C:\MYDIR\OLDFILE"
NewName = "C:\YOURDIR\NEWFILE"
Rename(OldName, NewName)   ' Move and rename file.

See Also

Kill Function | ArgumentException | FileNotFoundException | IOException