根据R中的父目录复制并重命名特定文件 [英] Copy and rename Specific Files based on parent directories in R

查看:87
本文介绍了根据R中的父目录复制并重命名特定文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用R解决这个问题,但是我会用任何编程语言来提高答案.

I am attempting to solve this issue in R, but I'll upvote answers in any programming language.

我有一个示例文件名矢量,例如所谓的file_list

I have an example vector of filenames like so called file_list

c("D:/example/sub1/session1/OD/CD/text.txt", "D:/example/sub2/session1/OD/CD/text.txt", 
"D:/example/sub3/session1/OD/CD/text.txt")

我要做的是移动并重命名文本文件,使其基于父目录中包含subsession的部分.因此,第一个文件将被重命名为sub2_session1_text.txt,并将其与其他文本文件一起复制到一个名为all_files

What I'm trying to do is move and rename the text files to be based on the part of the parent directory that contains the part about sub and session. So the first file would be renamed sub2_session1_text.txtand be copied along with the other text files to just 1 new directory called all_files

我正在为如何重命名文件而苦苦挣扎.我正在尝试将substrstr_locate_allpaste0结合使用,以基于这些父目录复制和重命名文件.

I'm struggling with some of the specifics of how to rename the file. I'm trying to use substr combined with str_locate_all and paste0 to copy and rename the files based on these parent directories.

在向量file_list的每个元素中定位位置,以构造substr的开始和结束位置

Locate the position in each element of the vector file_list to construct starting and ending position for substr

library(stringr)
ending<-str_locate_all(pattern="/OD",file_list)
starting <- str_locate_all(pattern="/sub", file_list)

然后我想以某种方式从这些列表中取出每个元素的那些模式的开始和结束位置,然后将其馈送到substr以进行命名,然后依次使用paste0创建 我想要的是

I then want to somehow pull out of those lists the starting and ending position of those patterns for each element and then feed it to substr to get the naming down and then in turn use paste0 to create What I'd like is something like

substr_naming_vector<-substr(file_list, start=starting[starting_position],stop=ending[starting_position])

但是我不知道如何为列表编制索引,这样它就可以知道如何为starting_position的每个元素正确编制索引.一旦弄清楚了,我就会填写这样的内容

but I don't know how to index the list such that it can know how to correctly index for each element the starting_position. Once I figure that out I'd fill in something like this

#paste the filenames into a vector that represents them being renamed in a new directory  
all_files <- paste0("D:/all_files/", substr_naming_vector)
#rename and copy the files 
file.copy(from = file_list, to = all_files)

推荐答案

下面是一个使用正则表达式的示例,该示例使其更短一些:

Here's an example using regular expression, which makes it somewhat shorter:

library(stringr)
library(magrittr)

all_dirs <-
  c("D:/example/sub1/session1/OD/CD/text.txt",
    "D:/example/sub2/session1/OD/CD/text.txt",
    "D:/example/sub3/session1/OD/CD/text.txt")

new_dirs <-
  all_dirs %>%
  # Match each group using regex
  str_match_all("D:/example/(.+)/(.+)/OD/CD/(.+)") %>%
  # Paste the matched groups into one path
  vapply(function(x) paste0(x[2:4], collapse = "_"), character(1)) %>%
  paste0("D:/all_files/", .)

# Copy them.
file.copy(all_dirs, new_dirs)

这篇关于根据R中的父目录复制并重命名特定文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