1.

Solve : What does this script do??

Answer»

We use a script to AUTOMATE the reboot of our servers. The script is run from the task manager with a parameter that indicates the number of times the routine should run before it quits. So it's CALLED like this: serverrebootscript.bat 100

The script starts like this:

Code: [SELECT]@echo off
If "%1"=="" GOTO ERROR
set E2=0
set E1=0
set E0=0
goto firstrun

:firstrun
query session >sessions.txt
FIND "Active"<sessions.txt
IF NOT errorlevel 1 GOTO loop

:loop
echo Users logged on, checking retry limit
echo Incrementing retry count
:E0
if %E0%==9 goto E1
if %E0%==8 set E0=9
if %E0%==7 set E0=8
if %E0%==6 set E0=7
if %E0%==5 set E0=6
if %E0%==4 set E0=5
if %E0%==3 set E0=4
if %E0%==2 set E0=3
if %E0%==1 set E0=2
if %E0%==0 set E0=1
goto DONE
:E1
set E0=0
if %E1%==9 goto E2
if %E1%==8 set E1=9
if %E1%==7 set E1=8
if %E1%==6 set E1=7
if %E1%==5 set E1=6
if %E1%==4 set E1=5
if %E1%==3 set E1=4
if %E1%==2 set E1=3
if %E1%==1 set E1=2
if %E1%==0 set E1=1
goto DONE
:E2
set E1=0
if %E2%==9 set E2=0
if %E2%==8 set E2=9
if %E2%==7 set E2=8
if %E2%==6 set E2=7
if %E2%==5 set E2=6
if %E2%==4 set E2=5
if %E2%==3 set E2=4
if %E2%==2 set E2=3
if %E2%==1 set E2=2
if %E2%==0 set E2=1
goto DONE
:DONE
If "%E2%%E1%%E0%"=="%1" GOTO RetryLimit

I think all of the "E" business above is a counter but I don't understand how it works so I'm hoping someone can explain it to me.

I'm also interested in knowing if I can replace all of that using something like this:

Code: [Select]SET /A Counter=0
goto firstrun

:firstrun
query session >sessions.txt
FIND "Active"<sessions.txt
IF NOT errorlevel 1 GOTO loop

:loop
SET /A Counter +=1
goto DONE

:DONE
IF "%1" EQU %COUNTER% GOTO RetryLimit

I've left an awful lot of the script out and just focused on the section I want to replace so if you notice holes in the script or things that could be written differently . . . I agree . For right now I'm just interested in learning about the "E" section.

Thanks for the help,

MJA word preceeded by a colon ":" indicates a flag/label/marker (people call them different things, I think Microsoft calls them labels) This works with the "goto" and "call" commands, and allows you to chunk your script.
Code: [Select]goto :blue
CODE1

:blue
CODE2

exit
When you get to 'goto :blue' it will skip down to the line ":blue" and then CONTINUE. This will skip whatever CODE1 is.

Code: [Select]call :Green
CODE1

exit
:Green
CODE2
goto :eof
In this case when you got to 'call :Green' it would skip down to the line ":Green" and continue until it runs into "goto :eof" and then goes back, so you would exicute CODE2 before CODE1. You can also use 'Call :Green' as many times as you wish in your code. It works much like a function in c++ would, but can start without being called (it will walk right past a flag, so be careful where you place them.)

You can replace it with this: Additionally everything in the second block of text does nothing - as there is no logic to do anything else but execute the code at :loop

Code: [Select]@echo off
If "%1"=="" GOTO ERROR

:firstrun
query session >sessions.txt
FIND "Active"<sessions.txt
IF NOT errorlevel 1 GOTO loop

:loop
echo Users logged on, checking retry limit
echo Incrementing retry count
set /a counter=counter+1
if "%counter%"=="%1" GOTO RetryLimitThank you!

MJ



Discussion

No Comment Found