viewpoint-particle

Author Topic: Read nested varible in ini file.  (Read 342 times)

Walls

  • Newbie
  • *
  • Posts: 11
Read nested varible in ini file.
« on: January 30, 2019, 06:39:27 am »
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

  • Tech Support
  • *****
  • Posts: 3016
    • WinBatch
Re: Read nested varible in ini file.
« Reply #1 on: January 30, 2019, 06:55:20 am »
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

  • Newbie
  • *
  • Posts: 11
Re: Read nested varible in ini file.
« Reply #2 on: January 30, 2019, 07:20:06 am »
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

  • Tech Support
  • *****
  • Posts: 3016
    • WinBatch
Re: Read nested varible in ini file.
« Reply #3 on: January 30, 2019, 07:52:06 am »
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

  • Sr. Member
  • ****
  • Posts: 280
Re: Read nested varible in ini file.
« Reply #4 on: February 06, 2019, 04:41:40 am »
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

  • Newbie
  • *
  • Posts: 11
Re: Read nested varible in ini file.
« Reply #5 on: February 07, 2019, 09:16:24 am »
Sorry for the mis communication. Thanks for the help!