Comments on: Defining DIR?

Carl Sassenrath, CTO
REBOL Technologies
6-May-2010 14:17 GMT

Article #0319
Main page || Index || Prior Article [0318] || Next Article [0320] || 13 Comments || Send feedback

The dir? function is intended to be an easy-to-use way to check if a file or file path is a directory.

For example, when processing a list of files we often write:

foreach file file-list [
    unless dir? file [
        process-file file

The problem is that dir? does two things:

  1. It checks if the filename ends with a slash (/).
  2. It checks if the file is a directory on local storage.

This behavior is problematic because the filenames being checked may have nothing to do with local storage. In the example above, the list of files may have been fetched from a server or a database, and they're not related to what's on disk.

So, we need to define dir? a little better. It should do one of the above operations, but not both.

Personally, I'd prefer the first behavior, because that's what I use the most. I never do the second, because I'm using filenames that were obtained from a directory, so they contain the slash in their names. However, that's not compatible with R2.

The second behavior could be supported with a refinement, like dir?/query which would perform the actual disk query.



Maxim Olivier-Adlhoch
6-May-2010 11:31:42
isn't DIR? already just checking the path?

I thought we had resolved that one a few years ago...

in A96:

>> dir? %/path/does/not/exist/
== true

PLEASE do not re-introduce the old R2 behaviour its useless.

it got replaced by exists? returning 'dir 'file or none

>> exists? %/path/does/not/exist/
== none
Carl Sassenrath
6-May-2010 11:38:41
Hi Maxim, no it does both... but you may not have noticed because it did the path string check first.

However, bug #602 shows the problem. A file %foo could be a directory on disk, in which case DIR? returns true.

My proposal is to simplify, and make it just check the path string and not go to disk.

Maxim Olivier-Adlhoch
6-May-2010 13:46:33
oh... well, the current behavior is even less meaningull then ;-)

yep, slash the file access, and I don't see any need to add the /QUERY refinement.

we've got EXISTS? for that, and its a sexy function which you can even use directly with SWITCH or CASE.

Carl Sassenrath
6-May-2010 13:54:49
Sounds good. A99 will change it.

If anyone has opposing opinions, post them really soon.

6-May-2010 16:57:55
Add a refinement for a comparator if needed (or use generator for dir? closures) Comparator as in a sort function.
7-May-2010 1:35:02
If the purpose of dir? is to check for a trailing /, then it's misnamed. Perhaps rename it to tail/? or something.
Brian Hawley
7-May-2010 4:42:42
Adding a refinement for a comparator is a little heavy for what would be a one-line mezzanine - what do you mean Maarten, in REBOL code? And tail/? isn't a valid word Graham, it's a path, so "or something" would be better.

Having dir? just work on the filename, and exists? check the file works for me. We need both operations on different occasions, whatever we end up calling dir?.

7-May-2010 9:16:42
Just to say I'm relieved at this decision.

I would also like to mention that in the beginning, I was confused by file? being a type check and dir? checking storage. Having dir? act as a subtype check is much more elegant.

7-May-2010 19:53:05
Having dir? check for a trailing / means it is true for a string such as "sfsf/" but false for "c:\windows\" ... so I think a better name is needed.
Brian Hawley
8-May-2010 5:52:55
The dir? function checks for both / and \, so your concern is handled, Graham.
25-Jan-2021 7:05:58
As I'm just reviewing it..

moving `dir?` to mezz caused many new issues like #79. The reason is, that `dir?` call is used in low-level `make-port*` function, which is responsible to open a directory OR file port. It is needed in each action call to file! type! As the mezz `dir?` checks only dir separator char, existing directory without a slash is not recognized causing use of wrong port type.

Naturally `exists?` should be used instead, but it's currently just a wrapper around `query` action, and so cannot be used, because `query` also depends on `make-port*` (it would be an infinite recursion).

25-Jan-2021 14:03:29
I've added `dir?/check` for check if the file is a directory on local storage.

15-Feb-2021 7:45:18
I've added `dir?/check` for check if the file is a directory on local storage.

Post a Comment:

You can post a comment here. Keep it on-topic.


Blog id:



 Note: HTML tags allowed for: b i u li ol ul font span div a p br pre tt blockquote

This is a technical blog related to the above topic. We reserve the right to remove comments that are off-topic, irrelevant links, advertisements, spams, personal attacks, politics, religion, etc.

Updated 16-Jul-2024 - Edit - Copyright REBOL Technologies -