Name
The Name statement renames the disk file specified in the OldFileName parameter, to the name specified in the NewFileName parameter.
The required parameter OldFileName must be valid existing file name, may contain a path structure, and may contain a drive letter.
The NewFileName parameter must be a string or expression that can represent a valid DOS file name value, may contain a relative path structure, and may contain a drive letter. The NewFileName parameter must be limited to less than 128 characters.
The Name statement uses the file system relative to the current directory. If no path structure is provided, the NewFileName file is expected to be in the current directory. If no drive is specified, the Name statement expects the file to be on the current drive.
Using Name, you can move a file from one directory or folder to another. If the path in NewFileName exists and is different from the path in OldFileName, the Name statement moves the file to the new directory or folder and renames the file, if necessary. If NewFileName and OldFileName have different paths and the same file name, Name moves the file to the new location and leaves the file name unchanged.
Name does not support the use of multiple-character ( * ) and single-character (?) wildcards to specify multiple files.
The Name statement does not work on a currently open file. You must close an open file before renaming it.
Note: The file system keeps track of the current drive, and the current directory of every drive. Use the CurDir statement to determine the current directory. The current drive letter can be extracted from the Left character returned in the CurDir statement.
Syntax
NameOldFileNameNewFileName
OldFileName:
A string or expression that can represent a valid file name, and may include a DOS path structure including directory or folder names, and a drive letter.
NewFileName:
A string or expression that can represent a valid file name, and may include a DOS path structure including directory or folder names, and a drive letter.
Related Functions
Close | FileCopy | FreeFile | Kill | Open
Example
Dim strNewFileName As String
Dim strOldFileName As String
strOldFileName = "c:\temp\oldfile.txt"
strNewFileName = "newfile.txt"
ChDir "\" ' change to root dir on current drive
Name strOldFileName strNewFileName ' moves file to root dir and renames it
ChDir strPath ' change back to previous path