R等待系统可执行文件完成

时间:2014-02-04 19:25:08

标签: r shell wait batch-processing system2

我正在尝试通过位于我的C:/ GenSoftware / Colony /目录中的可执行文件Colony2.exe运行一系列输入文件(位于我的C:/ GenSoftware / Colony / datFiles /目录中)。我尝试重命名文件1,将其复制到与可执行文件相同的目录,使用包rcolony中的run.colony函数(粘贴在底部)运行Colony2.exe,删除文件,然后继续执行文件2。

但是,代码尝试在可执行文件完成之前继续。如何让我的循环等到Colony2.exe完成后再进入下一行代码,然后重新运行循环。 run.colony调用系统命令(粘贴在底部)。

到目前为止,这是我的代码......

rm(list=ls())

setwd("C:/GenSoftware/Colony/")
getwd()
datFiles <- list.files("datFiles")
library(rcolony)



d <- 0
for (d in 1:length(datFiles))
{
  d <- d+1
  setwd("C:/GenSoftware/Colony/datFiles/")
  file.rename(datFiles[d],"Colony2.DAT")
  file.copy(from = "C:/GenSoftware/Colony/datFiles/Colony2.DAT",to = "C:/GenSoftware/Colony/")
  datPath <- "C:/GenSoftware/Colony/Colony2.DAT"
  setwd("C:/GenSoftware/Colony/")
  run.colony(colonyexecpath = "Colony2.exe", datPath, wait = TRUE, monitor = FALSE)
  unlink(x = "C:/GenSoftware/Colony/Colony2.DAT", recursive = FALSE, force = TRUE)
  setwd("C:/GenSoftware/Colony/datFiles/")
  file.rename("Colony2.DAT",datFiles[d])
}
########我的代码结束,run.colony CODE开始
run.colony
function (colonyexecpath = "prompt", datfilepath = "prompt", 
    wait = FALSE, monitor = TRUE) 
{
    if (colonyexecpath == "prompt") {
        cat("Please click to select your Colony2 executable (probably called Colony2.exe or Colony2).\n\n")
        flush.console()
        colonyexecpath <- file.choose()
    }
    if (datfilepath == "prompt") {
        cat("Please click to select your DAT file.\n\n")
        flush.console()
        datfilepath <- file.choose()
    }
    datadir <- sub("([A-Z a-z0-9:/\\]+[/\\]+)([A-Z.a-z0-9]+)", 
        "\\1", datfilepath)
    filename <- sub("([A-Z a-z0-9:/\\]+[/\\]+)([A-Z.a-z0-9]+)", 
        "\\2", datfilepath)
    colonyexec <- sub("([A-Z a-z0-9:/\\]+[/\\]+)([A-Z.a-z0-9]+)", 
        "\\2", colonyexecpath)
    current.wd <- getwd()
    x <- readLines(paste(datadir, filename, sep = ""), n = 2)
    outputfilename <- substring(x[2], 1, 20)
    outputfilename <- sub("^[\t\n\f\r ]*", "", outputfilename)
    outputfilename <- sub("[\t\n\f\r ]*$", "", outputfilename)
    outputfilename
    if (file.exists(paste(datadir, outputfilename, ".MidResult", 
        sep = ""))) {
        stop("\nThere are output files already in the directory. \nColony has already run. \nTry deleting (or moving) these files and starting again.\n")
    }
    setwd(datadir)
    if (monitor == TRUE & wait == TRUE) {
        stop("If you want to monitor the output, you must set wait as FALSE. Otherwise you cannot run other functions in the same R console.")
    }
    cat("Be aware: this may take several minutes, hours, or even weeks to run, depending on the settings used.\n")
    platform <- .Platform
    if (platform$OS.type == "unix") {
        if (file.exists("Colony2") == FALSE) {
            system(paste("cp", colonyexecpath, datadir, sep = " "))
        }
        if (filename != "Colony2.DAT") {
            system(paste("mv", paste(datadir, filename, sep = ""), 
                paste(datadir, "Colony2.DAT", sep = ""), sep = " "))
        }
        if (filename != "Colony2.DAT") {
            system(paste("cp", paste(datadir, "Colony2.DAT", 
                sep = ""), paste(datadir, filename, sep = ""), 
                sep = " "))
        }
        cat("#! /bin/sh\necho Running Colony2\nexport G95_MEM_SEGMENTS=0\n./Colony2", 
            file = paste(datadir, "Colony2.sh", sep = ""), append = FALSE)
        if (monitor == TRUE) {
            system("sh Colony2.sh | tee temp.txt", wait = wait)
        }
        else {
            system("sh Colony2.sh", wait = wait)
        }
        system(paste("rm", colonyexec))
        if (file.exists("Colony2.sh")) {
            system(paste("rm Colony2.sh"))
        }
        else {
        }
        if (filename != "Colony2.DAT") {
            system("rm Colony2.DAT")
        }
    }
    else {
        if (platform$OS.type == "windows") {
            shell(paste("copy", colonyexecpath, datadir, sep = " "))
            if (filename != "Colony2.DAT") {
                shell(paste("rename", paste(datadir, filename, 
                  sep = ""), paste(datadir, "Colony2.DAT", sep = ""), 
                  sep = " "))
            }
            shell.exec("Colony2.exe")
            if (filename != "Colony2.DAT") {
                shell(paste("rename", paste(datadir, "Colony2.DAT", 
                  sep = ""), paste(datadir, filename, sep = ""), 
                  sep = " "))
            }
            shell("del Colony2.exe")
        }
        else {
            stop(paste("This function is not correctly configured to run on", 
                platform$OS.type, "systems."))
        }
    }
    setwd(current.wd)
}

0 个答案:

没有答案
相关问题