Name (statement)

Syntax

Name oldfile$ As newfile$

Description

Renames a file.

Comments

Each parameter must specify a single filename. Wildcard characters such as * and ? are not allowed.

Some platforms allow naming of files to different directories on the same physical disk volume. For example, the following rename will work under Windows:

  Name "c:\samples\mydoc.txt" As "c:\backup\doc\mydoc.bak"

You cannot rename files across physical disk volumes. For example, the following will error under Windows:

  Name "c:\samples\mydoc.txt" As "a:\mydoc.bak" 'This will error!

To rename a file to a different physical disk, you must first copy the file, then erase the original:

  FileCopy "c:\samples\mydoc.txt","a:\mydoc.bak" 'Make a copy
  Kill "c:\samples\mydoc.txt"             'Delete the original

Example

This example creates a file called test.dat and then renames it to test2.dat.

Sub Main()
  oldfile$ = "test.dat"
  newfile$ = "test2.dat"

  On Error Resume Next
  If FileExists(oldfile$) Then
    Name oldfile$ As newfile$
    If Err <> 0 Then
      msg1 = "The following error occurred: " & Error(Err)
    Else
      msg1 = "'" & oldfile$ & "' was renamed to '" & newfile$ & "'"
    End If

  Else
    Open oldfile$ For Output As #1
    Close
    Name oldfile$ As newfile$
    If Err <> 0 Then
      msg1 = "'" & oldfile$ & "' not created. The following error occurred: " & Error(Err)
    Else
      msg1 = "'" & oldfile$ & "' was created and renamed to '" & newfile$ & "'"
    End If
  End If
  MsgBox msg1
End Sub

See Also

Kill (statement), FileCopy (statement).

More information

N