rkuechler
Contributor
Contributor

vRO 8.6.1: Copy a file (or it's content) to an ESXi datastore?

Jump to solution

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

0 Kudos
3 Solutions

Accepted Solutions
eoinbyrne
Expert
Expert

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);
}

 

View solution in original post

eoinbyrne
Expert
Expert

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?

View solution in original post

eoinbyrne
Expert
Expert

Hi again 🙂

If you use FileWriter.clean() before you use write(mime.content)

eoinbyrne_0-1646750821622.png

This looks like it should produce the 'clobber' effect you want.

 

 

View solution in original post

15 Replies
eoinbyrne
Expert
Expert

Hi

This action exists on my 7.6 installation

eoinbyrne_0-1645530271770.png

 

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

eoinbyrne_1-1645530367748.png

 

Maybe you can try it out and see how it goes?

 

0 Kudos
rkuechler
Contributor
Contributor

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?

rkuechler_0-1645531442124.png

 

 

 

 

0 Kudos
eoinbyrne
Expert
Expert

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

eoinbyrne_0-1645531629289.png

 

0 Kudos
rkuechler
Contributor
Contributor

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:

rkuechler_0-1645533439497.png

 

 

And now?

As I said, I'm really a beginner.


0 Kudos
eoinbyrne
Expert
Expert

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);
}

 

rkuechler
Contributor
Contributor

Thank you very, very much!
I will try this out asap (as fast as a beginner is... 😉 ) und let you know how it worked!

0 Kudos
rkuechler
Contributor
Contributor

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)?

0 Kudos
eoinbyrne
Expert
Expert

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);
}
0 Kudos
rkuechler
Contributor
Contributor

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.

0 Kudos
rkuechler
Contributor
Contributor

Hmmm...

I get some errors for unknown methods.
Neither the open() method is announced...

rkuechler_0-1645545817476.png

...nor the readerAll() method:

 

rkuechler_1-1645546051932.png

 

FileWriter seems to have no issues? What did I wrong?

 

 

0 Kudos
eoinbyrne
Expert
Expert

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?

rkuechler
Contributor
Contributor

It works! 😀

The methods open() and readAll() are both visible in the API Explorer:

rkuechler_0-1645599286355.png

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.

 

0 Kudos
rkuechler
Contributor
Contributor

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

0 Kudos
eoinbyrne
Expert
Expert

Hi again 🙂

If you use FileWriter.clean() before you use write(mime.content)

eoinbyrne_0-1646750821622.png

This looks like it should produce the 'clobber' effect you want.

 

 

rkuechler
Contributor
Contributor

Pretty easy, I could have seen for myself...  😞
Thanks very much!

0 Kudos