Code with error

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
alexandredneto
Posts: 39
Joined: 28 Feb 2013 13:40

Code with error

#1 Post by alexandredneto » 30 Jan 2014 05:55

Hi,

When I add this comment in two lines of the code below, the code works.

Code: Select all

:iis_ok
ECHO ++++++++++++++++++++++++++++++++++++++++++++++++
ECHO 005 ROTINA: iis_ok =============================
if defined goto_ret (
   ECHO 005+++ RETORNO
   set "goto_ret="
   set "iis_ok_perg=Stopped"
   if %W3SVC% EQU Running if %SMTPSVC% EQU Running if %HTTPFilter% EQU Running (
      ECHO 005+++ iisreset: %iis_ok_perg%
     set iis_ok_perg=Started
rem      )
   ECHO 005+++ iisreset: %iis_ok_perg%
rem   if %iis_ok_perg% EQU Started (
      ECHO 005+++ ROTINA: bye
      goto sair
      ) else (
     ECHO 005+++ ROTINA: iis_stop
      goto iis_stop
      )
   ) else (
   ECHO 005+++ ROTINA: iis_status
   set "goto_origem=iis_ok"
   goto iis_status
   )
ECHO 005+++++++++ FAILED ! ! !
goto bye


When I remove the comments, does not work, and the error is displayed:
C:\>ECHO 005 ROTINA: iis_ok =============================
005 ROTINA: iis_ok =============================
Started was unexpected at this time.

C:\> if EQU Started (

C:\>


What is the error code?

Thanks

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Code with error

#2 Post by foxidrive » 30 Jan 2014 06:52

You are setting the variable within a loop and trying to use it within the same loop.

Run this and you will see the problem. The variable is not available within the loop.

Code: Select all

@echo off
if not defined goto_ret (
   ECHO 005+++ RETORNO
   set "iis_ok_perg=Stopped"
 echo "%iis_ok_perg%"
)
pause


To use this kind of method you need to enable delayed expansion and then reference the variable as !iis_ok_perg! in the loop.

alexandredneto
Posts: 39
Joined: 28 Feb 2013 13:40

Re: Code with error

#3 Post by alexandredneto » 30 Jan 2014 07:51

Hi !
It is true, was this.
Your feedback was smart.
Thanks

Post Reply