Jump to content
Sign in to follow this  
Karstyn

Moving Files / Creatiung Directories

Recommended Posts

I can easily get a file listing from a directory, but then I would like to move all of those files to other directories. I may also need to create the new directories these files will be moved to.

I was surprised that these two functions (fileMove and directoryCreate) are not already a part of ScriptMaster.

So I thought I would take a crack at creating these myself. Looking at the Groovy Reference site, in the File category, I don't see these methods.

Am I missing something simple?

Thanks.

Share this post


Link to post
Share on other sites

Hi Karstyn - you'll love learning Groovy/Java! Take a look at the class documentation for java.io.File:

http://java.sun.com/j2se/1.4.2/docs/api/java/io/File.html

in particular, you'll need to use the rename() method and the mkdir() or mkdirs() methods. If you get stuck, post your code and I'm sure somebody will be happy to help.

Share this post


Link to post
Share on other sites

Great - Thanks!

I was able to create a directoryCreate function easy enough. I am having problems with fileMove though.

This is what I'm starting with:

new File(origPath).renameTo(newPath)

where both parameters are fully qualified paths ending with the file name.

One question I have with paths - it seems that they always take forward slashes, correct? Regardless of platform?

Share this post


Link to post
Share on other sites

Seems to be a problem with my use of .renameTo() - I'm getting this error:

groovy.lang.MissingMethodException: No signature of method: java.lang.String.renameTo() is applicable for argument types: (java.lang.String) values: {"C:Documents and SettingskmccoyDesktopimagesMovedA0166433.jpg"}

Share this post


Link to post
Share on other sites

This error indicates that you're trying to call rename() on a String, instead of a File. Try something like:

new File("/path/to/oldfile").renameTo( new File("/path/to/newfile") )

Share this post


Link to post
Share on other sites

That was it!

It's the simple things. I'm not at all familiar with java yet so just fumbling my way along. That helped a lot.

Thanks a million!

Share this post


Link to post
Share on other sites

I was hoping to do something similar to this. Could anyone suggest the best way to copy a whole directory. Instead of moving it I need to copy a directory from one place to another.

Thanks.

Share this post


Link to post
Share on other sites

Tanner, I started a new thread called 'Copying directories' with a response to your question.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.