SexScripts : Copy and rename a file? - https://ss.deviatenow.com:443/viewtopic.php?f=4&t=299 Page 1 of 1

Copy and rename a file?

Banjo [ Mon Mar 18, 2013 5:09 am ]

Is this possible from within SexScripts?

What I'd like to do is:
1) Let the user select a file on their hard drive (works)
2) Copy this file to a subfolder in Images (works)
3) Rename the file to "yourPic" (don't know how!)

No problem if it can't be done (there are other workarounds), but I wasn't sure if this was possible or not using groovy.

Re: Copy and rename a file?

doti [ Mon Mar 18, 2013 8:44 pm ]

Here are the 3 solutions.
1) Let the user select a file on their hard drive (works)
Code:
def oldName = getFile("Which file ?")

2) Copy this file to a subfolder in Images (works)
Code:
def newName = "images/myscript/"+new File(oldName).getName()
new File(newName) << new File(oldName).getBytes()

3) Rename the file to "yourPic" (don't know how!)
Code:
new File(newName).renameTo("images/myscript/yourPic.jpg")

2+3) but simpler
Code:
new File("images/myscript/yourPic.jpg") << new File(oldName).getBytes()



See also an old message

Re: Copy and rename a file?

Banjo [ Tue Mar 19, 2013 5:17 am ]

Thanks, that works great (was doing the first two... love the third 'simpler' option alternative!).

One more thing, though: I notice that I can only make it work 'once'... that is, it won't overwrite the same filename if I run the same script again (have to delete the image manually for it to work again).

Is there a way to let it overwrite the file if it already exists?

Re: Copy and rename a file?

doti [ Tue Mar 19, 2013 9:08 am ]

Doing a copy and not a renaming (2 or 2+3) should work.
Or deleting before renaming
Code:
new File(newName).delete


Here is my references, a full page from Groovy : Recipes For File

Re: Copy and rename a file?

doti [ Mon Aug 17, 2015 2:20 pm ]

Notice (Android) : when in scripts using some additional java functions to open a file, I recommand from now set the path to an absolute folder explicitely, with this expression : System.getProperty("user.dir").

For example, from :
Code:
new File(newName).renameTo("images/myscript/yourPic.jpg")

To :
Code:
def dir = System.getProperty("user.dir")
new File(dir+"/"+newName).renameTo(dir+"/images/myscript/yourPic.jpg")

Re: Copy and rename a file?

doti [ Sat Jun 16, 2018 7:05 pm ]

Notice 2 : since API 9, you should use instead getDataFolder()

Page 1 of 1 All times are UTC + 1 hour [ DST ]
https://ss.deviatenow.com:443/
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Maroon Fusion theme created by Oxydo
Software, theme modifications, phpBB modification by Doti 2010, 2011