0

I have a small batch script and wondering why is it working like this. The snippets shows a for loop which should look for Rf-Stick1.conf and find the string "type". Futhermore it should write the following value into "TYPE".

It works when i use "echo !TYPE!", but the if statement is not working. Someone knows the problem?

for /f "tokens=2 delims=: " %%a in ('type "Configs\Rf-Stick1.conf" ^| findstr /ir "^type" 2^>nul') DO set "TYPE=%%a"        

echo !TYPE!
if !TYPE! EQU "RF" echo rf
2
  • @MCND's answer is probably what you need, but also use something like echo ::!TYPE!:: whenever debugging values so you can see if there are leading/trailing spaces etc. that might be causing problems. Commented May 12, 2017 at 10:49
  • The if statement takes the quotation marks literally and includes them in the comparison; hence you must provide them on both sides of the comparison operator: if "!TYPE!" EQU "RF" echo rf Commented May 12, 2017 at 11:27

2 Answers 2

2

If the variable TYPE contains the value RF, then the command

if !TYPE! EQU "RF" echo rf

is parsed and converted into

if RF EQU "RF" echo rf

As you can see, you are comparing a string without quotes against a string with quotes, so, they don't match. Try with

if "!TYPE!" EQU "RF" echo rf
Sign up to request clarification or add additional context in comments.

Comments

0

Probably because if !TYPE! is interpreted as if not TYPE! You need to change your if condition so that the effective condition is what you want.

2 Comments

hmm, well, but if i use %TYPE% instead as i usually do, it doesnt work. Any suggestion?
What's the output of echo %TYPE%

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.