Copy a file inside to the same name folder

Post Reply
mcolmenero
Newbie
Posts: 19
Joined: Tue Feb 14, 2012 12:18 pm

Copy a file inside to the same name folder

Post by mcolmenero »

Hi,

We want to make a flow for it work to take a file and copy a folder with the same name as file.

At the begining we put a file a file it name is ABC015489L and the end of flow we have a folder and inside there are more folders with diferent names (ABC015489L, COM029874J....we don´t know how many) and Error folder.

If exist the folder copy the file inside and if it is not exist copy the file to Error folder.

I know i need a script.

Im preparing one, but I don´t know how to get up the list all folder to compare the name file with the name folder.

Could you help us please?

Thanks
rzacherl
Member
Posts: 38
Joined: Mon Mar 21, 2011 3:29 pm

Copy a file inside to the same name folder

Post by rzacherl »

Hi,



the basic code needed would look like this:



function jobArrived( s : Switch, job : Job )

{

var folderPath = s.getPropertyValue( "folderPath" );

var folder = new Dir( folderPath );

var entries = folder.entryList( job.getNameProper(), Dir.Dirs | Dir.NoDotAndDotDot, Dir.Unsorted );



if ( entries.length > 0 )

{

job.addBottomHierarchySegment( job.getNameProper() );

}

else

{

job.addBottomHierarchySegment( "Error" );

}



job.sendToSingle( job.getPath() );

}
mcolmenero
Newbie
Posts: 19
Joined: Tue Feb 14, 2012 12:18 pm

Copy a file inside to the same name folder

Post by mcolmenero »

Hi rzatzel,

Thanks for you help.

Perhaps I haven´t explained very good.

Attchement the script. But I can´t get up the list of folder

The first we take the file name only until first white space (normally 10 or 11 caracters)

Then we need take the list of folder in path (the path is F:/OUT/) and copy in the same folder as the file´s name (the folder name begin with 10 or 11 caracters of file´s name and then others caracters but these caracter as not important). If the folder is not exist, it copies to Error folder the file.

Best regards



The script:

For example i put two file ABC0123451 ZZZZ.pdf and CON029867J ZZZ.pdf, the First file exist a folder name ABC0123451 XXXXX and the second no exist any folder, It goes to Error Folder



var pathOri=job.getPath()

var nombre = job.getName();

nombre=nombre.toUpperCase();

var letras=nombre.indexOf(' ');

var nombrec=nombre.substring(0,letras);

var path = "F:/OUT/";

var myDirectory = new Dir(path);

//Here I can´t get up only the list of folder

var carpetas=myDirectory.entryList("*.*",Dir.All,"");

for (var i = 1; i //Here copy the file to F:/OUT/ABC012345l XXXXX/ABC0123451 ZZZZ.pdf

}

else

{

job.log(1, "incorrecto");

//Here copy the file to F:/OUT/Error Folder/CON029867J ZZZ.pdf

}

}
rzacherl
Member
Posts: 38
Joined: Mon Mar 21, 2011 3:29 pm

Copy a file inside to the same name folder

Post by rzacherl »

Hi,



the script code does with minor changes (you didn't explain the naming conventions before) exactly what you want.



All you have to do is to copy the code into a script package and create a script property named "folderPath", put a "Script element" in your flow and assign it this newly created script package and to put an "Archive hierarchy" flow element at the end of your flow.



Here's the modified code:

function jobArrived( s : Switch, job : Job )

{

var folderPath = s.getPropertyValue( "folderPath" );

var folder = new Dir( folderPath );

var shortJobName = job.getNameProper().match( /^[^ ]+/ );

var entries = folder.entryList( shortJobName + "*", Dir.Dirs | Dir.NoDotAndDotDot, Dir.Unsorted );



if ( entries.length > 0 )

{

if ( entries.length == 1 )

{

job.addBottomHierarchySegment( entries[0] );

}

else

{

job.addBottomHierarchySegment( "Error" ); //or whatever to do if more than one directory matches

}

}

else

{

job.addBottomHierarchySegment( "Error" );

}



job.sendToSingle( job.getPath() );

}
mcolmenero
Newbie
Posts: 19
Joined: Tue Feb 14, 2012 12:18 pm

Copy a file inside to the same name folder

Post by mcolmenero »

Hi rzacherl,

I have changed your script because I don´t know how to pass a argument into script. l have looked for swich help but i haven´t found.

This script goes on, but no delete the origen file. I know that this script copy file, no move, but how delete?

If I put " job.sendToSingle( job.getPath());" this sentence it moves the file into F:/OUT/ and the sentence s.copy it copies into its folder. Then I have the file in two places.

Thanks a lot for your help.

Best regards

Miguel



The script:



function jobArrived( s : Switch, job : Job )

{

var nombre = job.getName()

var folderPath = "F:/OUT/" ;

var folder = new Dir( folderPath );

var shortJobName = job.getNameProper().match( /^[^ ]+/ );

var entries = folder.entryList( shortJobName + "*", Dir.Dirs , Dir.Unsorted );

if ( entries.length > 0 )

{

if ( entries.length == 1 )

{

s.copy(job.getPath(),folderPath+entries[0]+"/"+nombre );

}

else

{

s.copy(job.getPath(),folderPath+"Entrada PS/"+nombre );

}

}

else

s.copy(job.getPath(),"F:/OUT/Entrada PS/"+nombre );

}

}
rzacherl
Member
Posts: 38
Joined: Mon Mar 21, 2011 3:29 pm

Copy a file inside to the same name folder

Post by rzacherl »

Hi Miquel,



seems that you're not familiar with many of the basic concepts in Switch like hierarchy info and especially with PowerSwitch and it's scripting API.



You don't need to copy the job to the final location or delete the original job file by using my script code. The script adds the necessary hierarchy info to the job's job ticket. As I already wrote simply put a "Archive hierarchy" flow element at the end of your flow, assign it the base path F:OUT and everything will work as expected. By using a sendTo...() method the incoming job file is automatically moved from the incoming folder to the selected outgoing connection(s).



Try to get a one day training course for Switch otherwise you really miss a lot of the really cool stuff in Switch.



Regards,



Robert
mcolmenero
Newbie
Posts: 19
Joined: Tue Feb 14, 2012 12:18 pm

Copy a file inside to the same name folder

Post by mcolmenero »

Hi rzacherl,

I know I don´t know a lot switch, I made a course of elemental switch (no scripting).

Here, I leave the script goes on.

We need create a flow element properties with name "ruta".

The script move the file to a destination folder where are a lot of subdirectory. Always the begining name's file and the begining name's folder.

Thanks for your help rzacherl, if you don´t help me i couldn´t do it

Here the script:

function jobArrived( s : Switch, job : Job )

{

var rutaini

var rutafin

var folderPath = s.getPropertyValue("Ruta" );

var nombre = job.getName();

var folder = new Dir( folderPath );

var shortJobName = job.getNameProper().match( /^[^ ]+/ );

var entries = folder.entryList( shortJobName + "*", Dir.Dirs , Dir.Unsorted );

if ( entries.length > 0 )

{

if ( entries.length == 1 )

{

rutaini=job.getPath();//ruta de origen;

rutafin=folderPath+entries[0]+"/"+nombre;//ruta fin

}

else

{

rutaini=job.getPath()

rutafin=folderPath+"Entrada PS/"+nombre

}

}

else

{

rutaini=job.getPath()

rutafin=folderPath+"Entrada PS/"+nombre

}

s.copy(rutaini,rutafin );

job.sendToNull(job.getPath());

job.log(1, "mueve de "+rutaini +" a "+rutafin );

}
Post Reply