根据文件名将文件移动到 R 中的子目录 [英] Moving files to subdirectory in R based on file name

查看:70
本文介绍了根据文件名将文件移动到 R 中的子目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想根据文件名中的某个部分将文件复制到特定文件夹.为了您的概述,我将我的文件夹结构放在下面.在文件夹 D0 和 D1 中,我有多个文件(例如,我将两个文件的名称放在这里)以及文件夹 Weather 和 Temperature.我想将 .txt 文件移动到文件夹 Weather 或 Temperature,具体取决于文件名中是否包含 Weather 或 Temperature(看看我想要什么).

So I would like to copy files to a specific folder based on a certain part in their name. For your overview I put my folder structure below. In the folders D0 and D1 I have multiple files (as example I put names of two files here) and the folders Weather and Temperature. I would like to move the .txt files to either the folder Weather or Temperature depending on whether the file name has Weather in its name or Temperature (see what I would like).

现状:

main Directory
|
|___ Experiment
        ├── D0
           ├── temperature
        │  └── Weather
           |__ Weather 100.txt
           |__ Temperature 100.txt
        └── D1
           ├── temperature
           └── weather
           |__ Weather 100.txt
           |__ Temperature 100.txt

我想要什么:

main Directory
    |
    |___ Experiment
            ├── D1
               ├── Weather
                        |__Weather 100.txt
               └── Temperature
                        |__ Temperature 100.txt

我尝试分步进行,因此首先使用 D0 移动天气,然后使用 D0 进一步移动温度文件、D1 天气,最后移动 D1 温度文件.

I tried to do it in steps, so first the D0 to move the Weather, and then go further with D0 and move the Temperature files, D1 Weather and lastly the D1 Temperature files.

然而,问题是双重的.第一个障碍是,虽然我使用 Wea​​ther 获得了文件列表,但一旦我想将其复制到新目录,我就会收到错误消息,指出它无法复制文件,因为没有这样的文件或目录.我的代码有什么问题?第二个问题是,如果我想这样做,代码效率不高,因为我必须运行代码四次(如果有两个以上的地图(D3,D4等),甚至更多.有吗?一种使代码更高效的方法,以便它可以一次完成所有工作?

The problem however is twofold. The first obstacle is that although I get a list of files with Weather, once I want to copy it to a new directory I get an error saying that it cannot copy the file because there is no such file or directory. What is wrong with my code? The second problem is that the code is not so efficient if I want to do it like this, because I have to run the code four times (and even more if there are more than two maps (D3, D4 etc.). Is there a way to make the code more efficient, so it can do it all at once?

推荐答案

这里有一个函数,先获取./Experiment中的目录,然后应用fun函数将其找到的文件移动到共享部分名称的子目录中.

Here is a function that first gets the directories in ./Experiment and then applies a function fun to each of them, moving the files it finds to sub-directories sharing part of the name.

fun <- function(path){
  files <- list.files(path = path)
  files <- file.path(path, files)
  info <- file.info(files)
  dirs <- files[info$isdir]
  fls <- files[!info$isdir]
  out <- lapply(dirs, function(d){
    i <- grep(basename(d), fls, ignore.case = TRUE)
    if(length(i)){
      to <- file.path(d, basename(fls[i]))
      tryCatch(
        file.rename(fls[i], to = to),
        error = function(e) e
      )
    } else NULL
  })
  out
}

setwd('~/tmp/Experiment')
d <- list.dirs(recursive = FALSE)
sapply(d, fun)

这篇关于根据文件名将文件移动到 R 中的子目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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