[IS 6.5] How to create a java service that renames a file?

Hello,

I looked for this kind of service in WMPublic in vain. The only service that I found is “getFile”!

What are the steps to follow to create this service? Is there anybody who had created a similar one? Thanks for help :wink:

Try this one.

[highlight=java]
// Specify pathname or parent/child. If both specified, pathname
// takes precedence. Renames a file specified by pathname or
// parent/child to the file specified by destpathname or
// destparent/destchild. Returns true if rename succeeds.
// Uses java.io.File to do the work.

public static final void renameTo (IData pipeline)
throws ServiceException
{
// — <<IS-START(renameTo)>> —
// @sigtype java 3.5
// [i] field:0:required pathname
// [i] field:0:required parent
// [i] field:0:required child
// [i] field:0:required destpathname
// [i] field:0:required destparent
// [i] field:0:required destchild
// [o] field:0:required result

IDataCursor pipelineCursor = pipeline.getCursor();
String pathname = IDataUtil.getString( pipelineCursor, “pathname” );
String parent = IDataUtil.getString( pipelineCursor, “parent” );
String child = IDataUtil.getString( pipelineCursor, “child” );
String destpathname = IDataUtil.getString( pipelineCursor, “destpathname” );
String destparent = IDataUtil.getString( pipelineCursor, “destparent” );
String destchild = IDataUtil.getString( pipelineCursor, “destchild” );

java.io.File f = null;
java.io.File dest = null;
if(pathname != null)
{
f = new java.io.File(pathname);
}
else if(parent != null && child != null)
{
f = new java.io.File(parent, child);
}

if(destpathname != null)
{
dest = new java.io.File(destpathname);
}
else if(destparent != null && destchild != null)
{
dest = new java.io.File(destparent, destchild);
}

IDataUtil.put( pipelineCursor, “result”, (f.renameTo(dest)?“true”:“false”));
pipelineCursor.destroy();
// — <> —
}
[/highlight]

Reamon,

can you please tell what values shoul we pass in parent/child string?

Pathname: fully-qualified path, such as c:\mydir\myfiile.txt or /application/etc/myfile; can also simply be directory, if you’re renaming a directory

Parent: directory containing the element to be renamed
Child: element (file or directory) within the parent to be renamed

If the pathname vars are used, the parent/child vars are ignored (they can null/not set).

Take a look at the Javadoc for the java.io.File constructors that are used for additional information.