Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

logical test if object is a directory

Tags:

r

Let's say I just created this directory and file:

dir.create("foo")
cat("bar", file="bar")

#both files exist:

file.exists("foo")
file.exists("bar")

I want to test both foo and bar to test logically if they are directories or not. If something is a directory I want to be able to put other files in it. If the item is not a directory I want to use stop but don't know of how test.

Poking around a bit got me to:

file.info("foo", "bar")
.Internal(file.info(fn <- c("foo", "bar")))

Which both give me the info I'm after but the .Internal use seems to be using a logical test of sorts for directory (meaning more efficient), but I can't figure out how. What is file.info using to test if the object is a directory?

like image 793
Tyler Rinker Avatar asked May 24 '13 16:05

Tyler Rinker


2 Answers

Maybe you want file_test?

file_test("-d", c("foo", "bar"))
# [1]  TRUE FALSE
like image 114
Josh O'Brien Avatar answered Oct 16 '22 04:10

Josh O'Brien


file.info is obviously platform dependent and uses _wstati64 on Windows and stat on Linux. See https://svn.r-project.org/R/trunk/src/main/platform.c

like image 37
eddi Avatar answered Oct 16 '22 04:10

eddi