Trouble with errorlevels
Moderator: DosItHelp
Trouble with errorlevels
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
)
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
there is one closing parenthesis missing in your batch.
Re: Trouble with errorlevels
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
)
: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
inside a code block you need delayed expansion to access !errorlevel! (not %errorlevel%)
Re: Trouble with errorlevels
OK now in english please:)
What is delayed expansion and how caI implement in my code.
What is delayed expansion and how caI implement in my code.
Re: Trouble with errorlevels
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.
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
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.
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
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
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