Page 1 of 1
Trouble with errorlevels
Posted: 10 Feb 2014 16:00
by eroica70
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
)
Re: Trouble with errorlevels
Posted: 10 Feb 2014 16:07
by Endoro
there is one closing parenthesis missing in your batch.
Re: Trouble with errorlevels
Posted: 10 Feb 2014 16:09
by eroica70
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
)
Re: Trouble with errorlevels
Posted: 10 Feb 2014 16:18
by Endoro
inside a code block you need delayed expansion to access !errorlevel! (not %errorlevel%)
Re: Trouble with errorlevels
Posted: 10 Feb 2014 16:26
by eroica70
OK now in english please:)
What is delayed expansion and how caI implement in my code.
Re: Trouble with errorlevels
Posted: 10 Feb 2014 17:00
by eroica70
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.
Re: Trouble with errorlevels
Posted: 10 Feb 2014 17:07
by eroica70
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.
Re: Trouble with errorlevels
Posted: 11 Feb 2014 00:01
by Endoro
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
Re: Trouble with errorlevels
Posted: 11 Feb 2014 12:11
by Squashman
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