Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: What does this script do?  (Read 2937 times)

0 Members and 1 Guest are viewing this topic.

powlaz

    Topic Starter


    Beginner
  • Thanked: 1
    What does this script do?
    « on: September 20, 2013, 09:57:55 AM »
    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,

    MJ

    Lemonilla



      Apprentice

    • "Too sweet"
    • Thanked: 70
    • Computer: Specs
    • Experience: Experienced
    • OS: Windows 7
    Re: What does this script do?
    « Reply #1 on: September 20, 2013, 02:02:05 PM »
    A 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.)

    Quote from: patio
    God Bless the DOS Helpers...
    Quote
    If it compiles, send the files.

    foxidrive



      Specialist
    • Thanked: 268
    • Experience: Experienced
    • OS: Windows 8
    Re: What does this script do?
    « Reply #2 on: September 20, 2013, 08:29:57 PM »
    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 RetryLimit

    powlaz

      Topic Starter


      Beginner
    • Thanked: 1
      Re: What does this script do?
      « Reply #3 on: September 21, 2013, 07:59:23 AM »
      Thank you!

      MJ