0

I am trying to do a very simple thing, I want to set a variable in Windows CMD and use it in a for loop, but for some reason my var is not loading inside the loop. Here's a simple example:

set myVar=someVal&&for /l %a in (0) do (echo %myVar%)

Let's say I want someVal to be echoed forever. One might assume this will print the val forever but when the loop starts it's like the variable was never set. Can someone point out what I'm missing?

I tried with single & and double &&. I also tried setlocal enabledelayedexpansion etc.

str4nger
  • 28
  • 4
  • 2
    Please read my answer [here](https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected/30284028#30284028) and adapt your question when you still have problems. – Stephan Feb 13 '23 at 18:53
  • Also as you've clearly used the [[tag:batch-file]] tag, then in a batch file, you'd have two lines. ```@Set "myvar=someVal"``` on line 1 and ```@For /L %%G In (0) Do @Echo %myVar%``` on line 2. Incidentally, there is absolutely no need to use `&&` over `&` in the single line case you've shown. – Compo Feb 13 '23 at 19:48

0 Answers0