Name Statement
Renames a disk file, directory, or folder.
Syntax
Name oldpathname As newpathname
oldpathname: Required. String expression that specifies the existing file name and location. May include directory or folder, and drive.newpathname: Required. String expression that specifies the new file name and location. May include directory or folder, and drive. Cannot specify a different drive from the one specified inoldpathname.
Remarks
- The
Namestatement renames a file and moves it to a different directory or folder, if necessary Namecan move a file across directories or folders, but botholdpathnameandnewpathnamemust be on the same drive- Using
Nameon an open file produces an error. You must close an open file before renaming it Namearguments can include relative or absolute paths- The
Namestatement can also rename directories or folders - If
newpathnamealready exists, an error occurs - Wildcard characters (* and ?) are not allowed in either
oldpathnameornewpathname
Examples
' Rename a file
Name "OLDFILE.TXT" As "NEWFILE.TXT"
' Move and rename a file
Name "C:\Data\Report.doc" As "C:\Archive\OldReport.doc"
' Rename a directory
Name "C:\OldFolder" As "C:\NewFolder"
' Move file to different directory (same drive)
Name "C:\Temp\Test.dat" As "C:\Data\Test.dat"
' Using variables
Dim oldName As String, newName As String
oldName = "File1.txt"
newName = "File2.txt"
Name oldName As newName