FileExists


Release:

 • 4.6 •  5.0 •  5.1 •  5.2 •  5.3 •  5.4 •  6.0 •  6.1 •  6.2 •  6.3 •  6.4 •  6.5 •   •  6.6 •  7.0 •  7.1 •  7.2

New to Analytica 7.2, requires Analytica Enterprise or better.

FileExists( filepath, files, folders )

Returns True if the file or folder «filepath» exists, or False if it does not.

There are other ways to test whether a file exists, using RunConsoleProcess or system COM objects, but those other methods may be unavailable for security reasons when you run your model on the public Analytica Cloud Platform (ACP) server, whereas this one is available and secure on ACP.

Before Analytica 7.2, the usual way to write this test was Size(FileSystemListing(filepath, 'Filename')) > 0. FileExists replaces that idiom. It asks about one file instead of listing a folder, it distinguishes files from folders directly, and it works on a custom file system provider path.

Parameters

  • «filepath»: The file or folder to test for. When this is a relative path, it is interpreted relative to the CurrentDataFolder(). Wildcards are not accepted -- use FileSystemListing when you want to know whether anything matches a pattern.
  • «files»: (optional, default True) Whether an existing file that is not a folder counts as existing. Set it to False to ask only whether «filepath» is an existing folder.
  • «folders»: (optional, default True) Whether an existing folder counts as existing. Set it to False to ask only whether «filepath» is an existing file and not a folder.

Since both flags default to True, plain FileExists(f) is True for a folder as well as for a file.

Return value

True or False. It array abstracts over its parameters, so an array of paths in «filepath» returns an array of Booleans with the same indexes.

Certain conditions are reported as an error rather than as False, because in those cases Analytica cannot actually tell whether the file is there. These are: no read permission on the containing folder (see Use in ACP), and, for a provider path, a store that cannot be reached or that rejects the credentials (see Custom file system providers).

Examples

  • FileExists( "C:\MyModels\main.ana" )True when that file is present.
  • FileExists( "results.csv" ) → tests for "results.csv" in the CurrentDataFolder().
  • FileExists( "C:\MyModels", files:False )True only when "C:\MyModels" is an existing folder.
  • FileExists( "D:\Feeds\hourly.csv", folders:False )True only when it is an existing file and not a folder.

Read a file when it is there, and fall back to a default when it isn't:

If FileExists(f) Then ReadTextFile(f) Else ""

Count how many of the expected input files have arrived:

Sum( FileExists(ExpectedFiles), ExpectedFiles )

where ExpectedFiles is an index (or an array) of file paths. This is the array-abstracting form -- one call tests them all.

Warn, rather than fail, when a data folder is missing:

If FileExists(dataDir, files:False) Then FileSystemListing(dataDir) Else Error( f"The data folder {dataDir} is not there." )

Custom file system providers

«filepath» can be a custom file system provider path, such as repo://models/main.ana:

FileExists( "repo://models/main.ana" )
FileExists( "repo://data", files:False )

For a provider path, FileExists asks the store service. An object stored at that exact key is treated as a file. A folder is recognized from the store's listing of the parent folder, since a store usually exposes a folder only as a listing prefix and answers "not found" when asked about it directly -- so FileExists("repo://data") is True for a folder that contains something. Name matching in a store is case-sensitive, unlike the local Windows file system.

When the store cannot be reached, or rejects the credentials, FileExists reports the underlying error rather than guessing an answer. It also reports an error when no provider is registered for the scheme, or when your edition does not include the feature. See Diagnostics for the wording of each.

Use in ACP

You can test only for files in folders where you have read permission, which is generally your model's own project folder and its subfolders, the standard Analytica libraries folders, and the Analytica example models folders. Asking about a file outside those raises an access-denied error rather than returning False, so that FileExists cannot be used to discover what a folder you have no access to contains.

See Also

Comments
Loading comments...