mercredi 21 novembre 2012

R  code  for listing directories/code R pour lister les repertoires

Here is a pretty R  code  for listing directories in a given directory
Voici un code R pour lister les repertoires d'un dossier donné
list.dirs <- function(path=".",recursive=FALSE) {
    get.list <- list.files(path)
    if (length(get.list) == 0) {return(NULL)} #no files or directories
    get.list.fixed <- paste(path,get.list,sep="/")
    whichonesaredir <-$isdir
    if (!any(whichonesaredir)) {return(NULL)} #no directories, only files
    dirlist <- get.list.fixed[whichonesaredir]
    finaldirlist <- dirlist
    if ((recursive & length(dirlist) > 0)) {
      for (i in 1:length(dirlist)) {
         finaldirlist <- c(finaldirlist,list.dirs(dirlist[i],recursive))
Save the code in a file (listDir.r for example) and in R console use the following commands  
source('listDir.r') #to source the file
list=list.dirs(path,rec)#path is the current path by default else the dir in path will be listed. 
#rec (TRUE or FALSE(defaulty)) indicates if the sub-directories will be listed or not

Aucun commentaire:

Enregistrer un commentaire