#!/usr/bin/env groovy File.metaClass.copy = {String destName -> if(delegate.isFile()){ new File(destName).withOutputStream{ out -> out.write delegate.readBytes() } } else if (delegate.isDirectory()) { (new File(destName)).mkdirs() delegate.eachFileRecurse({ def newLocation = it.path.replaceFirst(delegate.path, destName) it.copy((new File(newLocation)).canonicalPath) }) } } def commandParams = this.args as List def asIndex = commandParams.findIndexOf{it == "as"} def toIndex = commandParams.findIndexOf{it == "to"} def minIndex = asIndex != -1 && toIndex != -1 ? Math.min(asIndex, toIndex) : Math.max(asIndex, toIndex) assert minIndex >= 1, "missing source name" if (asIndex != -1) assert asIndex + 1 < commandParams.size(), "missing new name" if (toIndex != -1) assert toIndex + 1 < commandParams.size(), "missing target name" def sourceName = commandParams[0] def targetName = asIndex != -1 ? commandParams[asIndex + 1] : (new File(sourceName)).name def targetLocation = toIndex != -1 ? commandParams[toIndex + 1] : "." def targetLocationFile = new File(targetLocation + (targetLocation.endsWith(File.separator) ? "" : File.separator) + targetName) assert !targetLocationFile.exists() (new File(sourceName)).copy(targetLocationFile.absolutePath)