Files

Files
You can retrieve information about files on disk with the **Files** statement. The syntax is as follows:

FILES #handle, pathStringExpr$ //- Create a file accessor object for accessing files at pathStringExpr$//

The pathStringExpr$ contains drive and path information. It can also contain file information, including wildcards. Some possible **files** statements:

code files #m, DefaultDir$ files #n, DefaultDir$ + "\*.*" files #p, "C:\RBP\*.txt" code


 * FILE ACCESSOR methods**


 * 1) handle HASANSWER //- Return non-zero if the file accessor has at least one resulting row.//
 * 2) handle ROWCOUNT //- Return the number of rows returned.//
 * 3) handle NEXTFILE$ //- Advance to the next row and return a comma delimited string for the next file (name, size, date, time, directory flag).//
 * 4) handle NEXTFILE$([delimExpr$]) //- Like NEXTFILE$ but you get to specify the delimiter instead of a comma.//
 * 5) handle NAME$ //- Return the name of the current file row.//
 * 6) handle SIZE //- Return the size of the current file row.//
 * 7) handle DATE$ //- Return a string containing a formatted date for the current file row.//
 * 8) handle TIME$ //- Return a string containing a formatted time for the current file row.//
 * 9) handle ISDIR //- Return non-zero if the current file row represents a directory instead of a file.//
 * 10) handle RESET //- Reset the file accessor back to the beginning so you can read through them again.//
 * 11) handle DATEFORMAT(template$) //- Set the date format using a "mmm dd, yyyy" style template$.//
 * 12) handle TIMEFORMAT(template$) //- Set the time format using a "hh:mm:ss" style template$.//
 * 13) handle ISNULL //- Returns zero (or false)//
 * 14) handle DEBUG$ //- Returns the string "Files"//

The **HASANSWER** method tells you if there is a file matching the specifications in the template string.

The **ROWCOUNT** method tells you how many files are listed. You can use that value in a for/next loop to loop through the filenames and obtain information about them.

The **NEXTFILE$** method lists the information for the next filename. If an optional delimiter is specified, that delimiter is used to separate the items. The delimiter can be more than one character in length. A default NEXTFILE$ string looks like this: cloudsbanner1.JPG,17132,2006/11/08,10:05:58,

Using this format: gives this output: cloudsbanner1.JPG / 17132 / 2006/11/08 / 10:05:58
 * 1) handle nextfile$(" / ")

The **DATEFORMAT** method allows you to change the way the file date is formatted. Here's an example:

Output: 10/27/07
 * 1) handle DATEFORMAT("mm/dd/yy")

The **TIMEFORMAT** method allows you to change the way the file time is formatted. Here's an example:

Output: 06:23:06
 * 1) handle TIMEFORMAT("hh:mm:ss")

The **DEBUG$** method tells you what an object is. For a Files object, it returns "Files".

The following demo finds all files in the DefaultDir$ in a loop. It prints information about each file.

code format="vbnet" files #f, DefaultDir$ + "\*.*"

print "hasanswer: ";#f HASANSWER print "rowcount: ";#f ROWCOUNT print " " :print " "

'set format of file date to template given
 * 1) f DATEFORMAT("mm/dd/yy")

'set format of file time to template given
 * 1) f TIMEFORMAT("hh:mm:ss")

count = #f rowcount

for i = 1 to count if #f hasanswer then

'print nextfile info in default format: 'print "nextfile: ";#f NEXTFILE$

'set the delimiter for nextfile info print "nextfile info: ";#f nextfile$("   ")

print "name: ";#f NAME$ print "size: ";#f SIZE print "date: ";#f DATE$ print "time: ";#f TIME$ print "isdir: ";#f ISDIR print " " 'blank line end if next code

The next demo looks for jpg files, and if any are found, it prints the file names.

code format="vbnet" files #g, DefaultDir$ + "\*.jpg"

if #g HASANSWER then count = #g rowcount

for i = 1 to count

if #g hasanswer then 'retrieve info for next file 'print name of file print #g NAME$ end if
 * 1) g nextfile$

next

end if wait code

Another example not requiring a rowcount and outputs file date and time from one statement. This example also uses the **IsNull method and the **Debug** method to print out the type of object, in this case it is "Files".

code format="vbnet" print "Before creation, IsNull returns:" print #f isnull files #f, "C:\rbp\*.*" while #f hasanswer fileData$ = #f name$;" - "; #f date$;" - "; #f time$ print fileData$ wend print print """";#f debug$;""" is the type of object for #f" print print "After creation, IsNull returns:" print #f isnull wait code
 * 1) f nextfile$