Injecting files using a Script

Post Reply
reprokaiser
Newbie
Posts: 16
Joined: Mon Apr 04, 2011 3:56 pm

Injecting files using a Script

Post by reprokaiser »

Hello,



please help me understand the mechanism to use for copying files by a script. I really don't grab the idea of 'createPathWithName()'. Why shouldn't I just sendTo a file to a connection? Why can't I use a simple 'copy' command?



My current task is to develop a script which should read an incoming mail (a list of documents) and inject the requested files into a flow. The incoming list is very simple, like this:



Page 15. - Page A

Page 34. - Page B

Page 99. - Page C

.

.

.



The script is reading each line of the text, and if the corresponding file is available in a set repository, then it should inject it into the flow. The catch is, that in one 'request mail' there may be a dozen or more file orders.



I solved the file identification and sorting of requests, but all my attempts to inject the needed files are failed. Like this:



var theSourcePath = "Z:/Resository/Page A.pdf";

var thePDF = new File( theSourcePath );

thePDF.sendToSingle( theConnection, theSourcePath );



Am I so dumb thinking that picking a file should be that easy?



Thanks in advance!
bens
Advanced member
Posts: 252
Joined: Thu Mar 03, 2011 10:13 am

Injecting files using a Script

Post by bens »

Actually, sending a file to a single outgoing connection really is as simple as you think. But there are some things to remember:



- You have to send a *job* to the outgoing connection, not a file. Basically this means you should use job.sendToSingle instead of thePDF.sendToSingle.



- sendToSingle takes as its first argument the path to the file or folder you want to send, and only works if your script has exactly one outgoing connection. A second parameter is only necessary if you want to rename the job. If you want more than one outgoing connection, you have to use the job.sendTo function.



- If you don't need the incomming job anymore, you have to tell Switch it can be discarded (otherwise it stays in the input folder forever) using job.sendToNull( job.getPath() ).



Assuming that you only need one outgoing connection, your code could become:



var theSourcePath = "Z:/Repository/Page A.pdf"

job.sendToSingle( theSourcePath );

job.sendToNull( job.getPath() );



It is important to note that the source file will be *moved* from the repository folder. If you need to keep a copy, then you must first copy the file to a temp path, and then send that copy. This is because Switch, for performance reasons, always moves files when using the sendTo... functions. The copy of the file could be placed anywhere - but the job.createPathWithName will give you a path to a temporary location that Switch will clean up automatically. For example:



var theSourcePath = job.createPathWithName( "Page A.pdf" );

s.copy( "Z:/Repository/Page A.pdf", theSourcePath );

job.sendToSingle( theSourcePath );

job.sendToNull( job.getPath() );
reprokaiser
Newbie
Posts: 16
Joined: Mon Apr 04, 2011 3:56 pm

Injecting files using a Script

Post by reprokaiser »

Hi Bens,



thanks for the enlightement! Just one more question: what's the difference between job.sendToNull() and job.remove() ? Both of them effectively delete the remaining file, don't they?



Thanks in advance, kind regards,



Peter
dkelly
TOP CONTRIBUTOR
Posts: 658
Joined: Mon Nov 29, 2010 8:45 pm
Location: Alpharetta GA USA
Contact:

Injecting files using a Script

Post by dkelly »

>> thanks for the enlightement! Just one more question: what's the difference between job.sendToNull() and job.remove() ? Both of them effectively delete the remaining file, don't they?



There isn't a job.remove() function. Maybe you are thinking of file.remove()?



If so, then file.remove() simply removes the file from the filesystem and doesn't inform Switch to delete the job info, such as metadata and job status.
reprokaiser
Newbie
Posts: 16
Joined: Mon Apr 04, 2011 3:56 pm

Injecting files using a Script

Post by reprokaiser »

Hi dkelly,



thanks for the answer! Yeah, I though about file.remove(), and now I see the difference.



One more question, if you don't mind. Why there isn't a file.rename() function available? How can I rename a file (not the job) using a script? Do I need to go around the circle like getting a temporary path, copying the file to whatever I got, then copying back to the original location with a different name, then removing the original file?



Thanks in advance, kind regards,



Peter
dkelly
TOP CONTRIBUTOR
Posts: 658
Joined: Mon Nov 29, 2010 8:45 pm
Location: Alpharetta GA USA
Contact:

Injecting files using a Script

Post by dkelly »

I don't know why there isn't a rename function. Maybe there are problems with rename across network volumes? You'd have to get an answer from Enfocus tech support folks.



You could simply do a e.copy() and then file.remove(). Not as efficient obviously.



Dwight Kelly

Apago, Inc.

dkelly@apago.com

http://www.apago.com
bens
Advanced member
Posts: 252
Joined: Thu Mar 03, 2011 10:13 am

Injecting files using a Script

Post by bens »

reprokaiser wrote: Why there isn't a file.rename() function available? How can I rename a file (not the job) using a script? Do I need to go around the circle like getting a temporary path, copying the file to whatever I got, then copying back to the original location with a different name, then removing the original file?


Sorry for the late reply - thankfully Dwight Kelly is around to provide faster feedback :-)



I suppose the short answer is probably "because nobody needed it before". After all, the large majority of file manipulations in Switch is done through Jobs - and renaming a Job is easy (each of the sendTo functions can do it).



If you really need to rename a file, than yes I think it will have to be by creating a copy and removing the original.



Alternatively, you could use the Process class to call mv (Mac) or ren (Windows). Depending on the size of the file as well as a lot of other factors, this could be faster than copying it.
Post Reply