81

I thought this would be pretty straight-forward, but can't seem to get this. I have a File file and it has a path file.path which spits out something like /storage/emulated/0/Android/data/my_app/files/Pictures/ca04f332.png but I can't seem to find anything to get just ca04f332.png.

Jus10
  • 14,519
  • 21
  • 52
  • 77
  • Every answer I could find anywhere, is some variation of parsing the full path. This is brutally inefficient, and it matters a lot if you are going to walk a large and deep tree of files, for example to find a filename that matches a certain regex. Even Python would be faster at this. Really? We must parse the whole path? – freeideas Jul 20 '22 at 20:31

6 Answers6

167

You can use the basename function from the dart path library:

import 'package:path/path.dart';

File file = new File("/dir1/dir2/file.ext");
String basename = basename(file.path);
# file.ext
Ely Dantas
  • 705
  • 11
  • 23
jspcal
  • 50,847
  • 7
  • 72
  • 76
  • 7
    Don't forgot to import path.dart => import 'package:path/path.dart'; – Abhishek Kumar Apr 05 '20 at 19:04
  • under some reasons am getting a file name without extension, so how can i get the file extension. /storage/emulated/0/Android/data/my_app/files/Pictures/ca04f332 – b.john Apr 19 '20 at 13:30
  • 3
    or can use the basename function without extension: `String basename = basenameWithoutExtension(file.path);` – loonix Jul 10 '20 at 07:52
  • 1
    @b.john get the path `String path = file.path;` and file name with extension `fileName = path.split('/').last;` – Abdul Qadir Aug 26 '21 at 09:08
  • Just a note to my issue, If you use **BLoC** and using `part` and `part of` for file organizer, adding `import 'package:path/path.dart';` will gave error to the `context.read().yourMethod();` code line – Muhammad Faisal Jan 18 '23 at 03:53
  • recommended because path is different in cross platform – Jetwiz Mar 18 '23 at 08:18
70
File file = new File("/storage/emulated/0/Android/data/my_app/files/Pictures/ca04f332.png"); 
String fileName = file.path.split('/').last;

print(fileName);

output = ca04f332.png

0917237
  • 861
  • 7
  • 9
11

Since Dart Version 2.6 has been announced and it's available for flutter version 1.12 and higher, You can use extension methods. It will provide a more readable and global solution to this problem.

file_extensions.dart :

import 'dart:io';

extension FileExtention on FileSystemEntity{
  String get name {
    return this?.path?.split(Platform.pathSeparator)?.last;
  }
}

and name getter is added to all the file objects. You can simply just call name on any file.

main() {
  File file = new File("/dev/dart/work/hello/app.dart");
  print(file.name);
}

Read the document for more information.

Note: Since extension is a new feature, it's not fully integrated into IDEs yet and it may not be recognized automatically. You have to import your extension manually wherever you need that. Just make sure the extension file is imported:

import 'package:<your_extention_path>/file_extentions.dart';
Community
  • 1
  • 1
Saman Salehi
  • 1,995
  • 1
  • 22
  • 36
  • For the sake of total completeness, should the this?.path?.split(...) call be modified to use pathSeparator in cases of running on Windows? Or does Dart / Flutter always work with a "/" character at the language level? (I think Java does that, too). – ChrisH Jul 27 '20 at 23:00
  • 2
    @ChrisH Actually by the time that I wrote this answer, I didn't know that `Platform.pathSeparator` is exist. Yes, It would be a better approach if you use `Platform.pathSeparator` instead of `'/'` – Saman Salehi Jul 28 '20 at 13:04
  • @ChrisH feel free to suggest an edit on my answer and I will accept it – Saman Salehi Jul 28 '20 at 13:09
  • if anyone want to do it on `filePath` as a `String` then use this `extension FileExtention on String { String get name { return this?.split("/")?.last; } } ` – hewa jalal Apr 09 '21 at 09:05
5

Direct way:

File file = File('/foo/bar/baz/my_image.jpg');
String fileName = file.path.split(Platform.pathSeparator).last; // my_image.jpg

Using an extension:

1. Create an extension:

extension FileEx on File {
  String get name => path.split(Platform.pathSeparator).last;
}

2. Usage:

File file = File('/foo/bar/baz/my_image.jpg');
String fileName = file.name; // my_image.jpg
CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
1

Easy way to get name or any other file handling operations.I recommend to use this plugin : https://pub.dev/packages/file_support

main() {
  String filename= FileSupport().getFileNameWithoutExtension(<File Object>);
}
Parmeet Singh
  • 21
  • 1
  • 1
0

You cane use extension method No need to use any package

Create Method like this

extension FileNameExtension on File {
  String getFileName() {
    String fileName = path.split('/').last;
    return fileName;
  }
}

implement

File file = File("yourpath/example.pdf");
file.getFileName() // result =>  example.pdf
Jewel Rana
  • 2,397
  • 1
  • 19
  • 28