0

Here's my problem:

I collected data and it is in 145 folders

  • each representing a separate measurement (inside each folder are 100 csv files)

  • 19 of these folders are "Si" measurements, which I want to move into a new folder called "Si" to exclude in later work

  • I made a list of those folders and tried to move them with file.copy

  • I get all FALSE.

Ideas?

side note: I want them MOVED, not COPIED. but I simplified it for now

here's my code, followed by some outputs:

path = "C:/Users/anakar/Desktop/HOME OFFICE/Klebsiella/Data for analysis/SpRaw - dummy" # Where the data is stored
current.folder <- path
all.folders = list.files(current.folder, full.names = TRUE)               # Makes a list of all the folders
all.folders.Si = all.folders[grepl("~si",all.folders, ignore.case = T)]   # Checks which lines have "~Si" in them and gives them a "TRUE" value in the standard column
new.Si.folder <- paste(current.folder,"/Si", sep = "")                    # Where I want my files to be copied to
dir.create(new.Si.folder)                                                 # Creates that target folder
file.copy(all.folders.Si, to = new.Si.folder)                             # SHOULD copy the folders to the new directory

Outputs:

> head(all.folders)
[1] "C:/Users/anakar/Desktop/HOME OFFICE/Klebsiella/Data for analysis/SpRaw - dummy/AN~20201027~K.P~4aap"        
[2] "C:/Users/anakar/Desktop/HOME OFFICE/Klebsiella/Data for analysis/SpRaw - dummy/AN~20201027~K.P~E.~coli~Gr05"
[3] "C:/Users/anakar/Desktop/HOME OFFICE/Klebsiella/Data for analysis/SpRaw - dummy/AN~20201027~K.P~E.coli~Gr06" 
[4] "C:/Users/anakar/Desktop/HOME OFFICE/Klebsiella/Data for analysis/SpRaw - dummy/AN~20201027~K.P~K.~pne~A91"  
[5] "C:/Users/anakar/Desktop/HOME OFFICE/Klebsiella/Data for analysis/SpRaw - dummy/AN~20201027~K.P~K.~pne~Gr18" 
[6] "C:/Users/anakar/Desktop/HOME OFFICE/Klebsiella/Data for analysis/SpRaw - dummy/AN~20201027~K.P~K.pne~A99"  
> head(all.folders.Si)
[1] "C:/Users/anakar/Desktop/HOME OFFICE/Klebsiella/Data for analysis/SpRaw - dummy/AN~20201027~K.P~Si"
[2] "C:/Users/anakar/Desktop/HOME OFFICE/Klebsiella/Data for analysis/SpRaw - dummy/AN~20201028~K.P~Si"
[3] "C:/Users/anakar/Desktop/HOME OFFICE/Klebsiella/Data for analysis/SpRaw - dummy/AN~20201029~K.P~Si"
[4] "C:/Users/anakar/Desktop/HOME OFFICE/Klebsiella/Data for analysis/SpRaw - dummy/AN~20201030~K.P~Si"
[5] "C:/Users/anakar/Desktop/HOME OFFICE/Klebsiella/Data for analysis/SpRaw - dummy/AN~20201103~K.P~Si"
[6] "C:/Users/anakar/Desktop/HOME OFFICE/Klebsiella/Data for analysis/SpRaw - dummy/AN~20201104~K.P~Si"
> file.copy(all.folders.Si, to = new.Si.folder)
 [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
anakar
  • 316
  • 2
  • 13
  • See if this answer helps - https://stackoverflow.com/a/57928839/680068 – zx8754 Feb 10 '21 at 08:40
  • Possible duplicate https://stackoverflow.com/q/10266963/680068 – zx8754 Feb 10 '21 at 08:40
  • 1
    Thanks for the tips. The first one https://stackoverflow.com/questions/10266963/moving-files-between-folders/57928839#57928839 gives the same result. The second (duplicate: https://stackoverflow.com/q/10266963/680068) is about files, not folders and their solutions didn't work for me – anakar Feb 10 '21 at 08:57

1 Answers1

0
  • Use list.files with full.names = TRUE and recursive = TRUE so that you have include all the files inside the nested folder.
  • In lis.files you can specify the pattern for the files that you are looking for. (Used the pattern '~si' here).
  • To move files you need to use file.rename and give complete path of the location where you want to move the files to.

Try :

current.folder <- path
all.files = list.files(current.folder, full.names = TRUE, 
                       recursive = TRUE, pattern = '~si') 
dir.create(paste(path, 'si', sep = '/'))
file.rename(all.files, paste(path, 'si', basename(all.files), sep = '/'))
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213