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

Community Tip - Need help navigating or using the PTC Community? Contact the community team. X

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

diedrich
4-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

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
diedrich
4-Participant
(To:diedrich)

Even a solution for Prime is very welcome
 
Thank you very much
LucMeekes
23-Emerald III
(To:diedrich)

Here's the first incarnation in Prime 4:

LM_20191019_ReadIni.png

Which also works in Prime express.

 

Success!

Luc

LucMeekes
23-Emerald III
(To:LucMeekes)

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

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

diedrich
4-Participant
(To:Werner_E)

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

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

diedrich
4-Participant
(To:Werner_E)

-- delete --

Example INI-File:

LucMeekes
23-Emerald III
(To:diedrich)

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

diedrich
4-Participant
(To:LucMeekes)

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 ...
diedrich
4-Participant
(To:diedrich)

I'm having trouble using the forum.

All entries can be considered as a solution!
LucMeekes
23-Emerald III
(To:diedrich)

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

LucMeekes
23-Emerald III
(To:diedrich)

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

 

 

diedrich
4-Participant
(To:LucMeekes)

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
Top Tags