1

Given the following code:

file-info: info? %my_file.txt

How do you compare file-info/type in a conditional like:

if file-info/type = "directory" [ ... ]
Darrell Brogdon
  • 6,843
  • 9
  • 47
  • 62

3 Answers3

4

You can do this all in one line

if 'file = get in file-info: info? %my-file 'type [
   ... is file ...
]
Graham Chiu
  • 4,856
  • 1
  • 23
  • 41
1

In R2, You may want to try:

>> dir? %my_file.txt
== false

If R3, I wouldn't suggest that because it checks if a file ends with a slash.

I'm not sure what you are trying to do, but it seems a little overcomplicated.

kealist
  • 1,669
  • 12
  • 26
  • dir? simply checks an in memory value is either of type file or url and that the value ends with /. Info? checks the actual file and doesn't require the supplied name to end in "/" even if a directory. – Peter W A Wood Aug 27 '13 at 06:13
  • Peter's comment about dir? is true of Rebol 3. In Rebol 2, dir? does the info for you. I've always used the Rebol 3 style test in Rebol 2 so as to avoid the info call on URL type "directories" (e.g FTP). – Brett Aug 27 '13 at 07:17
  • I believe Darrell is using R2, not R3 – kealist Aug 27 '13 at 14:10
  • Yes, R2. It turns out I needed `file? %my_file.txt` but that seems to return true even for directories. – Darrell Brogdon Aug 27 '13 at 16:10
  • Darrell The issue with `file? %my_file.txt` is that it is checking if it is a Rebol file value (i.e. does it start with `%`): from help: `Returns TRUE for file values.` – kealist Aug 27 '13 at 17:16
  • So technically a directory is a file value? Does it think of files in the Unix sense where everything is a file? – Darrell Brogdon Aug 27 '13 at 17:38
  • 1
    Well anything of the form `%x` is the file datatype, if that makes it clearer. File extensions don't matter. As you see in Graham's example, you can use `info?` to get more specific information. When you `read file` it will automatically evaluate whether it's a file or directory and return `binary!` or `block!` accordingly – kealist Aug 27 '13 at 17:42
  • Additionally you have `exists?` that will respond `true` if, natch, the file exists. A test then might be: `filetype: if exists? file [either dir? file ['directory]['file]]` – rgchris Aug 30 '13 at 02:33
0
if file-info/type = 'directory [ ... ]

(Never underestimate your ability to realize the answer to your own question within 30 seconds of asking it.)

Darrell Brogdon
  • 6,843
  • 9
  • 47
  • 62