使用 R 中的函数从多个文件夹中复制相同的文件并根据文件夹结构重命名文件

斯蒂恩·哈斯特

我是 R 的新手,并且已经构建了一系列工作循环,可以从多个子文件夹复制和重命名单个文件。

太好了!......但是这个问题是关于什么的?

好吧,我已经对 R 学习了足够多的知识,我知道大多数用户都同意应该避免循环并尽可能使用函数。这是因为函数比循环快得多。

当我将我的循环应用于我的真实数据时,它必须循​​环通过 +10000 个文件,因此我希望尽可能快地完成它。

我的循环可以用函数来表达吗?
或者可以以某种方式优化循环吗?
任何关于如何实现上述任何一项的示例或建议将不胜感激。

关于我的文件结构的信息:

F:/Study
  1000
     1001
       Gait1
         annotations.txt
       Gait2
         annotations.txt
     1002
       Gait1
         annotations.txt
       Gait2
         annotations.txt
  59000
     59003 
       Gait1
         annotations.txt
       Gait2
         annotations.txt

备注 “Gait”文件夹包含许多其他文件和目录,但我只对“annotations.txt”文件感兴趣。

我的循环:

for( a in seq(1000, 99000, by = 1000) ) {
  if (!dir.exists(paste0("F:/Study/", a))) {
    next()
    }
  for ( b in seq(1, 200, by = 1) ) {
      if (!dir.exists(paste0("F:/Study/", a,"/", a+b))) {
    next()
      }
    for ( c in seq(1, 2, by = 1)) {
        if (!dir.exists(paste0("F:/Study/", a,"/", a+b, "/", "Gait", c))) {
           next()
             }
      file.copy(from = paste0("F:/Study/", a,"/", a+b, "/", "Gait", c,"/annotations.txt"), to = "F:/Study/Annotations", overwrite = TRUE)
      setwd("F:/Study/Annotations")
      file.rename( from = "annotations.txt", to = paste0(a+b, "_Gait", c, "_annotations.txt") )
    }
  }
}

结果是我的 Annotations 文件夹中的文件名为:

1001_Gait1_annotations
1001_Gait2_annotations
1002_Gait1_annotations
1002_Gait2_annotations
59003_Gait1_annotations
59003_Gait2_annotations

tl;dr 可以用函数来表达循环吗?如何?

曼努埃尔·比克尔

您可以尝试以下操作(我假设您的 /Annotations 目录已经存在)。那对你有用吗?

#get all file names (full path) with the pattern "annotations" within all folders in directory
files <- list.files("F:/Study/", full.names = TRUE, recursive = TRUE, pattern = "annotations")

#extract the desired parts for the new filename with regex
#d+ stands for 1+x digits, .* for anything and $ for the end of line
#extract only the second capturing group (the parts within parenthesis) via \\2
filenames_new <- gsub("(.*/)(\\d+/Gait\\d+.*$)", "\\2", files)
filenames_new <- gsub("/", "_", filenames_new)

#create the new filepaths
files_new <- paste0("F:/Study/Annotations/", filenames_new) 

#perform copy
file.copy(from = files, to = files_new)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

重命名文件名末尾带有$'\ r的文件(包括文件夹中的递归文件),而无需使用重命名

在R中递归复制文件夹

使用R将多个文件从多个文件夹复制到单个文件夹

使用zmv重命名子文件夹中的文件

计算文件夹中多个文件的两列之间的r平方,然后使用R将结果写入单个文件

使用正则表达式从带有R的文件夹中读取多个csv文件

使用gsutil重命名GCS中的文件夹

使用文件夹名称重命名文件夹中的所有图像

如何使用PowerShell根据每个文件中的特定内容重命名文件夹中的文件?

用R随机重命名文件夹中的图像

使用Matlab重命名文件夹中的文件

使用python重命名目录中的多个文件夹

使用文本文件重命名文件夹中的批处理文件

使用R根据先前存在的文件夹顺序创建文件夹

使用R将文件复制粘贴到名称匹配的文件夹中

使用R将文件夹中的所有PDF和所有子文件夹复制到新文件夹

如何使用一个命令重命名多个文件夹中的多个文件

遍历文件夹并使用文件夹名称顺序重命名每个文件夹中的所有文件

使用php重命名文件夹列表中的内容

重命名文件夹中的文件,而无需使用Java重命名扩展名

我可以使用父文件夹的名称重命名文件夹中的文件吗?

使用子文件夹标签重命名子文件夹中的文件

使用父文件夹的名称重命名子文件夹中的文件

如何使用累进编号重命名多个文件夹?

使用 VBA 重命名文件夹

根据另一个文件中的字符串重命名文件夹中的文件 - 使用 Python

使用脚本任务重命名文件夹中的多个 .docx 文件

如何使用 python 和 pandas 重命名子文件夹中的多个 .Json 文件

使用python重命名多个子文件夹中的多个文件