fileExists
Category: File Operations
Namespace: web.files.fileExists
Description
Checks whether a file exists at the specified path.
Syntax
const exists = await web.files.fileExists({
filePath: 'path/to/file.txt'
});
Parameters
- filePath (required) - File path to check (relative or absolute)
Returns
Promise<boolean> - Returns a promise that resolves with true if the file exists, false otherwise
Examples
Simple example
const exists = await web.files.fileExists({
filePath: 'config.json'
});
if (exists) {
console.log('Config file found');
} else {
console.log('Config file not found');
}
Conditional file loading
const configPath = 'settings.json';
const exists = await web.files.fileExists({ filePath: configPath });
if (exists) {
const content = await web.files.readTextFile({ filePath: configPath });
const settings = JSON.parse(content);
} else {
// Use default settings
const settings = { theme: 'light', language: 'en' };
}
Prevent overwriting
const outputPath = 'report.txt';
const exists = await web.files.fileExists({ filePath: outputPath });
if (exists) {
const overwrite = confirm('File exists. Overwrite?');
if (!overwrite) return;
}
await web.files.writeTextFile({
filePath: outputPath,
contents: reportData
});
Use Cases
- Check if configuration files exist before loading
- Verify files before attempting to read them
- Prevent accidental file overwrites
- Validate user-provided file paths
- Implement conditional file operations
Notes
- Returns
falseif the path exists but is a directory, not a file - Use
web.directory.dirExists()to check for directories - Relative paths are resolved from the application executable directory
- Does not throw errors - returns
falsefor invalid or inaccessible paths