仅将文本文件复制到 R 中的新文件夹中

Copying only text files into new folder in R

我有一个 PDF 文件夹,我应该在 R 中对其执行文本分析。到目前为止,最好的方法是使用 R 使用 pdftotext 将这些文件转换为文本文件。然而,在此之后我无法执行任何分析,因为文本文件与派生它们的 PDF 放在同一文件夹中。

我通过以下方式实现了这一目标:

dest <- "C:/PDF" 
myfiles <- list.files(path = dest, pattern = "pdf", full.names = TRUE) 
lapply(myfiles, function(i) system(paste('"C:/xpdfbin-win-3.04/bin64/pdftotext.exe"', paste0('"',i,'"')), wait= FALSE)) 

我想知道只保留文本文件的最佳方法,是在这一步中将它们保存到新创建的文件夹中,还是必须做更多。

我试过:

dir.create("C:/txtfiles")
new.folder <- "C:/txtfiles"
dest <- "C:/PDF"
list.of.files <-list.files(dest, ".txt$")
file.copy(list.of.files, new.folder) 

然而,这只会用前几行代码创建的文件命名的空白文本文件填充新文件夹 'txtfiles'。

使用以下代码:

files <- list.files(path="current folder location",pattern = "\.txt$")  #lists all .txt files
for(i in 1:length(files)){
    file.copy(from=paste("~/current folder location/",files[i],sep=""), 
          to="destination folder")

这应该将 "current folder location" 中的所有文本文件复制到单独的文件夹 "destination folder"。