Cancel
Showing results for 
Search instead for 
Did you mean: 

Global Windows Environment Variables

Valued Contributor
Valued Contributor

Hello,

 

I am attempting to have my model create an excel document in the windows directory that the model file is in. After that, I am attempting to read from this excel file. To do this without regard to what user account on the computer is using the model at the moment, I tried using the global windows environment variables in order to locate the current user's desktop, see example.

 

Example: TableFile.readExcelFile("%USERPROFILE%\Desktop\File.xlsx");

 

This does not work. Typing the absolute path (C:\users....\File.xlsx) works fine, however it doesn't solve the problem of multiple users. Writing to the root C:\ drive is not an option.

 

How do I have plant simulation create an Excel-file in the same directory that the model is run from, and what path should I type in the readExcelFile() method to find the excel file created?

 

 

Thank you.

8 REPLIES

Re: Global Windows Environment Variables

Siemens Phenom Siemens Phenom
Siemens Phenom

Hello ET_Boys,

 

you can use the function getenv to get the value of the environment variable USERPROFILE.

 

So something like this should work:

TableFile.readExcelFile(to_str(getenv("USERPOFILE"), "\Desktop\File.xlsx"));
Regards,

Ralf
------------------------------------------------------------------------------------------------------
Did you like the answer? Then click the Thumbs Up button.
Did the answer solve your problem? Then accept the answer as solution.

Re: Global Windows Environment Variables

Gears Esteemed Contributor Gears Esteemed Contributor
Gears Esteemed Contributor

you can use the method getCurrentDirectory to read the directory where the model file is located

Steffen Bangsow
freelance simulation specialist  
web: www.bangsow.eu
mail: steffen@bangsow.net

Re: Global Windows Environment Variables

Valued Contributor
Valued Contributor
Thank you, both solutions solved my problem Smiley Happy

Re: Global Windows Environment Variables

Genius
Genius
Is there also something like getCurrentFile to get the name of the model file?

Re: Global Windows Environment Variables

Valued Contributor
Valued Contributor
check

modelFile

Re: Global Windows Environment Variables

Genius
Genius
Super - yes, that's it - thanks a lot Steffen.

It's not in my PlantSim books, but it is in the help file. Hard to find that way, no link there between it and getCurrentDirectory.

Re: Global Windows Environment Variables

Genius
Genius

modelFile returns the full path and modelFileName, which is suitable in my application.

However there may be cases when just the filename is needed.
For future reference, and for others who may need this form, is there a way to get that?
(I realise that using string extraction may be the best solution)

And while this thread is open, (if there is a way) how can a method access its own name?
My reporting methods log the model name and execution timestamp, and it would also be useful for the reporting methods to identify their own names, to keep track of multiple nested reporting methods.

Re: Global Windows Environment Variables

Phenom
Phenom

@ 1. question try

 

var str:=splitString(modelFile,"\\")
print str[str.dim]

 

@ 2. question try

 

self.name