Is there a R-based solution to create Windows 10 folders based on data files and their common names properties?

Viewed 40

i have many single data files in different formats. I look for a possiblity to create folders in Windows 10 in a given directory based on similarities of the files names (not on the same extensions of the single files). For example I show a small list of file names:

H_TS_02_024_026.xlsx
H_TS_02_024_026_prep.IRI
H_TS_02_024_026_prep2.Magg
H_TS_02_026_028.xlsx
H_TS_02_026_028_prep.IRI
H_TS_02_026_028_prep2.Magg
H_TS_02_060_062.xlsx
H_TS_02_060_062_prep.IRI
H_TS_02_060_062_prep2.Magg

So the idea is to create folders with the sample name following only using their common sample names to be used for the folders names:

H_TS_02_024_026.xlsx
H_TS_02_024_026_prep.IRI
H_TS_02_024_026_prep2.Magg

will end up in a folder and name of the folder will be

H_TS__02_024_026

containing the three files and for the next set of sample data it will be

H_TS_02_026_028.xlsx
H_TS_02_026_028_prep.IRI
H_TS_02_026_028_prep2.Magg

are contained in a folder named

H_TS_02_026_028

and so on. Thank you very much for your help and have a nice evening, CL

3 Answers

Here is a base R way.

flname <- tools::file_path_sans_ext(files)
s <- unlist(strsplit(flname, "_[:alpha:]+.*$"))
u <- unique(s)
for(fl in u){
  if(!dir.exists(fl)) dir.create(fl)
  if(dir.exists(fl)){
    f <- grep(fl, files, value = TRUE)
    file.rename(from = f, to = fl)
  }
}

Data

files <- "
H_TS_02_024_026.xlsx
H_TS_02_024_026_prep.IRI
H_TS_02_024_026_prep2.Magg
H_TS_02_026_028.xlsx
H_TS_02_026_028_prep.IRI
H_TS_02_026_028_prep2.Magg
H_TS_02_060_062.xlsx
H_TS_02_060_062_prep.IRI
H_TS_02_060_062_prep2.Magg"

files <- scan(text = files, what = character())

We can remove the file suffix at the end by matching the . followed by other characters at the end and remove them with blank. Use that to split into a list of common elements

split(files, sub("\\..*", "", files))

Maybe something like this?

library(tidyverse)

# data.frame(file = list.files())
df1 <- data.frame(
  file = c(
    "H_TS_02_024_026.xlsx",
    "H_TS_02_024_026_prep.IRI",
    "H_TS_02_024_026_prep2.Magg",
    "H_TS_02_026_028.xlsx",
    "H_TS_02_026_028_prep.IRI",
    "H_TS_02_026_028_prep2.Magg",
    "H_TS_02_060_062.xlsx",
    "H_TS_02_060_062_prep.IRI",
    "H_TS_02_060_062_prep2.Magg"
  )
) %>%
  mutate(folder = str_sub(file, 1, 15))
df1
#>                         file          folder
#> 1       H_TS_02_024_026.xlsx H_TS_02_024_026
#> 2   H_TS_02_024_026_prep.IRI H_TS_02_024_026
#> 3 H_TS_02_024_026_prep2.Magg H_TS_02_024_026
#> 4       H_TS_02_026_028.xlsx H_TS_02_026_028
#> 5   H_TS_02_026_028_prep.IRI H_TS_02_026_028
#> 6 H_TS_02_026_028_prep2.Magg H_TS_02_026_028
#> 7       H_TS_02_060_062.xlsx H_TS_02_060_062
#> 8   H_TS_02_060_062_prep.IRI H_TS_02_060_062
#> 9 H_TS_02_060_062_prep2.Magg H_TS_02_060_062

str_c("mkdir ", unique(df1$folder)) %>%
  str_c(collapse = "\n") %>%
  cat()
#> mkdir H_TS_02_024_026
#> mkdir H_TS_02_026_028
#> mkdir H_TS_02_060_062

df1 %>%
  mutate(move = str_c("move  ", file, "  ", folder, "\\")) %>%
  pull(move)  %>%
  str_c(collapse = "\n") %>%
  cat()
#> move  H_TS_02_024_026.xlsx  H_TS_02_024_026\
#> move  H_TS_02_024_026_prep.IRI  H_TS_02_024_026\
#> move  H_TS_02_024_026_prep2.Magg  H_TS_02_024_026\
#> move  H_TS_02_026_028.xlsx  H_TS_02_026_028\
#> move  H_TS_02_026_028_prep.IRI  H_TS_02_026_028\
#> move  H_TS_02_026_028_prep2.Magg  H_TS_02_026_028\
#> move  H_TS_02_060_062.xlsx  H_TS_02_060_062\
#> move  H_TS_02_060_062_prep.IRI  H_TS_02_060_062\
#> move  H_TS_02_060_062_prep2.Magg  H_TS_02_060_062\
Related