2016-10-27 2 views
0

J'essaye de faire une boucle sur un dossier et ses sous-dossiers, et enregistrer tous les fichiers info (taille, mdate, etc) dans une table . J'ai écrit le code ci-dessous, mais cela ne fonctionne pas comme prévu, puisque je reçois 'folder/filename' comme nom de fichier et 'dossier principal' comme dossier dans chaque ligne. Quelqu'un pourrait-il m'aider? Merci.R - Essayer de faire défiler un dossier et des sous-dossiers, et sauvegarder les informations sur les fichiers dans une table

######################################################## 
######################################################## 
# Example:            # 
# The path 'c:/test' contains 1 file and 3 subfolders # 
# with 2 files in each -> 7 file in total    # 
# 
# c:/test 
#  file_000.txt 
# 
#  + subfolder1 
#     + file_001.docx 
#     + file_002.txt 
#  + subfolder2 
#     + file_003.docx 
#     + file_004.bmp 
#  + subfolder3 
#     + file_005.xlsx 
#     + file_006.txt 
######################################################## 

# Create the empty dataframe 'table' 
table = data.frame() 

# Set the working path 
path = 'c:/test' 

# List all folders and subfolders in path 
folders_list = list.dirs(path, recursive = TRUE) 

# Start looping in folders 
for(folder in folders_list){ 
    setwd(folder) 
    dir = getwd() 

    # Get files list in each folder 
    files_list = list.files(folder, recursive = TRUE) 

    # Start looping in files 
    for(file in files_list){ 

     # Get info about each file and append below the previous line in the dataframe 'table' 
     table = rbind(table, file.info(file)) 

    } 
    # Add a column withe the file name to each line in 'table' 
    table$file = row.names(file) 

    # Try to add the folder path to another column 'folder' 
    table$folder = dir 
} 
# Show 'table' 
View(table) 



############################################## 
# RESULT (head(table)): 
#       size isdir mode    mtime    ctime    atime exe folder 
# file_000.txt    0 FALSE 666 2016-10-27 17:19:40 2016-10-27 17:19:40 2016-10-27 17:19:40 no c:/test 
# subfolder1/file_001.docx 0 FALSE 666 2016-10-27 17:57:00 2016-10-27 17:57:00 2016-10-27 17:57:00 no c:/test 
# subfolder1/file_002.txt  0 FALSE 666 2016-10-27 17:18:34 2016-10-27 17:18:34 2016-10-27 17:18:34 no c:/test 
# subfolder2/file_003.docx 0 FALSE 666 2016-10-27 17:57:29 2016-10-27 17:57:29 2016-10-27 17:57:29 no c:/test 
# subfolder2/file_004.bmp  0 FALSE 666 2016-10-27 17:19:00 2016-10-27 17:19:00 2016-10-27 17:19:00 no c:/test 
# subfolder3/file_005.xlsx 8081 FALSE 666 2016-10-27 17:57:52 2016-10-27 17:57:52 2016-10-27 17:57:52 no c:/test 
############################################## 

############################################## 
# NEEDED: 
#       size isdir mode    mtime    ctime    atime exe folder 
# file_000.txt    0 FALSE 666 2016-10-27 17:19:40 2016-10-27 17:19:40 2016-10-27 17:19:40 no c:/test 
# file_001.docx    0 FALSE 666 2016-10-27 17:57:00 2016-10-27 17:57:00 2016-10-27 17:57:00 no c:/test/subfolder1 
# file_002.txt    0 FALSE 666 2016-10-27 17:18:34 2016-10-27 17:18:34 2016-10-27 17:18:34 no c:/test/subfolder1 
# file_003.docx    0 FALSE 666 2016-10-27 17:57:29 2016-10-27 17:57:29 2016-10-27 17:57:29 no c:/test/subfolder2 
# file_004.bmp    0 FALSE 666 2016-10-27 17:19:00 2016-10-27 17:19:00 2016-10-27 17:19:00 no c:/test/subfolder2 
# file_005.xlsx   8081 FALSE 666 2016-10-27 17:57:52 2016-10-27 17:57:52 2016-10-27 17:57:52 no c:/test/subfolder3 
############################################## 

Répondre

1

Quelque chose comme cela devrait fonctionner:

files  <- list.files("test", full.names = TRUE, recursive = TRUE) 
file_info <- lapply(files, file.info) 
table  <- do.call(rbind, file_info) 

table$file <- gsub(".*/(.+)$", "\\1", rownames(table)) 
table$folder <- gsub("(.*)/.+$", "\\1", rownames(table))