Trouble with errorlevels

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
eroica70
Posts: 9
Joined: 18 Feb 2013 12:33

Trouble with errorlevels

#1 Post by eroica70 » 10 Feb 2014 16:00

Hello,
I'm writing a batch script that checks for the presence of certain java files to change the security level on the computer. The problem is when checking the line IF %ERRORLEVEL% EQU 0 is never true in this code segment.. I put in an echo %errorlevel% command and I see that the errorlevel is always 1 even when the second findstr command should return a 0. I'm not most proficient at batch files so any suggestions most welcome

:Global

FINDSTR /I deployment.security.level= %userprofile%\AppData\LocalLow\Sun\Java\Deployment\deployment.properties >NUL
IF %ERRORLEVEL% EQU 1 (
FINDSTR /I /L deployment.security.level=MEDIUM C:\Windows\Sun\Java\Deployment\deployment.properties >NUL
IF %ERRORLEVEL% EQU 0 (
ECHO Java Security Level is already set to MEDIUM Globally. Press any key to exit...
pause >NUL
GOTO eof
)

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: Trouble with errorlevels

#2 Post by Endoro » 10 Feb 2014 16:07

there is one closing parenthesis missing in your batch.

eroica70
Posts: 9
Joined: 18 Feb 2013 12:33

Re: Trouble with errorlevels

#3 Post by eroica70 » 10 Feb 2014 16:09

Sorry I should have posted the whole goto label:
:Global

FINDSTR /I deployment.security.level= %userprofile%\AppData\LocalLow\Sun\Java\Deployment\deployment.properties >NUL
IF %ERRORLEVEL% EQU 1 (
FINDSTR /I /L deployment.security.level=MEDIUM C:\Windows\Sun\Java\Deployment\deployment.properties >NUL
IF %ERRORLEVEL% EQU 0 (
ECHO Java Security Level is already set to MEDIUM Globally. Press any key to exit...
pause >NUL
GOTO eof
)

ECHO deployment.security.level=MEDIUM >>C:\Windows\Sun\Java\Deployment\deployment.properties
FINDSTR /I /L deployment.security.level=MEDIUM C:\Windows\Sun\Java\Deployment\deployment.properties >NUL
IF %ERRORLEVEL% EQU 1 (
ECHO Changed Java Global Security Level to MEDIUM Succesfully. Press any key to end program.
pause
GOTO eof
)
) ELSE (
Echo Need to delete key value deployment.security.level in local profile file deployment.properties.
PAUSE
GOTO eof
)

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: Trouble with errorlevels

#4 Post by Endoro » 10 Feb 2014 16:18

inside a code block you need delayed expansion to access !errorlevel! (not %errorlevel%)

eroica70
Posts: 9
Joined: 18 Feb 2013 12:33

Re: Trouble with errorlevels

#5 Post by eroica70 » 10 Feb 2014 16:26

OK now in english please:)
What is delayed expansion and how caI implement in my code.

eroica70
Posts: 9
Joined: 18 Feb 2013 12:33

Re: Trouble with errorlevels

#6 Post by eroica70 » 10 Feb 2014 17:00

Thanks for the reply I got it working by doing Following:
SetLocal EnableDelayedExpansion
SET /P Java_Sec_Scope=Would you like to set the java security level (G)lobally or for (C)urrent User?
IF /i "%Java_Sec_Scope%"=="C" GOTO Local
IF /i "%Java_Sec_Scope%"=="G" GOTO Global ELSE (
ECHO Incorrect Input. Terminating Program!
PAUSE
GOTO END
)

:Global
FINDSTR /I deployment.security.level= %userprofile%\AppData\LocalLow\Sun\Java\Deployment\deployment.properties >NUL
IF %ERRORLEVEL% EQU 1 (
FINDSTR /I /L deployment.security.level=MEDIUM C:\Windows\Sun\Java\Deployment\deployment.properties >NUL
IF !ERRORLEVEL! EQU 0 (
ECHO Java Security Level is already set to MEDIUM Globally. Press any key to exit...
pause >NUL
GOTO END
)

ECHO deployment.security.level=MEDIUM >>C:\Windows\Sun\Java\Deployment\deployment.properties
FINDSTR /I /L deployment.security.level=MEDIUM C:\Windows\Sun\Java\Deployment\deployment.properties >NUL
IF !ERRORLEVEL! EQU 1 (
ECHO Changed Java Global Security Level to MEDIUM Succesfully. Press any key to end program.
pause
GOTO END
)
) ELSE (
Echo Need to delete key value deployment.security.level in local profile file deployment.properties.
PAUSE
GOTO END
)

Still somewhat hazy on why this was necessary. Because there were three errorlevel checks inside :GLOBAL I had to use delayed expansion??? But on the first errorlevel it isnt necessary? I have another label with only one errorlevel check so the !errorlevel! isnt necessary either. When I have some time I will have to reread the documentation online. But thanks again.

eroica70
Posts: 9
Joined: 18 Feb 2013 12:33

Re: Trouble with errorlevels

#7 Post by eroica70 » 10 Feb 2014 17:07

