Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell from a path string whether it is a file or a directory

Tags:

dart

dart-io

When I have a path as string what is the simplest way to get information whether the path points to a file or a directory.

like image 364
Günter Zöchbauer Avatar asked Oct 22 '25 03:10

Günter Zöchbauer


1 Answers

import 'dart:io' as io;

void main() {
  print(io.Directory.current);
  String path = '../console/readline';

  var f = new io.File(path);
  print('exists: ${f.existsSync()}');
  print('file: ${f.statSync().type == io.FileSystemEntityType.FILE}');
  print('directory: ${f.statSync().type == io.FileSystemEntityType.DIRECTORY}');
  print('link: ${f.statSync().type == io.FileSystemEntityType.LINK}');
  print('not found: ${f.statSync().type == io.FileSystemEntityType.NOT_FOUND}');
}

prints for an existing directory

Directory: '/home/user/dart/playground/bin/io/get_entry_type_from_path'
exists: false
file: false
directory: true
link: false
not found: false
like image 110
Günter Zöchbauer Avatar answered Oct 26 '25 06:10

Günter Zöchbauer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!