0

I try use this code (work very well):

setlocal enabledelayedexpansion & set "WP=(wordA) (wordB) (wordC)" & set "WN=wordBB"
IF NOT "!WP:(%WN%)=!"=="!WP!" (echo found) ELSE (echo Not found)

But this not work (for use in vbscript):

cmd /v /c "setlocal enabledelayedexpansion & set "WP=(wordA) (wordB) (wordC)" & set "WN=wordBB" & IF NOT "!WP:(%WN%)=!"=="!WP!" (echo found) ELSE (echo Not found)"

What am I doing wrong?

EDIT:

This is a better example in my case:

cmd /v:on /c "@echo off & set "WP=(v0) (v2) (v6) (v4)" & (for %a in (v1 v2 v3 v4) do set "WN=%a" & if not \"!WP:(%WN%)=!\"==\"!WP!\" (echo Found %a) else (echo not %a) ) & pause"

I need use !WN! variable to search a string in !WP! (I can not use %a directly because !WN! is a filter for other functions ...

What i need to fix this?

murilo.xd
  • 69
  • 6

1 Answers1

0

You need to escape the inner double quotes (") with a backslash (\). Please check about escape characters in batch file here.

Except the above syntax cmd /v [...] is wrong. cmd /v:on will turn on delayed variable expansion and cmd /v:off will turn it off. Please type cmd /? in a fresh cmd for more information.

So, after these, you have to modified your command, so it will look like this:

cmd /v:on /c "set \"WP=(wordA) (wordB) (wordC)\" & set \"WN=wordB\" & if not \"!WP:(%WN%)=!\"==\"!WP!\" (echo found) else (echo Not found)"

By the way command setlocal enabledelayedexpansion was removed as it was not needed because in cmd you set delayed variable expansion with cmd /v:on as mentioned above.

Hope this helps!

double-beep
  • 5,031
  • 17
  • 33
  • 41
  • The `\ `is not an escape character in `cmd`, [it is the `^`](http://ss64.com/nt/syntax-esc.html) (also for you, @LotPings)... – aschipfl Dec 03 '18 at 21:09
  • Hello, it looks like there's still something wrong. Regardless of the text in the! WN !, it always results in "Found". Please look at my last issue. – murilo.xd Dec 03 '18 at 23:02