I spoke to quickly. Now I'm skipping over :Global Altogether when I input G at prompt. Here is my code:
rem Declare Variables
SET Java_Sec_Scope=""
SetLocal EnableDelayedExpansion
SET /P Java_Sec_Scope=Would you like to set the java security level (G)lobally or for (C)urrent User?
IF /i "%Java_Sec_Scope%"=="C" GOTO Local
IF /i "%Java_Sec_Scope%"=="G" GOTO Global ELSE (
ECHO Incorrect Input. Terminating Program!
PAUSE
GOTO END
)

:Global
FINDSTR /I deployment.security.level= %userprofile%\AppData\LocalLow\Sun\Java\Deployment\deployment.properties >NUL
IF !ERRORLEVEL! EQU 1 (
FINDSTR /I /L deployment.security.level=MEDIUM C:\Windows\Sun\Java\Deployment\deployment.properties >NUL
IF !ERRORLEVEL! EQU 0 (
ECHO Java Security Level is already set to MEDIUM Globally. Press any key to exit...
pause >NUL
GOTO END
)

ECHO deployment.security.level=MEDIUM >>C:\Windows\Sun\Java\Deployment\deployment.properties
FINDSTR /I /L deployment.security.level=MEDIUM C:\Windows\Sun\Java\Deployment\deployment.properties >NUL
IF !ERRORLEVEL! EQU 1 (
ECHO Changed Java Global Security Level to MEDIUM Succesfully. Press any key to end program.
pause
GOTO END
)
) ELSE (
Echo Need to delete key value deployment.security.level in local profile file deployment.properties.
PAUSE
GOTO END
)

:Local
FINDSTR /I /L deployment.security.level=MEDIUM %userprofile%\AppData\LocalLow\Sun\Java\Deployment\deployment.properties >NUL
IF %ERRORLEVEL% EQU 0 (
ECHO Java Security Level is already set to MEDIUM for Currently logged in User. Press any key to exit...
pause >NUL
GOTO END
)
ECHO deployment.security.level=MEDIUM >>%userprofile%"\AppData\LocalLow\Sun\Java\Deployment\deployment.properties
ECHO Java security level successfully changed to MEDIUM for Current User.
Pause
GOTO END
:END
EXIT
Any suggestions.

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: Trouble with errorlevels

#8 Post by Endoro » 11 Feb 2014 00:01

untested

Code: Select all

@ECHO OFF &SETLOCAL disableDelayedExpansion

rem Declare Variables
SET "Java_Sec_Scope="
SET /P "Java_Sec_Scope=Would you like to set the java security level (G)lobally or for (C)urrent User? "
IF /i "%Java_Sec_Scope%"=="C" GOTO Local
IF /i "%Java_Sec_Scope%"=="G" GOTO GLOBAL
ECHO Incorrect Input. Terminating Program!
PAUSE
GOTO END

:Global
SETLOCAL EnableDelayedExpansion
FINDSTR /LI "deployment.security.level=" "%userprofile%\AppData\LocalLow\Sun\Java\Deployment\deployment.properties" >NUL
IF %ERRORLEVEL% EQU 1 (
   FINDSTR /I /L "deployment.security.level=MEDIUM" "C:\Windows\Sun\Java\Deployment\deployment.properties" >NUL
   IF !ERRORLEVEL! EQU 0 (
      ECHO Java Security Level is already set to MEDIUM Globally. Press any key to exit...
      pause >NUL
      GOTO END
   )
   ECHO deployment.security.level=MEDIUM >>C:\Windows\Sun\Java\Deployment\deployment.properties
   FINDSTR /I /L "deployment.security.level=MEDIUM" "C:\Windows\Sun\Java\Deployment\deployment.properties" >NUL
   IF !ERRORLEVEL! EQU 1 (
      ECHO Changed Java Global Security Level to MEDIUM Succesfully. Press any key to end program.
      pause
      GOTO END
   )
) ELSE (
   Echo Need to delete key value deployment.security.level in local profile file deployment.properties.
   PAUSE
   GOTO END
)

:Local
FINDSTR /I /L "deployment.security.level=MEDIUM" "%userprofile%\AppData\LocalLow\Sun\Java\Deployment\deployment.properties" >NUL
IF %ERRORLEVEL% EQU 0 (
   ECHO Java Security Level is already set to MEDIUM for Currently logged in User. Press any key to exit...
   pause >NUL
   GOTO END
)
ECHO deployment.security.level=MEDIUM >>"%userprofile%\AppData\LocalLow\Sun\Java\Deployment\deployment.properties"
ECHO Java security level successfully changed to MEDIUM for Current User.
Pause
GOTO END
:END
EXIT

Squashman
Expert
Posts: 4487
Joined: 23 Dec 2011 13:59

Re: Trouble with errorlevels

#9 Post by Squashman » 11 Feb 2014 12:11

You can also just check the errolevel like this so that you don't need to use delayed expansion.

Code: Select all

IF ERRORLEVEL 0 goto foo
IF ERRORLEVEL 1 goto foobar

Post Reply