循环列出零件中的文件 [英] Loop to list files in parts

查看:63
本文介绍了循环列出零件中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试复制大约10,000个文件,并使用以下代码将其粘贴到另一个目录中:

I'm trying to copy around 10,000 files and paste it into another directory with the following code:

f <- c (MYFILE $ COL1) # PART 1
m <- paste0 ("(", paste (f, collapse = "|"), ") .xml") #PART 2
files <- list.files (pattern = m) #PART 3

file.copy (paste0 (getwd (), "/", files), paste0 (getwd (), "/ FILES_XML /", files), overwrite = TRUE) # PART 4

当我尝试运行 files<-list.files(pattern = m)部分时,出现以下消息:

When I try to run the files <- list.files (pattern = m) part, the following message appears:

assertion 'tree-> num_tags == num_tags' failed in executing regexp: file 'tre-compile.c', line 634

为了防止出现该消息,我考虑过以500为500的部分运行代码.我如何使用 for 的部分来运行代码?

To prevent the message from appearing, I thought about running the code in 500 parts in 500. How would I do it in parts using for?

推荐答案

也许会执行以下操作.
请注意使用 help("file.path")代替 paste .

Maybe something like the following will do it.
Note the use of help("file.path") as a replacement for paste.

n <- length(f)
sp <- rep(1:ceiling(n / 5), each = 5, length.out = n)
lapply(split(f, sp), function(x){
  m <- paste0 ("(", paste (x, collapse = "|"), ")\\.xml")
  files <- list.files(pattern = m)
  infiles <- file.path(getwd(), files)
  outfiles <- file.path(getwd(), "FILES_XML", files)
  file.copy(infiles, outfiles, overwrite = TRUE)
})

这篇关于循环列出零件中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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