1

Basically when I am running this script, after runprog.exe returns (echos in cmd prompt) everytihng in the do ( ) section.

@echo off
set NODES=(server1.com server2.com)
for %%i in %NODES% do (
    echo Log stuff... >> logfile.txt
    runprog.exe /switch %%i
    if %ERRORLEVEL%==0 (echo success) else (echo fail)
    sleep 5
)

Edit: @echo off is at the top of the script.

leeman24
  • 2,729
  • 3
  • 29
  • 42
  • 2
    did you try `@echo off`? – Marc B Jan 30 '15 at 15:59
  • @echo off is set in the top. – leeman24 Jan 30 '15 at 16:37
  • Perhaps your `Log stuff...` evaluates to `on` at some point, so it gets interpreted as `echo on`. – Mike Nakis Jan 30 '15 at 16:43
  • @MikeNakis What do you mean 'evaluates' to on? I had an idea regarding that.. I tried to put echo off within my do statement at the top as well as atsign to supress the commands in the do statement ex. atsign echo. When I did that, it displayed blank lines for every line in the do statement. – leeman24 Jan 30 '15 at 18:37
  • Well, in MS-DOS batch files, instead of 'evaluates to' it is more correct to say 'expands to'. What I mean is that obviously, your statement does not exactly say `echo Log stuff...`, it says something else, which you have omitted because it is probably irrelevant. However, if this something else happens to be the text `on`, (or if it is something which somehow gets translated to `on`,) then the statement will be interpreted as `echo on`, thus turning echo on. – Mike Nakis Jan 30 '15 at 19:13
  • Interesting point. Although the first line within the `do ()` is `echo. >> %AUTOLOG% – leeman24 Jan 30 '15 at 19:21
  • It looks like you have posted a summary of your code, and not the actual code. I'm going to assume that `runprog.exe` is not the actual name of the program you are executing. If that program is actually a batch file, then it will cause the problem you are seeing. See http://stackoverflow.com/q/12077525/1012053 for a similar question, along with my accepted answer that explains what is happening. – dbenham Jan 30 '15 at 21:10
  • @MikeNakis: that isn't a "MS-DOS" batch file. That's a Windows batch file. –  Jan 31 '15 at 08:50
  • What is `sleep`? Another batch file? What does _that_ contain? –  Jan 31 '15 at 08:50
  • Please post your code by cutting and pasting. Summarising often obscures problems. Is your `.exe` truly an `.exe`? have you tried `start`ing it? What is `sleep`? Are you aware that `if %ERRORLEVEL%==0` will use the value of `errorlevel` as it was when the `for` command was parsed, not the changed value returned by your `.exe`? Too many questions to provide an adequate response . Why not post a sample of the screen output - just a couple of loops from the beginning Please edit this into your question. – Magoo Jan 31 '15 at 08:55
  • @a_horse_with_no_name well, of course the specific batch file at hand is a Windows batch file, but when speaking of the name of a general concept in the parsing and executing of these batch files, (string expansion,) it is not wrong to mention MS-DOS, because this concept has been in place since MS-DOS. – Mike Nakis Jan 31 '15 at 08:57
  • @MikeNakis: MS-DOS did not have string expansion remotely similar to what Windows does. –  Jan 31 '15 at 09:52

2 Answers2

1

The problem is in this sleep 5 command: it is a custom batch file that you have.

The funny thing is that if I run your batch file on my computer the exact same thing is happening, and I most probably have a different 'sleep' batch file than you. Mine contains the following:

@echo off
ping -n %1 127.0.0.1 > NUL 2>&1

Replacing sleep 5 with call sleep 5 fixes the problem here.

I have no idea why. Ask Microsoft.

Mike Nakis
  • 56,297
  • 11
  • 110
  • 142
0

As Mike Nakis said, it's the batch file sleep.
It's not important if sleep.bat contains @echo off or not.

The problem is that starting a batch file from another batch file transfers the control to the new batch file but doesn't return to the caller.

But in your case you have a FOR-loop which is completly cached by the cmd.exe.
That's the cause why the first batch doesn't stops immediately.
But when the second loop runs, the cmd.exe has leaved the batch-file-mode and is now in the cmd-line-mode.

You could control this by adding an echo line.

@echo off
set "world="  -- unset the world variable
for /L %%n in (1 1 3) do @(
  call echo Hello %%n %%world%%
  sleep.bat 1
)

The output will be

1 Hello
2 Hello %world%
3 Hello %world%

That's because the cmd-line-mode doesn't remove percent expansions when the variable is unset.

The CALL sleep.bat solves the problem, as then control simply returns to the caller, as expected.

jeb
  • 78,592
  • 17
  • 171
  • 225