因此,我想根据文件名称中的某个部分将文件复制到特定文件夹。为了您的概述,我将我的文件夹结构放在下面。在文件夹 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
在 stackoverflow 上提出的另一个问题中,我发现了一个代码,我认为我可以根据自己的情况进行调整,但我收到一条错误消息:mapply 中的错误(FUN = function (path, showWarnings = TRUE, recursive = FALSE, : zero-length输入不能与非零长度的输入混合。显然需要形成的列表(部分)只显示不适用。我使用的代码如下:
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)
它哪里出错了?