Nested IF

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
joecepy
Posts: 27
Joined: 28 Sep 2012 13:58

Nested IF

#1 Post by joecepy » 28 Sep 2012 14:05

I'm trying to get the following batch working but i keep getting the syntax of the command is incorrect. I'm certain it is the nested IF EXIST but not sure why i'm getting it.

Code: Select all

@ECHO OFF
setlocal EnableDelayedExpansion
FOR /F %%A IN ( machine.txt ) DO (

   ping -n 1 -w 3 %%A>NUL
 
   IF ERRORLEVEL 0 IF NOT ERRORLEVEL 1 (
      IF NOT EXIST (\\%%A\c$\temp\test.txt" copy /Y test.txt "\\%%A\c$\temp\" >>copy.log
      @ECHO [begin files needed on %%A...]>>copy.log)
      ELSE (@ECHO file already exists on %%A)
             
   ) ELSE (
      @ECHO unable to connect to %%A>>copy-offline.log
   )
)
endlocal

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Nested IF

#2 Post by abc0502 » 28 Sep 2012 14:28

EDITED
for a start the error level is written wrong and why there is two, the one that equal to zero is enough, and there was 2 or 3 missing " ) "
here is the code ( i didn't remove the errorlevel, but you should)

Code: Select all

@ECHO OFF
setlocal EnableDelayedExpansion
FOR /F "tokens=* delims=" %%A IN ('type "machine.txt"') DO (
   
   ping -n 1 -w 3 %%A >NUL
   
   IF NOT %ERRORLEVEL% == 1 (
   
      IF %ERRORLEVEL% == 0 (
   
         IF NOT EXIST "\\%%A\c$\temp\test.txt" (
                               
                 copy /Y test.txt "\\%%A\c$\temp\" >>copy.log
                 @ECHO [begin files needed on %%A...]>>copy.log
         
         )ELSE ( @ECHO file already exists on %%A )
             
      ) ELSE (
      
         @ECHO unable to connect to %%A>>copy-offline.log
      
      )
   )
)
endlocal

joecepy
Posts: 27
Joined: 28 Sep 2012 13:58

Re: Nested IF

#3 Post by joecepy » 28 Sep 2012 14:40

Thanks for the quick reply. I got ELSE( was unexpected at this time.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Nested IF

#4 Post by abc0502 » 28 Sep 2012 14:50

joecepy wrote:Thanks for the quick reply. I got ELSE( was unexpected at this time.

because else should be written that way

Code: Select all

if <condition> ( <one commands>
) Else ( <one command> )

or

Code: Select all

if <condition> (
      <many commands>
) Else (
      <many commands>
)

missing ")" before it

Edit:
do you mean in my code?

joecepy
Posts: 27
Joined: 28 Sep 2012 13:58

Re: Nested IF

#5 Post by joecepy » 28 Sep 2012 15:39

Sorry abc. It was my bad. I was trying for neatness and deleted the space after ELSE ( and it didn't like. I didn't realize batch so fickled. Thanks again for the help. Worked great.

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: Nested IF

#6 Post by Liviu » 28 Sep 2012 20:22

abc0502 wrote:for a start the error level is written wrong and why there is two, the one that equal to zero is enough

Just as a footnote, but you misread the OP's "IF ERRORLEVEL 0 IF NOT ERRORLEVEL 1" line, which does indeed check that ERRORLEVEL is exactly 0 (because it translates to "if errorlevel is greater than or equal to 0, and it is not greater than or equal to 1"). Outside of for loops, it can be simplified to "IF %ERRORLEVEL%==0", but inside a loop that won't work since the %ERRORLEVEL% value is expanded at the time the for loop is parsed, not after each iteration. In the case here %ERRORLEVEL% would be always 0 (from "setlocal"), and the "ECHO unable to connect" line never executes, even if the "ping" fails.

Liviu

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Nested IF

#7 Post by abc0502 » 28 Sep 2012 20:26

Liviu wrote:
abc0502 wrote:for a start the error level is written wrong and why there is two, the one that equal to zero is enough

Just as a footnote, but you misread the OP's "IF ERRORLEVEL 0 IF NOT ERRORLEVEL 1" line, which does indeed check that ERRORLEVEL is exactly 0 (because it translates to "if errorlevel is greater than or equal to 0, and it is not greater than or equal to 1"). Outside of for loops, it can be simplified to "IF %ERRORLEVEL%==0", but inside a loop that won't work since the %ERRORLEVEL% value is expanded at the time the for loop is parsed, not after each iteration. In the case here %ERRORLEVEL% would be always 0 (from "setlocal"), and the "ECHO unable to connect" line never executes, even if the "ping" fails.

Liviu

I never took that in consideration before, thanks :)

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Nested IF

#8 Post by Ed Dyreen » 30 Sep 2012 08:04

'
In other words, 'IF %ERRORLEVEL%==0' equals 'if not errorLevel 1' outside the block but the latter is preffered cause it works inside the block.

Code: Select all

set /a errorLevel=0
(
       set /a errorLevel=1
       IF %ERRORLEVEL%==0 echo.%ERRORLEVEL%==0
       if not errorLevel 2 echo.errorLevel lss 2
)

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: Nested IF

#9 Post by Liviu » 01 Oct 2012 10:31

Ed Dyreen wrote:In other words, 'IF %ERRORLEVEL%==0' equals 'if not errorLevel 1' outside the block
That is only true if %ERRORLEVEL% is known to be positive or 0. In the general case (including possible negative values) 'IF %ERRORLEVEL%==0' is the same as 'if errorlevel 0 if not errorLevel 1'.

Code: Select all

@echo off

call :setErrLev -2
if not errorlevel 1 echo errorlevel = -2 : this line *is* echoed
call :setErrLev 4294967295 &rem hex 0xFFFFFFFF == -1 as a signed 32-bit integer
if not errorlevel 1 echo errorlevel = -1 : this line *is* echoed
call :setErrLev 0
if not errorlevel 1 echo errorlevel =  0 : this line *is* echoed
call :setErrLev 1
if not errorlevel 1 echo errorlevel = 1 : this line is *not* echoed
exit /b 0

:setErrLev
exit /b %~1

Liviu

Post Reply