Search code examples
rtidyversefile-manipulationmovefile

How to move files in a other folder with base on a df list


I'm download 20.000 files .png and put in order to specific folder name. Each folder has 1 or 2 .png files.

link of example: https://drive.google.com/drive/folders/1di8b1L1i1lc2ZKTAWyrNiZNpxMmGcQAH?usp=share_link

order_NEW<- data.frame(ACCESION=c('G    1','G    7A', 'G35015', 'G40897','G27573'),
                    FOLDER= c( 'P_vulgaris', 'P_vulgaris','P_dumosus', 'P_albescens', 
                               'P_lunatus'))

In order_NEW of FOLDER I'm want create this folders that contain 2 other folders, for sed and pod but depend of FOLDER name.

For example the FOLDER P_vulgaris is necessary create 2 others folders, sed and pod. In sed folder I'm want put the sed picture of G 1 and pod folder put the pod picture.

I have the folders in this form:

enter image description here

The folders contais this type of pictures: enter image description here

The idea is move the files in other folder, but depend of order_NEW df.

enter image description here


Solution

  • Im find this solution. Maybe dont is the most easy but for now is well for me. Sahe with us.

    1. Get the names of directories

      main_dir <- ('C:\\Users\\macosta\\Downloads\\CORE2')
      dir_list <- as.data.frame(list.dirs(main_dir,recursive = T))   
      1     C:\\Users\\macosta\\Downloads\\CORE2\\G   57
      3     C:\\Users\\macosta\\Downloads\\CORE2\\G   76
      4     C:\\Users\\macosta\\Downloads\\CORE2\\G   87
      5     C:\\Users\\macosta\\Downloads\\CORE2\\G   92
      6     C:\\Users\\macosta\\Downloads\\CORE2\\G  148
      7     C:\\Users\\macosta\\Downloads\\CORE2\\G  166
      
    2. Get only the folder name

       dir_list2 <- as.data.frame(list.dirs(main_dir,recursive = T,full.names = 
        FALSE)) 
           2                                                    G   57
           3                                                    G   76
           4                                                    G   87
           5                                                    G   92
           6                                                    G  148
           7                                                    G  166
      
    3. Merge the names and add to .jpg format (the pictures files has the same names of each folder)

       dir_list$NEW <- paste0(dir_list$`list.dirs(main_dir, recursive = T)`,"\\",
                          dir_list2$`list.dirs(main_dir, recursive = T, full.names = FALSE)` ," ", "seed.jpg")
      
      # [2] "C:\\Users\\macosta\\Downloads\\CORE2\\G   57\\G   57 seed.jpg"  
      # [3] "C:\\Users\\macosta\\Downloads\\CORE2\\G   76\\G   76 seed.jpg"  
      # [4] "C:\\Users\\macosta\\Downloads\\CORE2\\G   87\\G   87 seed.jpg"  
      # [5] "C:\\Users\\macosta\\Downloads\\CORE2\\G   92\\G   92 seed.jpg"  
      # [6] "C:\\Users\\macosta\\Downloads\\CORE2\\G  148\\G  148 seed.jpg"  
      # [7] "C:\\Users\\macosta\\Downloads\\CORE2\\G  166\\G  166 seed.jpg" 
      
    4. Im filter the names of each ACCESION in each FOLDER. And create in manual form each one. And in each one the two other folder, seed and pod.

    enter image description here

    1. Fit the names for use in a function. Add ' in start and final of each path, , for separate each one.

      dir_list$NEW<- paste0("'", dir_list$NEW,"'", ",")
      
      # I have problems with \\ and \. But is easy to change with replace tools of 
      Rstudio.
      # [2] "'C:\\Users\\macosta\\Downloads\\CORE2\\G   76\\G   76 seed.jpg',"  
      # [3] "'C:\\Users\\macosta\\Downloads\\CORE2\\G   87\\G   87 seed.jpg',"  
      # [4] "'C:\\Users\\macosta\\Downloads\\CORE2\\G   92\\G   92 seed.jpg',"  
      # [5] "'C:\\Users\\macosta\\Downloads\\CORE2\\G  148\\G  148 seed.jpg',"  
      # [6] "'C:\\Users\\macosta\\Downloads\\CORE2\\G  166\\G  166 seed.jpg',"  
      # [7] "'C:\\Users\\macosta\\Downloads\\CORE2\\G  169\\G  169 seed.jpg'"  
       #delete the (,) in final object. 
      
    2. Run this code

      # Im use this example 
      # https://stackoverflow.com/questions/71601557/moving-and-copying-multiple- 
        files
      df = data.frame(                                            
      source = c('C:\\Users\\macosta\\Downloads\\CORE2\\G   57\\G   57 seed.jpg',
                'C:\\Users\\macosta\\Downloads\\CORE2\\G   76\\G   76 seed.jpg',
                'C:\\Users\\macosta\\Downloads\\CORE2\\G   87\\G   87 seed.jpg',
                'C:\\Users\\macosta\\Downloads\\CORE2\\G   92\\G   92 seed.jpg',
                'C:\\Users\\macosta\\Downloads\\CORE2\\G  148\\G  148 seed.jpg',
                'C:\\Users\\macosta\\Downloads\\CORE2\\G  166\\G  166 seed.jpg',
                'C:\\Users\\macosta\\Downloads\\CORE2\\G  169\\G  169 seed.jpg'),
       destination = 
           c('C:\\Users\\macosta\\Downloads\\CORE_NEW_ORDER\\P_Vulgaris\\seed',                      
           'C:\\Users\\macosta\\Downloads\\CORE_NEW_ORDER\\P_Vulgaris\\seed',              
           'C:\\Users\\macosta\\Downloads\\CORE_NEW_ORDER\\P_Vulgaris\\seed',
           'C:\\Users\\macosta\\Downloads\\CORE_NEW_ORDER\\P_Vulgaris\\seed',             
           'C:\\Users\\macosta\\Downloads\\CORE_NEW_ORDER\\P_Vulgaris\\seed',
           'C:\\Users\\macosta\\Downloads\\CORE_NEW_ORDER\\P_Vulgaris\\seed',
           'C:\\Users\\macosta\\Downloads\\CORE_NEW_ORDER\\P_Vulgaris\\seed')
      
         file.copy(from = df$source, to = file.path(df$destination,
         basename(df$source)))
      
         # destination  is the new folder.
      
    3. For P_Vulgaris this are the files in seed.

    enter image description here