Read nested varible in ini file.

Started by Walls, January 30, 2019, 06:39:27 AM

Previous topic - Next topic

Walls

I am wanting to read a named varible from an ini file and convert it to a its value.

The script below returns the computername varible correctly to the message box. This what I am looking for.

ConfigFile=StrCat(DirScript(),"imail.ini")
COMPUTERNAME=Environment("COMPUTERNAME")
ATTACHMENT=IniReadPvt ("MAIL", "ATTACHMENT", "C:\Log\%COMPUTERNAME%-log.txt", ConfigFile)
Message ("My computer is %COMPUTERNAME%",  ATTACHMENT)
exit

Below, when I force it to read the same string from the ini, the message box displays the litteral string without computer's name being subsituted.

ConfigFile=StrCat(DirScript(),"imail.ini")
COMPUTERNAME=Environment("COMPUTERNAME")
ATTACHMENT=IniReadPvt ("MAIL", "ATTACHMENT", "", ConfigFile)
Message ("My computer is %COMPUTERNAME%",  ATTACHMENT)
exit

Can this be done....?





td

I guess this is another example of why the use of substitution is discouraged.  To put another way, if you don't understand substitution, you probably shouldn't use it.  Substitution is only performed on lines in a script.  It is not performed on the contents of variables.

You need to use StrReplace or some other technique to replace a placeholder in your stored string with the actual computer name.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

Walls

I appreciate the answer...I do, but I don't see the need scoff at my question. I didn't understand, thats why I asked the question.

td

Not sure why you think there was some kind of scoffing going on.  Substitution is a powerful tool but is only needed in a small number of limited circumstances.   It is not well understood by WinBatch users and certainly is not needed in this case.  In other words, don't use substitution unless you absolutely need to.

In fact, the original creator of WinBatch regretted ever adding it to WIL and considered its inclusion his biggest mistake.   
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

kdmoyers

Yeah, no scoff -- this is actually a frequent topic around here -- the regretfully tiny number of cases where using substitution is (a) allowed and (b) a good idea.  All of us have dealt with it and just want to save you grief of struggling to make the rather subtle feature work.  Welcome to the burned-by-substitution club! We're all members! <smile>
-Kirby
The mind is everything; What you think, you become.

Walls

Sorry for the mis communication. Thanks for the help!