tags:

views:

4111

answers:

3

I want to copy text files and only text files from src/ to dst/

groovy:000> "cp src/*.txt dst/".execute().text       
===> 
groovy:000> 

You can see the command executes w/out error but the file "src/test.txt" does not get copied to dst/

This also fails

groovy:000> "cp src/* dst/".execute().text       
===> 
groovy:000> 

However...

"cp src/this.txt dst/".execute().text

works

Also,

"cp -R src/ dst".execute().text

works

Why dose the wild card seem to cause my command to silently fail?

+2  A: 

Wildcard expansion is performed by the shell, not by cp (or groovy). Your first example is trying to copy a file named *. You could make your command "sh -c 'cp ...'"

A: 
groovy:000> "sh -c 'cp src/*.txt dst/' ".execute().text
===>

I'm still unable to get that to work.

JJohnson
Hi. see my blurb about getting the error stream as well as the standard out stream in http://stackoverflow.com/questions/159148/groovy-executing-shell-commands and I think you will find out what the problem is.
Bob Herrmann
+5  A: 

Thanks tedu for getting me half way there.

I believe the reason that his solution didn't work was because of an 'escaping' issue.

For instance...

"sh -c 'ls'".execute()

works. But...

"sh -c 'ls '".execute()

does not.

There is probably a way to escape it properly in line there but the workaround I'm using is to pass a string array to Runtime.getRuntime().exec

command = ["sh", "-c", "cp src/*.txt dst/"]
Runtime.getRuntime().exec((String[]) command.toArray())

works beautifully!

JJohnson
I think you could simplify that with: command = .... command.execute()As arrays also understand execute
TimM