Force Variable Substitution from string read in from INI file?

Started by jerwah, December 04, 2014, 12:59:49 PM

Previous topic - Next topic

jerwah

I am trying to build a dymanic string for use in a SQL query. I'm reading in a WHERE statement from an INI file that gets constructed dynamically. Inside that text, I've put %VARIABLE% placeholders for several variables that also get set and loaded dynamically, that all is working properly. The issue is that if I set the variable "WHERECLAUSE" to the text I need in the script, then variable substitution works as expected, however if I read the exact same line of text from the INI file, the variables will not be substitued.

INIFILE:

[QUERYDEF]
VALIDWHERE=VALA='%VALA%' AND VALB='%VALB%' AND VALC='%VALC%' AND VALD='%VALD%'


And a sample bit of code showing the issue..

INIFILE="C:\Temp\Test.ini"

VALA="VALA GOES HERE"
VALB="VALB GOES HERE"
VALC="VALC GOES HERE"
VALD="VALD GOES HERE"

WHERECLAUSE="VALA='%VALA%' AND VALB='%VALB%' AND VALC='%VALC%' AND VALD='%VALD%'"
Message("WORKS",WhereClause)

WhereClause=IniReadPvt("QUERYDEF","VALIDWHERE","",INIFILE)
Message("DOESNT WORK",WhereClause)




I've also tried:


SQL=""
WhereClause=IniReadPvt("QUERYDEF","VALIDWHERE","",INIFILE)
SQL="%WhereClause%"
Message("DOESNT WORK",SQL)


and several other variant flavors to no avail..  Is there a way to force variable substitution to occur against an existing string?  Since the variables are also dynamically built I can't simply use strcat since I don't know "What" to substitue in until I read in the INI file..




td

WinBatch does not perform substitution on the contents of a variable. It can only use the existing contents of an existing variable.  By definition substitution is implemented as a preprocessor that works on the literal lines in the script but not on the preexisting contents of variables.

Since  you have to have specific variables instantiated with values (this would be necessary even if substitution worked the way you wanted it to) you can use string concatenation.  It would just require a some parsing of your where clause.

A useless example of combining substitution with concatenation:
Code (winbatch) Select

x = 'a'
y = 'x' ; The parsed variable name could be assigned here
z = %y%

Message("Test", "Z is ":z)

 
There are many ways to solve your problem so I will not be surprise if a forum member doesn't comes up with a more elegant approach.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

jerwah

Thanks.

That's essentially what I tried with this:

SQL=""
WhereClause=IniReadPvt("QUERYDEF","VALIDWHERE","",INIFILE)
SQL="%WhereClause%"
Message("DOESNT WORK",SQL)

That also doesn't work, I've come up with a workaround where I iterate through all the variables and do a string replace if it's found, but it's rather clunky, just seems like there is a better way.


td

You where not trying what was suggested.  Here is a hopefully better explanation using your example

Code (winbatch) Select

; Init the variables
VALA="VALA GOES HERE"
VALB="VALB GOES HERE"
VALC="VALC GOES HERE"
VALD="VALD GOES HERE"

; Create and read the ini file contents
INIFILE="C:\Temp\Test.ini"
IniWritePvt("QUERYDEF","VALIDWHERE","VALA='%%VALA%%' AND VALB='%%VALB%%' AND VALC='%%VALC%%' AND VALD='%%VALD%%'",INIFILE)
SQL=""
tmpWhereClause=IniReadPvt("QUERYDEF","VALIDWHERE","",INIFILE)

; Parse, substitute, and concatenate
WhereClause = ""
nItems = ItemCount(tmpWhereClause, '%%')
for i = 1 to nItems
   strItem = ItemExtract(i, tmpWhereClause, '%%')
   if i mod 2 then WhereClause:=strItem
   else WhereClause:= %strItem%
next

SQL = WhereClause
Message("DOES WORK",SQL)


I am not advocating that the above is the best way to accomplish the task.  In fact, I use a general purpose 'tokenizer' script snippet for most parsing problems. The above simple illustrates how to use substitution to solve the problem at hand.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade