NAME..AS

From Liberty BASIC Family
Jump to navigation Jump to search
Supported in Just BASIC Supported in Liberty BASIC Not supported in Liberty BASIC 5 Not supported in Run BASIC

Description

This command is used to rename and move files and folders.
The destination must not exist or you receive a hard error.

Syntax

Rename a file

  1. name fileName$1 as fileName$2

Move a file

  1. name path\fileName$ as newPath\fileName$
NOTE: The file can be renamed when moved.

Hints

' Rename a file
    name "JanTots.dat" as "FebTots.dat"
    ' or...
    name "JanTots.dat" as "JanTots.bak"

' Move a file
    name "JanTots.dat" as "backup\JanTots.bak"

Example

' To make a backup of a file and move it to a backup folder.
' Assumes "backup" exists as a sub-folder of the current folder

    ' Open file to read
    open "JanTots.dat" for input as #1

    ' Open file for backup
    open "JanTots.bak" for output as #2

    ' Read file contents
    txt$ = input$(#1, lof(#1))

    ' Write contents to backup file
    print #2, txt$

    ' Close the files
    close #1
    close #2

    notice chr$(13);"Backup file successfully created";chr$(13);_
        "Will now move file to backup folder..."


    ' Move backup file to backup folder
    ' Check if file exists
    on error goto [ohoh]

    name "JanTots.bak" as "backup\JanTots.bak"
    notice "Backup complete."
    end

    ' Error handler
    [ohoh]
    if Err = 58 then print "File already exists."
    print "Program ending."
    end

Useful Procedures

' Place a useful function or sub using this keyword here