Skip to content

Instantly share code, notes, and snippets.

@jdesilvio
Created June 21, 2015 00:30
Show Gist options
  • Save jdesilvio/219a6eaa585d1368f951 to your computer and use it in GitHub Desktop.
Save jdesilvio/219a6eaa585d1368f951 to your computer and use it in GitHub Desktop.
R - Get contents and information for all files in a directory
#list all files from a folder
getFolderContent = function(path) {
pathNow = getwd()
setwd(path)
files = list.files()
setwd(pathNow)
files
}
#create helper function to paste path and file, then get info
fileInfo = function(file, path=path, ...) {
file.info(paste(path, file, sep='/'))
}
#get info for all files in a folder
getFolderInfo = function(path) {
files = as.data.frame(getFolderContent(path))
df = as.data.frame(lapply(files, function(x) fileInfo(x, path)))
names(df) = c("size", "isDir", "mode", "modified", "created", "accessed", "isExe")
df$path = row.names(df)
df$file = unlist(llply(df$path, function(x) strsplit(x, '/')[[1]][9]))
df
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment