renaming files using streamFilesFromServer

Questions, tips and tricks and techniques for scripting in Servoy

renaming files using streamFilesFromServer

Postby rodneysieb » Sat Jan 08, 2011 8:37 am

Hi

Am using the file plugin to stream files to the server and renaming them with the primary key (because of versioning) from a table using code
Code: Select all
         
if (vPath)
{
   var new_name = "/" + vID + vFiletype;
   plugins.file.streamFilesToServer(vPath, new_name);
}


this works perfectly as per the tool tip allowing me to rename the file and saving the file in the default server directory .

I want to do the same thing using 'streamFilesFromServer' (to change the filename back to its original) and as per the tooltip
plugins.file.streamFilesFromServer(file/fileName|fileArray/fileNameArray, serverFile/serverFileName|serverFileArray/serverFileNameArray, [Function callbackFunction])

this should be possible. The samplecode however is different as:
Code: Select all
// transfer all the files of a chosen server folder to a directory on the client
var dir = plugins.file.showDirectorySelectDialog();
if (dir)
{
   var list = plugins.file.getRemoteFolderContents('/images/user1/', null, 1);
   if (list)
   {
           var monitor = plugins.file.streamFilesFromServer(dir, list, callbackFunction);
    }
}

and there doesn't seem to be a way that i can rename the file as it is done in 'streamFilesToServer'. Am i missing something? Is this possible? What is the best way to rename the file during or after it is streamed?

Any help would be appreciated.
Regards,
Rodney Siebenhausen

logic@work
Brisbane, Australia
rodneysieb
 
Posts: 260
Joined: Tue Feb 14, 2006 9:11 am
Location: Brisbane, Australia

Re: renaming files using streamFilesFromServer

Postby ptalbot » Mon Jan 10, 2011 5:24 am

You can certainly rename files using streamFilesFromServer.
You can do it with one file or an array of files like in the following example:

Code: Select all
   // transfer all the files of a chosen server folder to a directory on the client
   var dir = plugins.file.showDirectorySelectDialog();
   if (dir)
   {
      var list = plugins.file.getRemoteFolderContents('/images/user1/', null, 1);
      if (list)
      {
         var clientFiles = new Array();
         for (var i = 0; i < list.length; i++)
         {

               // you will have implement your own renaming scheme of course... here, I just set the name to (i+1) + extension
               var extension = getExtension(list[i].getName());
               var newName = (i+1) + "." + extension;

               // create the new JSFile which will receive the data, setting its new name:
               var f = plugins.file.createFile(dir.getAbsolutePath() + "/" + newName);

               // put that in the array (in the same order as the remote files):
               clientFiles.push(f);

            }

            // then use this array as first argument:
         var monitor = plugins.file.streamFilesFromServer(clientFiles, list, callbackFunction);
         //...
       }
   }

   // inner extract extension function:
   function getExtension(name) {
      if (name) {
         var tokens = name.split("\.");
         return tokens[tokens.length - 1];
      }
      return name;
   }



You cannot (yet) use renameTo() on a remote JSFile the way you can on a local JSFile.
Will look at implementing that soon...

Hope this helps,
Patrick Talbot
Freelance - Open Source - Servoy Valued Professional
https://www.servoyforge.net
Velocity rules! If you don't use it, you don't know what you're missing!
User avatar
ptalbot
 
Posts: 1654
Joined: Wed Mar 11, 2009 5:13 am
Location: Montreal, QC

Re: renaming files using streamFilesFromServer

Postby rodneysieb » Tue Jan 11, 2011 2:49 am

Hi Patrick

Thanks for the help, all working fine.

Am writing the file now back to a temp directory and automatically opening with the default program.
Regards,
Rodney Siebenhausen

logic@work
Brisbane, Australia
rodneysieb
 
Posts: 260
Joined: Tue Feb 14, 2006 9:11 am
Location: Brisbane, Australia


Return to Methods

Who is online

Users browsing this forum: No registered users and 8 guests