R将具有特定名称模式的文件移动到不同子目录中的文件夹中



我想根据文件名称中的某个部分将文件复制到特定文件夹中。下面你会发现我的文件夹结构和文件的位置。在D0和D1文件夹中,您会发现文件的命名结构如下:20210308_DML_D0_Temp_s1_t1.txt或20210308_DML_D1_weather_s3_t6.txt,其中D0/D1位于哪个文件夹中,Temp/wweather是温度还是天气文件,s1/s3是位置,t1/t6是时间点。我想做的第一件事是循环遍历D0和D1文件中的txt文件,并将名称中包含Temp的文件移动到temperature子文件夹,将名称中含有weather的文件移动至D0和D1文件夹中的weather子文件夹

main Directory
|
|___ weather_day
├── D0
├── temperature
│  └── weather
|__ 20210308_DML_D0_Temp_s1_t1.txt
|__ 20210308_DML_D1_weather_s3_t6.txt
└── D1
├── temperature
└── weather
|__ 20210308_DML_D0_Temp_s1_t1.txt
|__ 20210308_DML_D1_weather_s3_t6.txt

我试着用一个for循环来做这件事,比如:

wd = getwd() #set working directory to subfolder
pathway = paste0(wd,"/weather_day/")
for (i in pathway){
file.copy(i,"temperature)
file.copy(i,"weather")
}

最后,我希望txt文件在文件夹中,根据它们的名称中是否有温度或天气:

main Directory
|
|___ weather_day
├── D0
├── temperature
|__20210308_DML_D0_Temp_s1_t1.txt
└── weather
|__ 20210308_DML_D0_weather_s3_t6.txt
├── D1
├── temperature
|__20210308_DML_D1_Temp_s1_t1.txt
└── weather
|__20210308_DML_D1_weather_s3_t6.txt

然而,它对我不起作用。我想我必须使用file.copy,但我如何使用此函数根据文件的特定名称模式移动文件?我是否可以使用for循环中的for循环来读取文件夹D0和D1,然后读取这些文件夹中的txt文件?

您没有提供太多信息。如果我理解你的要求,这应该行得通。

library(tidyverse)
# collect a list of files with their paths
collector = list.files(paste0(getwd(), "/weather_day"), 
full.names = T, # capture the file names along with the full path
recursive = T)  # look in subfolders 
# establish the new 'weather' path
weather = paste0(getwd(), "/weather/")
# establish the new 'temp' path
temp = paste0(getwd(), "/temp/")
collector = data.frame("files" = collector) %>%    # original path
mutate(files2 = ifelse(str_detect(str_extract(files, 
"([^\/]+$)"),
"weath"),  # if weather, make a new path
paste0(weather, 
str_extract(files,
"([^\/]+$)")
), # end paste0/ if true
ifelse(str_detect(str_extract(files,
"([^\/]+$)"),
"temp"), # if temp, make a new path
paste0(temp, 
str_extract(files,
"([^\/]+$)")
), # end paste0/ if true
files)    # if not weather or temp, no change
) # end if
) # end mutate
dir.create(weather)    # create directories
dir.create(temp)
# move the files
file.rename(from = collector[,1],
to = collector[,2])
# validate the change
list.files(weather) # see what's different
list.files(temp)    # see what's different

根据@alexdegrote1995添加的内容,这个怎么样:

# collect a list of files with their paths
collector = list.files(paste0(getwd(), "/weather_day"), 
full.names = T, # capture the file names along with the full path
recursive = T)  # look in subfolders 
# establish the new 'weather' path
weather = paste0(getwd(), "/D0/weather/")
# establish the new 'temp' path
temp = paste0(getwd(), "/D0/temperature/")
collector = data.frame("files" = collector) %>% 
mutate(files2 = ifelse(str_detect(str_extract(files, 
"([^\/]+$)"),
"weath"),
paste0(weather, 
str_extract(files,
"([^\/]+$)")
), # end paste0/ if true
ifelse(str_detect(str_extract(files,
"([^\/]+$)"),
"temp"),
paste0(temp,
str_extract(files,
"([^\/]+$)")
), # end paste0/ if true
files)    # if not weather or temp, don't change
), # end if
filesD1 = paste0(gsub(pattern="D0",          # make a third column for the D1 folder
replacement="D1",
x =files2,))) # end mutate

file.rename(from = collector[,1],  # move files to the D0 folder
to = collector[,2])
file.copy(from = collector[,2],    # add copy to the D1 folder
to = collector[,3])

编辑以包含更多文件名、前置条件(无目录结构(和后置条件。(加上移动而不是复制。(

files <- c("20210308_DML_D0_Temp_s1_t1.txt", "20210308_DML_D0_weather_s3_t6.txt",
"20210308_DML_D1_Temp_s1_t1.txt", "20210308_DML_D1_weather_s3_t6.txt")
# write some temp (empty) files for copying
for (f in files) writeLines(character(0), f)
parts <- strcapture(".*_(D[01])_([Tt]emp|[Ww]eather)_.*", files, list(d="", tw=""))
parts
#    d      tw
# 1 D0    Temp
# 2 D0 weather
# 3 D1    Temp
# 4 D1 weather
dirs <- do.call(file.path, parts[complete.cases(parts),])
dirs
# [1] "D0/Temp"    "D0/weather" "D1/Temp"    "D1/weather"
### pre-condition, only files, no dir-structure
list.files(".", pattern = "D[0-9]", full.names = TRUE, recursive = TRUE)
# [1] "./20210308_DML_D0_Temp_s1_t1.txt"    "./20210308_DML_D0_weather_s3_t6.txt" "./20210308_DML_D1_Temp_s1_t1.txt"   
# [4] "./20210308_DML_D1_weather_s3_t6.txt"
### create dirs, move files
Vectorize(dir.create)(unique(dirs), recursive = TRUE) # creates both D0 and D0/Temp, ...
#    D0/Temp D0/weather    D1/Temp D1/weather 
#       TRUE       TRUE       TRUE       TRUE 
file.rename(files, file.path(dirs, files))
# [1] TRUE TRUE TRUE TRUE
### post-condition, files in the correct locations
list.files(".", pattern = "D[0-9]", full.names = TRUE, recursive = TRUE)
# [1] "./D0/Temp/20210308_DML_D0_Temp_s1_t1.txt"       "./D0/weather/20210308_DML_D0_weather_s3_t6.txt"
# [3] "./D1/Temp/20210308_DML_D1_Temp_s1_t1.txt"       "./D1/weather/20210308_DML_D1_weather_s3_t6.txt"

最新更新