Skip to content

Instantly share code, notes, and snippets.

@ezhov-da
Last active March 10, 2019 12:23
Show Gist options
  • Save ezhov-da/3e71489a17c0a52e132791efb4b75e1d to your computer and use it in GitHub Desktop.
Save ezhov-da/3e71489a17c0a52e132791efb4b75e1d to your computer and use it in GitHub Desktop.
groovy script execute cmd command
[code:]groovy[:code]package ru.ezhov.groovy
def fileCreate = File.createTempFile("test", ".tmp")
def fileWriter = new FileWriter(fileCreate)
fileWriter.write("test all good")
fileWriter.flush()
fileWriter.close()
println("""file create: ${fileCreate.getAbsolutePath()}""")
println("file exist: ${fileCreate.exists()}")
def runtime = Runtime.getRuntime();
def process = runtime.exec("""cmd /c del /P ${fileCreate.getAbsolutePath()}""")
//def process = runtime.exec("cmd /c tasklist")
def outputStreamWriter = new OutputStreamWriter(process.getOutputStream())
outputStreamWriter.write("Y")
outputStreamWriter.flush()
outputStreamWriter.write("\r\n")
outputStreamWriter.close()
def inputStreamReader = new InputStreamReader(process.getInputStream(), "cp866")
inputStreamReader.each {
println(it)
}
inputStreamReader.close()
process.destroy()
println("file exist: ${fileCreate.exists()}")
[/code]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment