【问题标题】:Move files with specific name pattern to specific subfolders将具有特定名称模式的文件移动到特定子文件夹
【发布时间】:2021-06-30 15:40:59
【问题描述】:

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

目前情况:

main Directory
|
|___ Experiment
        ├── D1
           ├── Brightfield
        │  └── FITC
           |__ 20210205_DML_3_4_D1_PM_flow__w1brightfield 100 - CAM_s3_t5.TIF
           |__ 20210205_DML_3_4_D1_PM_flow__w2FITC 100- CAM_s3_t5.TIF
        └── D2
           ├── temperature
           └── weather
           |__ 20210219_DML_3_4_D2_AM_flow__w1brightfield 100 - CAM_s4_t10.TIF
           |__ 20210219_DML_3_4_D2_AM_flow__w2FITC 100- CAM_s4_t10.TIF

我想要什么:

main Directory
    |
    |___ Experiment
            ├── D1
               ├── Brightfield
                        |__20210205_DML_3_4_D1_PM_flow__w1brightfield 100 - CAM_s3_t5.TIF
               └── FITC
                        |__ 20210205_DML_3_4_D1_PM_flow__w2FITC 100- CAM_s3_t5.TIF
            ├── D2
               ├── Brightfield
                        |__20210219_DML_3_4_D2_AM_flow__w1brightfield 100 - CAM_s4_t10.TIF
               └── FITC
                        |__20210219_DML_3_4_D2_AM_flow__w2FITC 100- CAM_s4_t10.TIF

在 * 上提出的另一个问题中,我发现了一个我认为可以根据我的情况进行调整的代码,但我收到一条错误消息:mapply 中的错误(FUN = function (path, showWarnings = TRUE, recursive = FALSE, :零长度输入不能与非零长度的输入混合。显然需要形成的列表(部分)只显示NA。我使用的代码如下:

files <- c("20210205_DML_3_4_D0_PM_flow__w1brightfield 100 - CAM_s3_t5.TIF", "20210205_DML_3_4_D0_PM_flow__w2FITC 100- CAM_s3_t5.TIF",
           "20210219_DML_3_4_D1_AM_flow__w1brightfield 100 - CAM_s4_t10.TIF", "20210219_DML_3_4_D1_AM_flow__w2FITC 100- CAM_s4_t10.TIF")
# write some temp (empty) files for copying
for (f in files) writeLines(character(0), f)

parts <- strcapture(".*_(D[01])_*_([brightfield]|[FITC])_.*", files, list(d="", tw=""))
parts
#    d      tw
# 1 D0    Brightfield
# 2 D0    FITC
# 3 D1    Brightfield
# 4 D1    FITC

dirs <- do.call(file.path, parts[complete.cases(parts),])
dirs
# [1] "D0/Brightfield"    "D0/FITC" "D1/Brightfield"    "D1/FITCr"

### pre-condition, only files, no dir-structure
list.files(".", pattern = "D[0-9]", full.names = TRUE, recursive = TRUE)
# [1] "./20210205_DML_3_4_D0_PM_flow__w1brightfield 100 - CAM_s3_t5.TIF"    "./"20210205_DML_3_4_D0_PM_flow__w2FITC 100- CAM_s3_t5.TIF" 

### create dirs, move files
Vectorize(dir.create)(unique(dirs), recursive = TRUE) # creates both D0 and D0/Brightfield, ...
#    D0/Brightfield D0/FITC    D1/Brightfield D1/FITC 
#       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)

哪里出错了?

【问题讨论】:

    标签: r subdirectory movefile


    【解决方案1】:

    你做的parts &lt;- 有点不对,应该是这样的:

    
    parts <- strcapture(".*_(D[01])_.*(brightfield|FITC).*", files, list(d="", tw=""))
    parts
    
    

    输出:

    
    > parts
       d          tw
    1 D0 brightfield
    2 D0        FITC
    3 D1 brightfield
    4 D1        FITC
    
    

    有几个错误:

    • 您在 _*_ 中忘记了 .,正确的应该是 _.*_
    • 不要将 [] 放在明​​场和 FITS 周围,这不是您使用 [] 的方式。
    • 文件名中明场或 FITC 周围没有下划线。所以不要在你的正则表达式中加上下划线。

    我可以推荐阅读介绍文章或教程吗?在这里学习克服问题所需的知识并不需要太多。

    【讨论】: