Created
June 21, 2015 00:30
-
-
Save jdesilvio/219a6eaa585d1368f951 to your computer and use it in GitHub Desktop.
R - Get contents and information for all files in a directory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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