Luke Hankins
Luke Hankins

Reputation: 596

Get list of files, without subdirectories

I want to get the list of files in a directory, but drop any subdirectories completely. list.files has the option include.dirs but forces it to be TRUE when recursive is FALSE.

I dont want files in subdirectories, and I dont want the names of the subdirectories. I want to be able to call:

list.files(recursive = F, include.dirs = F)

OS is Windows 7.1

Upvotes: 0

Views: 109

Answers (2)

Gregor Thomas
Gregor Thomas

Reputation: 145775

How about this?

list_files_only = function(...) {
    all_files = list.files(...)
    dirs = list.dirs(..., recursive = FALSE, full.names = FALSE)
    setdiff(all_files, dirs)
}

Works for the current working directory just fine, thanks to BenBarnes, should be able to pass through a path arg or other args.

Upvotes: 2

Richie Cotton
Richie Cotton

Reputation: 121067

Using assertive:

library(assertive)
files <- dir()
files[!is_dir(files)]

Upvotes: 1

Related Questions