BB___FILES

=FILES= toc

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$//

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: code format="lb" dim info$(1, 1) files DefaultDir$, info$ code 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: code format="lb" dim info$(1, 1) files DefaultDir$, "MyData.txt", info$ code 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: code format="lb" if val(info$(0, 0)) > 0 ' then the file exists code You can use a wild card, too. To list all the text files in the DefaultDir$, do this: code format="lb" files DefaultDir$, "*.txt", info$ code

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) justBASIC\wikispaces\the Files command, by StPendl

> We'll keep the examples on this page more modest.

Getting information
code format="lb" ' 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 code

fileExists
code format="lb" ' 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 code

dirExists
You might want similar function for folders: dirExists(path$, dirname$) function

reading file size via FILES
It works even for files >4GB code format="lb" dim info$(1, 1)

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

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

end

function getFileSize(fileName$) 'it gets seze 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 code