我想通过使用 groovy 执行 xcopy 命令来简单地复制文件

I want to simply copy the file by executing xcopy command using groovy

我想复制一个文件,其源和目标存储在变量中。

def  gitPath = 'C:\demo1\'
def  ServerPath= 'D:\demo\git1\' 
def  file = 'Sports/Badminton/Players/Saina.txt'
def  command = "xcopy /y $gitPath$file $ServerPath$file /Q"

def copyManuscriptsCommandExecute =command.execute();

def cmcErr = new StringBuffer()
copyManuscriptsCommandExecute.consumeProcessErrorStream(cmcErr)

  def copyManuscriptsCommandExecuteOutput=copyManuscriptsCommandExecute.text
        println "Copy Manuscripts Command Execution Output: "+copyManuscriptsCommandExecuteOutput
        println "Copy Manuscripts Command Execution Error: "+cmcErr.toString() 

我收到无效错误 parameters.IF 我使用 def file = 'Sports\Badminton\Players\Saina.txt' 然后它工作 fine.However 我从命令执行的输出中获取变量文件的值,所以我可以改变那个。有没有办法来解决这个问题?

您需要将 unix 路径分隔符转换为 windows,如 \.

 ...

 file = normalize(file)
 def command = "xcopy /y $gitPath$file $ServerPath$file /Q"
...
static String normalize(String s) {
    s.replaceAll('/', File.separator)
}

在使用从命令行读取的字符串之前,只需规范化分隔符。