cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

How can I read a value from an ini file into a variable?

SOLVED
diedrich
Participant

How can I read a value from an ini file into a variable?

I have configuration files in INI format. I would like to read these in Mahcad. Can someone give me a tip as I can read the values ​​individually one by one in MathCad variables?

Something like a := READ(File,Section,valuename)

1 ACCEPTED SOLUTION

Accepted Solutions

Re: How can I read a value from an ini file into a variable?

Here is a first approach. Not much error handling included and will work for "well behaved" ini-files and numerical values only.

 
 
 

B.PNG

MC15 worksheet attached

View solution in original post

13 REPLIES 13

Re: How can I read a value from an ini file into a variable?

Even a solution for Prime is very welcome
 
Thank you very much

Re: How can I read a value from an ini file into a variable?

Here's the first incarnation in Prime 4:

LM_20191019_ReadIni.png

Which also works in Prime express.

 

Success!

Luc

Re: How can I read a value from an ini file into a variable?

Corrected an error where it would miss the last character of a parameter value.

 

Since the ReadIni(file, section, parameter) approach doesn't care about comment statements, it's sort of immune to them. This function will even handle this .INI file  https://downloads.avaya.com/elmodocs2/ip_phones/r2_7/output/16_601433_2/IPTel_Server044.html

 

Luc

Re: How can I read a value from an ini file into a variable?

You'll have to be more specific about the structure of an "INI-file".

Re: How can I read a value from an ini file into a variable?

Example of INI-File:

Filename: 'Geometry.ini'
Content (ASCII-File):
[Cylinder1]
Hight=100.12
Diameter=25.123456
[PointA]
X=123.12345
Y=123458.3
Z=7.0

I would like to read the Value of MathCad Variable CylHight out of the INI-File.
I'm looking for something like:

CylHight:=READINI('Geometry.ini','Cylinder1','Hight')

Best regards

Re: How can I read a value from an ini file into a variable?

Here is a first approach. Not much error handling included and will work for "well behaved" ini-files and numerical values only.

 
 
 

B.PNG

MC15 worksheet attached

View solution in original post

Re: How can I read a value from an ini file into a variable?

-- delete --

Example INI-File:

Re: How can I read a value from an ini file into a variable?

Note that you test.ini file fails to assign a value to "RT6Kanal" in section [A-Achse]. That will produce a 0 value for that parameter in my update of the function.

 

Luc

Re: How can I read a value from an ini file into a variable?

Thanks again for your support
Your solutions work as described by you. I have used some of your ideas and have now completed my implementation. The programming is certainly not optimal, but it works.
The INI files to be processed by me are between 500 and 1000 lines long. Therefore, I have disconnected the file read from the search. I first read the file into an array. In doing so, I remove all comments and unnecessary spaces and tabs. When accessing individual values, I use this array.
I attach an example as Zip.
Best regards and have a nice time ...

Re: How can I read a value from an ini file into a variable?

I'm having trouble using the forum.

All entries can be considered as a solution!

Re: How can I read a value from an ini file into a variable?

I guess that's how it works. The forum doesn't tell you which is the solution, it's up to you, the Original Poster (OP), to decide which of the reactions you consider to be THE solution.

 

Success!
Luc

Re: How can I read a value from an ini file into a variable?

Here's another approach.

LM_20191019_ReadIni.png

In your 'windows' directory you'll find more .INI files. Example:

LM_20191019_ReadIni.png

 

Success!
Luc

 

 

Re: How can I read a value from an ini file into a variable?

Hi
Many thanks for your (Werner_E and  help. You helped me a lot. My INI files are a bit more complicated (string values, comments, empty lines, comment lines, different comment characters (*, //), tabulators, indents, etc). But you have shown me a way to solve my problems. In the next weeks I will work on a solution and then show it here.
Thanks again and best regards
Announcements
Check out the latest
Mathcad Tip
"PTC Mathcad 15 / Prime 1-6 Update."