|
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.
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. It may also be an http:// or https:// address: see Reading a Web Address below.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.Output:
Output:
Output:
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, and it never raises an exception for a file it cannot reach.
A zero-length file also returns a null string, so to tell the two apart, test
_integer right after the call: it is 1 when the file was read and 0 when it
could not be. The SIZE and date items set _integer the same way.
Output:
The file specification may be an http:// or https:// address.
The page or file is fetched the same way OPEN FILE reads a URL, with the same
10-second default timeout, and comes back as one string. The SIZE item works on
an address too. A page that cannot be fetched, a 404 for instance, returns a null string
with _integer 0, exactly like a missing file.
Output:
When the goal is only a copy, the COPY FILE statement does it in one line,
keeps the file's revision date, and streams the bytes instead of holding the whole file in
memory: copy file sourcefile$ to destfile$. See Working with Files, Console Output, URLs, and Data.
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. |