Writing a Text File via (Java) Scripting
Writing a Text File via (Java) Scripting
I searched the existing posts, didn't find an exact fit for what I want to be able to do. I'm simply looking for a code example showing the combining of perhaps a data field from the job, and perhaps some other static custom text, and writing it to a local file (on Windows), UTF-8. So: defining the file path and name, the guts of it and executing.
-
- Member
- Posts: 93
- Joined: Fri Jul 08, 2011 10:31 am
- Location: The Netherlands
Re: Writing a Text File via (Java) Scripting
Hi,
If you take a look at : https://www.enfocus.com/manuals/Develop ... class.html you can find this code :
This reads a file and writes it to a new file.
You can change the var str with other content.
If you take a look at : https://www.enfocus.com/manuals/Develop ... class.html you can find this code :
Code: Select all
//copies the content of the jobfile (in UTF-8) to a new path
//read the contents of the jobfile
var str = File.read(job.getPath(), "UTF-8");
//create new path
var destPath = job.createPathWithName("dest." + job.getExtension());
//write the contents to destPath
File.write(destPath, str, 'UTF-8');
//send to single outgoing connection
job.sendToSingle(destPath);
You can change the var str with other content.
- JimmyHartington
- Advanced member
- Posts: 310
- Joined: Tue Mar 22, 2011 7:38 am
Re: Writing a Text File via (Java) Scripting
Hi,
I have a script package, which does this.
You can download it here: https://d.pr/f/xrVmAx+
It writes a UTF-8 file with the content you provide in the variables text-box.
Out is the new file and the original file.
The code probably could be better, so if any of the coding wizards have some improvements please let my know.
This is the scripts code:
I have a script package, which does this.
You can download it here: https://d.pr/f/xrVmAx+
It writes a UTF-8 file with the content you provide in the variables text-box.
Out is the new file and the original file.
The code probably could be better, so if any of the coding wizards have some improvements please let my know.
This is the scripts code:
Code: Select all
// Is invoked each time a new job arrives in one of the input folders for the flow element.
// The newly arrived job is passed as the second parameter.
function jobArrived( s : Switch, job : Job )
{
var extension = s.getPropertyValue("Extension");
var tempFile = job.createPathWithExtension(extension);
var myFile = new File(tempFile, "UTF8");
var TextToWrite = s.getPropertyValue("Text_to_Write");
var InputJob = job.getPath();
myFile.open( File.WriteOnly | File.Truncate );
myFile.writeLine(TextToWrite);
myFile.close();
job.sendToSingle(InputJob);
job.sendToSingle(tempFile)
}
// Is invoked at regular intervals regardless of whether a new job arrived or not.
// The interval can be modified with s.setTimerInterval().
function timerFired( s : Switch )
{
}
Re: Writing a Text File via (Java) Scripting
I forget, what is the .sfixture file used for?
- JimmyHartington
- Advanced member
- Posts: 310
- Joined: Tue Mar 22, 2011 7:38 am
Re: Writing a Text File via (Java) Scripting
I do not know what the .sfixture is used for. But I would imagine it is used by the Script Editor.
Re: Writing a Text File via (Java) Scripting
When you work in scripted, then you can add a test job with test metadata etc in the fixture pane. This way you can test the script in scripter with the test data.
All the test data is stored in the sficture file. You don't need it to run the script in Switch.
All the test data is stored in the sficture file. You don't need it to run the script in Switch.