Sdk: File.exists on directories

Created on 3 May 2012  路  10Comments  路  Source: dart-lang/sdk

It looks to me as File.exists claims that directories doesn't exist. I know a directory isn't a file, but I think it would be handy to have one operation to determine if a file name refers to something that exists or not.

area-library library-io

Most helpful comment

We've landed some changes, so now:

new File(path).existsSync() == true, iff path is file, or link to file.
new Directory(path).existsSync() == true, iff path is directory, or link to directory.
new Link(path).existsSync() == true, iff path link.

To query just once, you can use FileSystemEntity.typeSync(path) != FileSystemEntityType.NOT_FOUND.


_Added Fixed label._

All 10 comments

I agree this is a useful operation, but the current behavior is also useful (and being used in pub). I'm up for adding this as a new operation, but I wouldn't want to change the current semantics of File.exists.

_This comment was originally written by @seaneagan_


if there were a separate Path object on Files and Directories as has been suggested, then that could have an exists methods with the semantics requested here.

_This comment was originally written by @seaneagan_


Or have a common base interface for File and Directory, and have it's exists method return true if anything exists at all:

interface FileNode {
  String path;
  void fullPath(void callback(String fullPath));
  String fullPathSync();
  void create(void callback());
  void createSync();
  void delete(void callback());
  void deleteSync();
  void exists(void callback(bool));
  bool existsSync();
}

interface File extends FileNode //...
interface Directory extends FileNode //...

_This comment was originally written by @ahmetaa_


Perhaps a Path class as in Java and C# can resolve this?

_Added this to the Later milestone._

We've landed some changes, so now:

new File(path).existsSync() == true, iff path is file, or link to file.
new Directory(path).existsSync() == true, iff path is directory, or link to directory.
new Link(path).existsSync() == true, iff path link.

To query just once, you can use FileSystemEntity.typeSync(path) != FileSystemEntityType.NOT_FOUND.


_Added Fixed label._

I'm not convinced this is a good solution. I think you have made the common operation hard, and the uncommon operation easy.

I don't think it would be a bad thing to add bool FileSystemEntity.exists(path).
I'm not sure what it should report on broken links, though. Probably true.

_Removed Area-IO label._
_Added Area-Library, Library-IO labels._

FileSystemEntityType.NOT_FOUND is deprecated and FileSystemEntityType.notFound should be used.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nex3 picture nex3  路  3Comments

DartBot picture DartBot  路  3Comments

matanlurey picture matanlurey  路  3Comments

xster picture xster  路  3Comments

bergwerf picture bergwerf  路  3Comments