|
FILEINFO$() and FINDFILE$() File Related Functions |
The FILEINFO$ function parses a file specification and returns either a full file
specification or specific file specification fields.
FILEINFO$(str_expr1 [, str_expr2 [, str_expr3]])
str_expr1 - The file specification to be parsed. If no file specification is given,
the device and directory you are currently running from are returned.str_expr2 - A list of field names, separated by commas, which are to be returned. The field names are:
CONTENTS - File contentsDEVICE - Drive nameDIRECTORY - Directory nameNAME - File nameTYPE - Type or extension nameLOCATION - Device and directory namesBACKUP_DATE - Last file backup dateCREATION_DATE - Date file was createdEXPIRATION_DATE - File expiration dateREVISION_DATE - Date file was last modifiedREVISION - The number of times a given file has been revised (given that the underlying OS supports this)SIZE - The size of the file in bytesALL or "" - Full file specificationstr_expr3 - The default file specification. This parameter is optional.
print fileinfo$('x.y', 'ALL')
print fileinfo$('', 'ALL')
print fileinfo$('@config.ini', 'ALL') // Relative to program directory
end
Output:
c:\Sheerpower\x.y
c:\Sheerpower
(The '@' example will output the full path to config.ini in your program's directory)
x$ = 'Sheerpower:samples\client'
print fileinfo$(x$, 'ALL', '.ars')
end
Output:
c:\Sheerpower\samples\client.ars
print fileinfo$('Sheerpower:samples\client', 'all', '.ars')
print fileinfo$('Sheerpower:samples\client', 'location')
print fileinfo$('Sheerpower:samples\client', 'location, name')
print fileinfo$('Sheerpower:samples\client.ars')
end
Output:
c:\Sheerpower\samples\client.ars
c:\Sheerpower\samples\
c:\Sheerpower\samples\client
c:\Sheerpower\samples\client.ars
The CONTENTS option of FILEINFO$ returns the entire
contents of the file 'some_file.xxx' into all_of_file$. If the file cannot be found,
then it returns a null string. A zero-length file will also return a null string.
all_of_file$ = fileinfo$('some_file.xxx', 'contents')
all_of_file$ = fileinfo$('Sheerpower:Sheerpower.ini', 'contents')
print all_of_file$
end
Output:
[license]
LicenseKey=xxx-xxx-xxx-xxx
Username=non-commercial
EmailAddress=non-commercial
sourcefile$ = 'source.xxx'
destfile$ = 'destination.xxx'
contents$ = fileinfo$(sourcefile$, 'contents')
open file dest_ch: name destfile$, access output, unformatted
print #dest_ch: contents$
close #dest_ch
end
The FINDFILE$ function searches for files that match a
given name or pattern and returns the full file specification of
the first match found.
If no matching file exists, the function returns a null string
('').
*.spsrc).
1 (the first match).
'' if no match exists.
Problem: Many languages require complex directory APIs or external libraries just to locate files matching a pattern.
Solution: FINDFILE$ provides a simple,
built-in way to search for files using familiar wildcard patterns.
Efficiency: File lookup is handled internally by the runtime, avoiding manual directory traversal and reducing code complexity.
Takeaway: File discovery is a one-line operation in Sheerpower, making common tasks both simple and reliable.
Example output:
Use the optional index parameter to iterate through all matches.
Example input:
Example output:
You can combine FINDFILE$ with other functions such as
FILEINFO$ to process each file.
FINDFILE$ calls can be nested, provided the inner call
uses only the file specification (no index parameter).
Problem: File-processing pipelines often require multiple lookup steps, which can become verbose and error-prone.
Solution: Nested FINDFILE$ calls allow
composing file searches directly in expressions.
Efficiency: Reduces intermediate variables and keeps file-selection logic compact and readable.
Takeaway: File discovery can be composed just like string or numeric expressions.
|
Hide Description
|
|
|
Enter or modify the code below, and then click on RUN |
|
Looking for the full power of Sheerpower?
Check out the Sheerpower website. Free to download. Free to use. |