0

In an attempt to reuse code I am trying to use dynamic variables to test a condition but am unable to get the result I need. I'm using delayed expansion.

1    Outside the for loop:
2    set "H_HEADER=FALSE"
3    set "SUB_TRANSTYPE=#"
4    
5    Inside the for loop:
6    set "SUB_TRANSTYPE=!FULL_LINE:~0,1!"
7    if !SUB_TRANSTYPE!==H (
8      echo sub_transtype_header is !!SUB_TRANSTYPE!_HEADER!
9    )

Line 6 sets SUB_TRANSTYPE to H

Line 8 prints H_HEADER to console but I want it to print FALSE (the value of H_HEADER)

I've messed around with escape characters but can't get this to work. Help!

ash
  • 3
  • 2
  • 1
    …but surely this is easier: `If !SUB_TRANSTYPE!==H Echo sub_transtype_header is !H_HEADER!` – Compo Sep 25 '17 at 13:47
  • It is, but I'm trying to reuse my code instead of having 100 separate lines with static variables. – ash Sep 27 '17 at 05:58
  • You and your accepted answer have both used a static character, `H` at the beginning of the `If` comparison there is therefore no need to use a variable to represent exactly the same known static character at the end of the same line. – Compo Sep 27 '17 at 08:48
  • This code is just part of a larger function, but this section is the part that is common over numerous functions. The static H is necessary because certain other actions are performed for different transaction types. – ash Sep 27 '17 at 10:43
  • `If` the string content of the variable `SUB_TRANSTYPE` is equal to the case sensitive string character `H`, then you know that the sub_transtype_header is the string content of the variable `H_HEADER`. Please provide me with the line you now have which doesn't use the static string character `H` and show me how that 'different' comparison string is arrived at. – Compo Sep 27 '17 at 10:54

2 Answers2

0
if !SUB_TRANSTYPE!==H (
   CALL echo sub_transtype_header is %%!SUB_TRANSTYPE!_HEADER%%
)

note that you have a = missing from the == operator.

This executes the echo in a subshell.


To interpret the value in an if statement, use

call set "someothervariable=%%!SUB_TRANSTYPE!_HEADER%%"
if "!someothervariable!"=="value" (
Magoo
  • 77,302
  • 8
  • 62
  • 84
  • Thank you, that worked. How then do I assemble a similar statement in an IF statement? Presumably CALL doesn't work for an IF? I.e. if %%!SUB_TRANSTYPE!_HEADER%%==FALSE () – ash Sep 25 '17 at 13:27
0

Line 6 should be:

set "SUB_TRANSTYPE=!FULL_LINE:~0,1!"

Line 7 should be:

if !SUB_TRANSTYPE!==H (

Line 8 should be:

for /F %%A in ("!SUB_TRANSTYPE!") do echo sub_transtype_header is !%%A_HEADER!

This type of management is fully described at this answer.

Aacini
  • 65,180
  • 12
  • 72
  • 108
  • Thank you, yes you found my typo's. I'll correct them in the original. Is there any benefit of a FOR statement over a CALL statement as Magoo suggested? – ash Sep 25 '17 at 13:30
  • In the linked answer there is this explanation: _"Another way to achieve the previous process is to use an additional FOR command to change the delayed expansion of the index by an equivalent replaceable parameter, and then use the delayed expansion for the array element. **This method runs faster than previous CALL**"_ Also, you may place an `if` inside the `for` and complete the comparison directly with no additional variables... – Aacini Sep 25 '17 at 13:35