1

我想根据文件名称中的某个部分将文件复制到特定文件夹。您将在下面找到我的文件夹结构以及文件所在的位置。在 D0 和 D1 文件夹中,您将找到名称如下结构的文件:20210308_DML_D0_Temp_s1_t1.txt 或 20210308_DML_D1_weather_s3_t6.txt,其中 D0/D1 位于哪个文件夹,温度/天气,无论是温度还是天气文件,s1/s3是位置,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文件呢?

4

2 回答 2

1

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

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"
于 2021-03-28T16:34:27.710 回答
1

您没有提供太多信息。如果我明白你在问什么,这应该可以。

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])
于 2021-03-28T17:14:20.040 回答