Hi there
I use vRO 8.6.1 as a stand-alone installation in my vSphere/ESXi environment - and I'm really quite new with vRO.
Now, I have a seemingly simple problem, which I have no solution for: I want to copy a file (a *.xml) to a datastore of an ESXi host. The file is already imported as a ressource and I'm able get it's content within my workflow.
But now I stuck how to copy that file - or its content - to a new file on a datastore on an ESXi host?
Has anybody an understandable hint for a "beginner" like my? Would be very glad.
Regards
Roman
No problem, here you go, try this after you've created
// assuming ResourceElement is in myResource
// write the content to the tempFile
myResource.writeContentToFile(tempfilePath);
the temp file should contain the content from the resource now. I can't recall if it will be the straight content or the vRO export format of the resource. If the latter then you might need to use a FileWriter with the content of the MimeAttachment embedded inside the ResourceElement
// myResource is ResourceElement
// myTempFile is the temp file
var mime = myResource.getContentAsMimeAttachement();
try
{
var fileWriter = new FileWriter(myTempFile.path);
fileWriter.open();
fileWriter.write(mime.content);
fileWriter.close();
// Alternative to using FileWriter might this btw
// mime.write(myTempFile.directory, myTempFile.name);
}
catch(error)
{
System.log("Something went wrong writing the file content! " + error);
}
Apologies, the method 'readerAll' is a typo on my part - it should be 'readAll'.
I don't have access to an 8.x vRO Instance right now so I can't check. The methods you've flagged both appear in the API Explorer on my 7.6 deployment and on the class doc on vroapi.com
Seems odd.... On the IDE pane for your 8.x client page, can you look through the API Explorer pane to verify the following
- FileReader class exists
- and has the open() and readAll() methods defined?
Hi again 🙂
If you use FileWriter.clean() before you use write(mime.content)
This looks like it should produce the 'clobber' effect you want.
Hi
This action exists on my 7.6 installation
Can you check on you 8.x deployment and see if the same one is there? There's a workflow here (again 7.6) which uses this action
Maybe you can try it out and see how it goes?
Thanks.
I've seen this workflow as well in my version (8.6.1), and I tried it.
The problem I encounter with this workflow is, that it expects an input "srcFilepath" value as a string. I couldn't find out, how to point this string to a file which is imported as a resource?
Aha, OK, time for some dirty hacking then 😀
You can use the following to get where you want to be
var tmpFile = System.createTempFile(null); // if the argument is 'null' the file is created as ".tmp". Add a value to set something else
var srcPath = tmpFile.path; // this will be the path to the file object & you can pass this to the upload action
Check here for the details on File - https://www.vroapi.com/Class/Intrinsics/1.0.0/File
Thank you so much. But I'm slightly overwhelmed... 😥
Would you go a little bit more into the details? The source file I want to use is imported under "Resources"...
Then, I create an empty file:
And now?
As I said, I'm really a beginner.
No problem, here you go, try this after you've created
// assuming ResourceElement is in myResource
// write the content to the tempFile
myResource.writeContentToFile(tempfilePath);
the temp file should contain the content from the resource now. I can't recall if it will be the straight content or the vRO export format of the resource. If the latter then you might need to use a FileWriter with the content of the MimeAttachment embedded inside the ResourceElement
// myResource is ResourceElement
// myTempFile is the temp file
var mime = myResource.getContentAsMimeAttachement();
try
{
var fileWriter = new FileWriter(myTempFile.path);
fileWriter.open();
fileWriter.write(mime.content);
fileWriter.close();
// Alternative to using FileWriter might this btw
// mime.write(myTempFile.directory, myTempFile.name);
}
catch(error)
{
System.log("Something went wrong writing the file content! " + error);
}
Thank you very, very much!
I will try this out asap (as fast as a beginner is... 😉 ) und let you know how it worked!
A question arises:
How can I read this temporary file, e.g. to verify it's content or for debugging purposes (before I continue with the next workflow element)?
You can use a FileReader to open and read the file content like this
// open the file with a FileReader
// assuming srcFile refers to the file you wrote earlier
var fileReader = new FileReader(srcFile);
fileReader.open();
// you can read it all in one pass
var fileContent = fileReader.readerAll();
// then print
System.log(fileContent);
// or, read it line by line
var line;
while((line = fileReader.readLine()) != null)
{
System.log(line);
}
Superb!
I played already around with
new FileReader
assuming it must be something like this, but I didn't know the
fileReader.readerAll()
method...
Thanks a lot.
Hmmm...
I get some errors for unknown methods.
Neither the open() method is announced...
...nor the readerAll() method:
FileWriter seems to have no issues? What did I wrong?
Apologies, the method 'readerAll' is a typo on my part - it should be 'readAll'.
I don't have access to an 8.x vRO Instance right now so I can't check. The methods you've flagged both appear in the API Explorer on my 7.6 deployment and on the class doc on vroapi.com
Seems odd.... On the IDE pane for your 8.x client page, can you look through the API Explorer pane to verify the following
- FileReader class exists
- and has the open() and readAll() methods defined?
It works! 😀
The methods open() and readAll() are both visible in the API Explorer:
I don't know why they do not appear in the autocomplete function of the JS editor - as my screenshots prove?
Anyway, it work's now and thanks to your valueble help I can proceed.
Hi again
As I just see,
// myResource is ResourceElement
// myTempFile is the temp file
var mime = myResource.getContentAsMimeAttachement();
try
{
var fileWriter = new FileWriter(myTempFile.path);
fileWriter.open();
fileWriter.write(mime.content);
fileWriter.close();
// Alternative to using FileWriter might this btw
// mime.write(myTempFile.directory, myTempFile.name);
}
catch(error)
{
System.log("Something went wrong writing the file content! " + error);
}
"fileWriter.write(mime.content);" adds the content to the temporary file, instead of replacing its content? Is there a possibility to overwrite the just opened temporary file?
Regards
Hi again 🙂
If you use FileWriter.clean() before you use write(mime.content)
This looks like it should produce the 'clobber' effect you want.
Pretty easy, I could have seen for myself... 😞
Thanks very much!