Q(uick)BASIC Statement: NAME
Quick View
NAME
A statement that uses DOS to change the name of a disk file or directory
Worth knowing
Useful and cross-version information about the programming environments of QBasic and QuickBasic.
Syntax
- NAME oldspec$ AS newspec$
Description/Parameter(s)
oldspec$, newspec$ | The name of an existing file and the new name for the file. Each name may include a path. |
Example
INPUT "Old Name: "; OldFN$
INPUT "New Name: "; NewFN$
NAME OldFN$ AS NewFN$
See also:
Syntax
- NAME oldfilename AS newfilename
Description/Parameter(s)
The NAME statement is similar to the DOS RENAME command. NAME can move a file from one directory to another but cannot move a directory.
The arguments oldfilename and newfilename are string expressions each of which contains a file or directory name and an optional path. If the path in newfilename is different from the path in oldfilename, the NAME statement changes the pathname as well as renames the file as indicated.
A file named oldfilename must exist and the newfilename must not be in use. Both files must be on the same drive. Using NAME with different drive designations in the old and new file names produces an error message that reads "Rename across disks."
After a NAME statement, the file exists on the same disk, in the same disk space, but with the new name.
Using NAME on an open file causes a run-time error message that reads "File already open." You must close an open file before renaming it.
Tip: | oldfilename and/or newfilename may contain a pathname. If the path in oldfilename is different than the path in newfilename, the NAME statement moves the file and renames it. Both files must be on the same drive. |
You can also use NAME to move a file from one directory to another. You can move README.DOC to the directory MYDIR with the following statement:
|
Example
This example shows how to use NAME to rename your README.DOC file.
CLS ' Clear screen
'Change README.DOC to READMINE.DOC
NAME "README.DOC" AS "READMINE.DOC"
FILES ' Display the files in your directory to confirm name change
PRINT
PRINT "README.DOC is now called READMINE.DOC"
PRINT "Press any key to continue"
DO
LOOP WHILE INKEY$ = ""
'Change name back to README.DOC and confirm by displaying files
NAME "READMINE.DOC" AS "README.DOC"
FILES
PRINT
PRINT "README.DOC is back to its original name"
Syntax
- NAME oldfilespec$ AS newfilespec$
Description/Parameter(s)
Usage Notes
- The NAME statement is similar to the DOS RENAME command. NAME can move a file from one directory to another but cannot move a directory.
- The file oldfilespec$ must exist and the file newfilespec$ must not be in use. Both files must be on the same drive. If you use NAME with different drive designations in the old and new filespecs, BASIC generates the error message, "Rename across disks."
- If you use NAME on an open file, BASIC generates the run-time error message, "File already open." You must close an open file before renaming it.
Example
This example uses the NAME statement to rename your README.DOC file.
CLS 'Clear screen.
'Change README.DOC to READMINE.DOC.
NAME "README.DOC" AS "READMINE.DOC"
FILES 'Display the files in your directory to confirm name change.
PRINT
PRINT "README.DOC is now called READMINE.DOC"
PRINT "Press any key to continue"
DO
LOOP WHILE INKEY$ = ""
'Change name back to README.DOC and confirm by displaying files.
NAME "READMINE.DOC" AS "README.DOC"
FILES
PRINT
PRINT "README.DOC is back to its original name"