0

I have the following line of CMD code (not batch):

for /l %A in (1,1,2) do (echo "in loop") & echo "out loop; this should only be echoed once"

Basically, those brackets (at least I thought) is what were supposed to end the loop code and start a new command OUTSIDE of the do command; however that doesn't seem to be happening.

If someone could tell me how to terminate the do statement to execute the last echo command individually after the loop instead of on each iteration of the loop while still keeping it a one-liner, it would be much appreciated.

Thank you for your time.

1 Answers1

2

The entire FOR command line must be enclosed in parenthesis and then the additional ECHO command can be appended with operator & to be executed unconditionally after the FOR loop.

Command line:

(for /L %A in (1,1,2) do @echo in loop) & @echo out loop; this should only be echoed once

Batch file:

@echo off
(for /L %%A in (1,1,2) do echo in loop) & echo out loop; this should only be echoed once

The single command echo in loop executed by for does not need to be enclosed in parenthesis.

See also How does the Windows Command Interpreter (CMD.EXE) parse scripts?

Mofi
  • 46,139
  • 17
  • 80
  • 143
  • Thanks for your succinct answer. The `@` before the `echo` makes the output cleaner in CMD as well, which is something I was unaware of. – agent987240 Oct 27 '18 at 17:51