FILES

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

FILES collects file and directory information of a single folder, which is placed in a double-dimensioned array. A program can access the different array elements to retrieve and use that information.
FILES is also used to collect information about a particular file or files matching a pattern.

Syntax

To check a folder

  1. files pathSpec$, arrayName$()

To check a single file

  1. files pathSpec$, fileName$, arrayName$()

Usage

File/Directory Information

Most often, the name programmers use for the arrayName$() is info$(), which is a good descriptive name. We'll use DefaultDir$ for the pathSpec$. The DefaultDir$ is the name of the directory where the program currently running in JB is saved. So, in order to gather information about the current directory, we would write these statements:

    dim info$(1, 1)
    files DefaultDir$, info$()

Info$() is dimensioned as a double-dimension array at the start of the program, or before the FILES statement is executed. The initial size of the array is not important, since the FILES statement will change the dimensions to fit the information it provides. For memory consumption considerations a size of 1 x 1 is the best to use.

Check for File Existence

To see if a file named "MyData.txt" is saved in the DefaultDir$, we would do this:

    dim info$(1, 1)
    files DefaultDir$, "MyData.txt", info$()

If the file does exist, FILES will fill the info$() array with information. So, all we have to do is check if the numerical value of info$(0,0) is greater than zero and we'll know if the file is existing:

    if val(info$(0, 0)) > 0 ' then the file exists

You can use a wild card, too. To list all the text files in the DefaultDir$, do this:

    files DefaultDir$, "*.txt", info$()

What FILES Collects

General Directory Information
info$(0, 0) a string specifying the qty of files found
info$(0, 1) a string specifying the qty of subdirectories found
info$(0, 2) the drive spec
info$(0, 3) the directory path
File Information
File information starts at info$(1, x)
info$(1, 0) the file name
info$(1, 1) the file size
info$(1, 2) the file date/time stamp
info$(1, 3) the file attributes
Subdirectory Information
Subdirectory information starts at last file number + 1
info$(n + 1, 0) the complete path of subdirectory1
info$(n + 1, 1) the name of subdirectory1

Hints

There are some very good demos and sample programs that give more in-depth explanations of FILES on the following support pages:
  1. LBPE - Using the Files statement, by alix
  2. The Files command, by StPendl (talk)
We'll keep the examples on this page more modest.

Example

Getting information

' List information about the current directory

dim info$(1, 1)

files DefaultDir$, info$()

print "No. of files:",info$(0, 0)
print "No. of subdirectories:",info$(0, 1)
print "Drive spec:",,info$(0, 2)
print "Directory path:",info$(0, 3)
print

' List information about a file in the current directory.

files DefaultDir$, "test.txt", info$()
print "File name:",, info$(1, 0)
print "File size:",, info$(1, 1)
print "Date/time stamp:", info$(1, 2)
print "Attributes:",, info$(1, 3)
end

Useful Procedures

fileExists

' Here is an example one might use to check if a sequential
' data file had already been created before attempting to
' open the file for INPUT, which would cause an error.
dim info$(1, 1)

' Provide the path and filename of the file to check for.

' This statement calls the function fileExists, wherein the
' FILES command checks for the existence of "test.txt".
if fileExists(DefaultDir$, "test.txt") then

    gosub [openFile]    ' the file exists, so do this
 else
    notice "This file does not exist."
    gosub [createFile]  ' doesn't exist, so do this
end if

' Branch to one of these labels depending on fileExists result
[openFile]
print "File exists. Please continue with data input."
wait
[createFile]
print "Create the file now."
wait

' dimension the array info$(10, 10) at beginning of program
' Since this is a user-defined function, you can name it and
' the array anything you like.
' fileExists and info$() are conventional names used most often.
function fileExists(path$, filename$)
    files path$, filename$, info$()
    fileExists = val(info$(0, 0)) ' non-zero is true
end function
end

dirExists

dim info$(0, 0) 'as for fileExists function, should be DIMmed before use

'demo to work in JB folder
print "DefaultDir$ is ";DefaultDir$ 
print
files DefaultDir$ , chr$(0), info$()
print "Number of sub-directories: "; info$(0, 1)
print "sub-directory list:"
for i = 1 to val(info$(0, 0))+val(info$(0, 1))
    print  info$(i, 0), info$(i, 1), info$(i, 2), info$(i, 3)
next
print
print "Checking for existing folder"
print dirExists(DefaultDir$, "SPRITES")
print "Checking for non-existing folder"
print dirExists(DefaultDir$, "XYZZY")

end

function dirExists(path$, dirname$)
    files path$, chr$(0), info$()   'file name is invalid by design, so we get only folders
    'and info$(0,0)) always 0
    dirExists = 0
    If Val(info$(0,1)) > 0 Then
        For i = 1 To Val(info$(0,1))
            If info$(i, 1) = dirname$ Then
                dirExists = 1
                Exit Function
            End If
        next
    end if
end function

reading file size via FILES

It works even for files >4GB

dim info$(1, 1)

filedialog "Please select (BIG) file to check", "*.*", fName$
if fName$="" then end 'user canceled

print "Size by FILES command", getFileSize(fName$)

end

function getFileSize(fileName$)
'it gets size from data provided by FILES command
'Another approach is to open file and get LOF, but it reports wrong value for files >4GB
    'get aPath$, aName$ 
    i = len(fileName$)
    'look for last "\"
    while mid$(fileName$, i, 1) <> "\" and mid$(fileName$, i, 1) <> ""
        i = i-1
    wend
    aPath$ = left$(fileName$, i)
    aName$  = mid$(fileName$, i+1)

    files  aPath$, aName$, info$()
    getFileSize = val(info$(1, 1))
end function