R-如何从RDCOMclient捕获错误

时间:2018-12-16 20:53:55

标签: r ms-access

我有一些代码可以压缩和修复许多MS Access数据库:

23

但是,有时代码返回以下错误:

library(RDCOMClient)
library(stringr)

accfolders <- list.dirs('C:\\users\\username\\accessdb\\',recursive = FALSE,full.names=F)[-1] #need -1 to exclude current dir
accfolders <- paste0("C:\\users\\username\\accessdb\\",accfolders)

#launch access
oApp <- COMCreate("Access.Application")

for (folder in accfolders) {

  accfiles <- list.files(path=folder, pattern="\\.mdb", full.names=TRUE)

  print(paste("working in dir", folder))

  for (file in accfiles){

    print (paste("working in db", file))
    bkfile <- sub(".mdb", "_bk.mdb", file)

    oApp$CompactRepair(file, bkfile, FALSE)

    file.copy(bkfile, file, overwrite = TRUE)
    file.remove(bkfile)    
  }  

  #print(paste("completed", folder))
}

oApp$quit()
gc()

此错误似乎是随机发生的,它在第二个for循环期间发生在调用<checkErrorInfo> 80020009 Error: Exception occurred.

我似乎无法弄清楚为什么会发生这种情况,它是通过随机oApp$CompactRepair文件而不是特定文件发生的。有时候我运行代码,根本没有问题,其他时候它会产生错误。

看到我无法弄清楚,我想知道是否可以某种方式捕获此错误,然后跳过for循环中的该元素?这样,代码就不会崩溃

0 个答案:

没有答案