Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a better way to find out of a file or directory exists?

Tags:

dart

dart-io

This seems a bit cumbersome

var fileExists = 
    new File(path).existsSync() || 
    new Directory(path).existsSync() || 
    new Link(path).existsSync() 

Is there a shorter or better way?

like image 972
Günter Zöchbauer Avatar asked Aug 22 '16 08:08

Günter Zöchbauer


People also ask

How do you check if a path is directory or file?

To check if the path you have is a file or directory, import os module and use isfile() method to check if it is a file, and isdir() method to check if it is a directory.

What method of file is used to test if a file or directory exists?

File. exists() is used to check whether a file or a directory exists or not. This method returns true if the file or directory specified by the abstract path name exists and false if it does not exist.

How do you determine whether a file exists?

To check if a file exists, you pass the file path to the exists() function from the os. path standard library. If the file exists, the exists() function returns True .

How do you check if it is a file or directory in C?

The isDir() function is used to check a given file is a directory or not.


1 Answers

A shorter way is

import 'dart:io';

FileSystemEntity.typeSync(path) != FileSystemEntityType.notFound

See also https://github.com/dart-lang/sdk/issues/2883#issuecomment-108317456

like image 79
Günter Zöchbauer Avatar answered Oct 11 '22 14:10

Günter Zöchbauer