问题描述
我想根据文件名中的某个部分将文件复制到特定文件夹。您将在下面找到我的文件夹结构以及文件所在的位置。在 D0 和 D1 文件夹中,您都会找到名称如下结构的文件:20210308_DML_D0_Temp_s1_t1.txt 或 20210308_DML_D1_weather_s3_t6.txt 带有 D0/D1 位于哪个文件夹中,Temp/weather 文件,无论是温度还是天气/天气/天气/天气是位置,t1/t6 是时间点。我想做的第一件事是遍历 D0 和 D1 文件中的 txt 文件,并将名称中包含 Temp 的文件移动到温度子文件夹,将名称中包含天气的文件移动到 D0 中的天气子文件夹和 D1 文件夹
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文件呢?
解决方法
编辑以包含更多文件名、前置条件(无目录结构)和后置条件。 (加上移动而不是复制。)
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(".",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"
,
您没有提供太多信息。如果我明白你在问什么,这应该可行。
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,"([^\\/]+$)")
),# 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(),"/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,paste0(weather,paste0(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[,# move files to the D0 folder
to = collector[,2])
file.copy(from = collector[,2],# add copy to the D1 folder
to = collector[,3])