getFileName static method

String? getFileName(
  1. String filePath
)

This method gets the file name from a path.

getFileName("/path/to/file/my_file.jpg") == "my_file.jpg"
getFileName("/path/to/file/my_file") == null
getFileName("/path/to/file/.jpg") == null

Returns null if filePath is not formatted in a valid way.

Hint:

  • filePath must include a file extension.
  • If the path contains separators, there must be characters between the last separator and the dot in the file extension.

Note: This method will not work on hidden files. It will view them as a file extension since hidden files like ".gitconfig" look like file extensions to this program and not valid file names.

Implementation

static String? getFileName(String filePath) {
  String? baseName = getBaseName(filePath);
  String? fileExt = tryParseFileExt(filePath);

  if (baseName == null || fileExt == null) return null;

  return baseName + fileExt;
}