exists function

bool exists(
  1. String path, {
  2. bool followLinks = true,
})

File system existence and type checking utilities.

This module provides utility functions for checking file system entity existence, types, and environment conditions. Check if a file system path exists.

Parameters:

  • path: The path to check (cannot be empty)
  • followLinks: If true, follows symbolic links (defaults to true)

Returns true if the path exists, false otherwise.

Throws ArgumentError if path is empty.

Example:

if (exists('/path/to/file.txt')) {
  print('File exists');
}

// Check without following symbolic links
if (exists('/path/to/symlink', followLinks: false)) {
  print('Symlink exists');
}

Implementation

/// Check if a file system path exists.
///
/// Parameters:
/// - [path]: The path to check (cannot be empty)
/// - [followLinks]: If true, follows symbolic links (defaults to true)
///
/// Returns true if the path exists, false otherwise.
///
/// Throws [ArgumentError] if [path] is empty.
///
/// Example:
/// ```dart
/// if (exists('/path/to/file.txt')) {
///   print('File exists');
/// }
///
/// // Check without following symbolic links
/// if (exists('/path/to/symlink', followLinks: false)) {
///   print('Symlink exists');
/// }
/// ```
bool exists(String path, {bool followLinks = true}) {
  if (path.isEmpty) {
    throw ArgumentError('path must not be empty.');
  }

  final exists = FileSystemEntity.typeSync(path, followLinks: followLinks) !=
      FileSystemEntityType.notFound;

  return exists;
}