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

Author Topic: get the sum of %errorlevel% at the end of a subroutine.  (Read 11929 times)

0 Members and 1 Guest are viewing this topic.

osvikvi

    Topic Starter


    Rookie

    Thanked: 2
    • Experience: Experienced
    • OS: Windows XP
    get the sum of %errorlevel% at the end of a subroutine.
    « on: February 21, 2012, 06:57:11 AM »
    I composed a batch file to ping a list of devices, and return a notification whenever a device did not reply. These errors are also prepend to an existing logfile.

    All this works fine, here is my current script: (I don't think I have to explain the steps to you guru's...)

    Code: [Select]
    @echo off
    set fnm=D:\scripts\testip.txt
    set tnm=D:\scripts\testtemp.txt
    set lnm=D:\scripts\testpingme.txt

    if exist %fnm% goto :Label1

    echo.
    echo Cannot find %fnm%
    echo.
    pause
    goto :eof

    :Label1
    Set _t1=google.com ::Here I set my domain name to check if I am in the correct domain
    ping %_t1% -n 1
    if %ERRORLEVEL% EQU 1 goto :Label2
    echo YOU ARE CONNECTED
    goto :Label3

    :Label2
    echo    ***************************************************** >> %tnm%
    echo    ***************************************************** >> %tnm%
    echo    ** PingTest CANCELLED on %date% at %time% ** >> %tnm%
    echo    ** Reason: YOU ARE NOT CONNECTED TO THE OFFICE NETWORK ** >> %tnm%
    echo    ***************************************************** >> %tnm%
    echo    ***************************************************** >> %tnm%
    echo * >> %tnm%
    type "testpingme.txt" >> "testtemp.txt"
    del "testpingme.txt"
    ren "testtemp.txt" "testpingme.txt"
    MSG "%username%" /TIME:5 Pingtest CANCELLED
    goto :eof

    :Label3
    echo    ***************************************************** >> %tnm%
    echo    ***************************************************** >> %tnm%
    echo    ** PingTest STARTED on %date% at %time%   ** >> %tnm%
    echo    **                                                 ** >> %tnm%
    echo.
    for /f "skip=1 tokens=1,2 eol=;" %%a in (%fnm%) do call :sub %%a %%b
    echo.
    echo    **                                                 ** >> %tnm%
    echo    ** PingTest ENDED on %date% at %time%     ** >> %tnm%
    echo    ***************************************************** >> %tnm%
    echo    ***************************************************** >> %tnm%
    echo * >> %tnm%
    type "testpingme.txt" >> "testtemp.txt"
    del "testpingme.txt"
    ren "testtemp.txt" "testpingme.txt"
    goto :eof

    :Sub
    ping -n 1 %1
    IF %ERRORLEVEL% EQU 1 ECHO    **  # %1 - %2 is not responding     ** >> %tnm%
    IF %ERRORLEVEL% EQU 1 (mplayerc.exe /minimized /play /close "dingdong.wav") & MSG "%username%" /TIME:600 %2 - %1 is not responding

    Now what I'm trying to do next is, I want to have a message box (only when errors have occurred)asking if I want to see the logfile.

    to get this messagebox i could find a vbs script doing that
    Code: [Select]
    Option Explicit
    Dim oShell, retCode
    Set oShell = WScript.CreateObject("WScript.Shell")

    retCode = oShell.Popup("Open Logfile?", 10, "No Reply from host", 4 + 32)

    Select Case retCode
    case 6
    CreateObject("WScript.Shell").Run "testpingme.txt"
    case 7, -1
    WScript.quit(1)
    End Select

    Now when I try to call this vbs script from the batch, after the subroutine in label3 the rule applies only to the last device in the file. And it shows me the logfile but it is to early as the filename renaming is not finished yet.
    with this line:
    Code: [Select]
    IF %ERRORLEVEL% EQU 1 cscript yesno.vbs
    Now my question, How can I do a check when the subroutine is finished and if there where (one or more) errors during the routine to show me this messagebox and open the file once it's ready?

    Or does someone know if there is another approach to achieve the same result (probably it would even be better to show me a summary of errors at the end of the routine including an option to open the logfile)

    Thanks for helping out!

    osvikvi

      Topic Starter


      Rookie

      Thanked: 2
      • Experience: Experienced
      • OS: Windows XP
      Re: get the sum of %errorlevel% at the end of a subroutine.
      « Reply #1 on: February 22, 2012, 02:21:42 AM »
      Quote
      [PM]
      exit /b  will return to main program next line

      Thanks for the tip, where did you intend to insert it?
      I don't see why I should return anywhere, or am I missing something.

      patio

      • Moderator


      • Genius
      • Maud' Dib
      • Thanked: 1769
        • Yes
      • Experience: Beginner
      • OS: Windows 7
      Re: get the sum of %errorlevel% at the end of a subroutine.
      « Reply #2 on: February 22, 2012, 07:45:21 AM »
      Who did you recieve a PM from ? ?
      " Anyone who goes to a psychiatrist should have his head examined. "

      osvikvi

        Topic Starter


        Rookie

        Thanked: 2
        • Experience: Experienced
        • OS: Windows XP
        Re: get the sum of %errorlevel% at the end of a subroutine.
        « Reply #3 on: February 22, 2012, 08:32:54 AM »
        PM stands for private message, so let's keep that private.

        I presume that that person has good reasons why not posting in public. He has the right to do so and I think we should respect that. What does it matter anyway?....

        patio

        • Moderator


        • Genius
        • Maud' Dib
        • Thanked: 1769
          • Yes
        • Experience: Beginner
        • OS: Windows 7
        Re: get the sum of %errorlevel% at the end of a subroutine.
        « Reply #4 on: February 22, 2012, 08:35:07 AM »
        Do as you like...

        I have a good reason for asking.
        " Anyone who goes to a psychiatrist should have his head examined. "

        osvikvi

          Topic Starter


          Rookie

          Thanked: 2
          • Experience: Experienced
          • OS: Windows XP
          Re: get the sum of %errorlevel% at the end of a subroutine.
          « Reply #5 on: February 22, 2012, 08:38:52 AM »
          Okay, I'm still not with you but nevertheless...
          Do you have any suggestions?

          Squashman



            Specialist
          • Thanked: 134
          • Experience: Experienced
          • OS: Other
          Re: get the sum of %errorlevel% at the end of a subroutine.
          « Reply #6 on: February 22, 2012, 09:08:38 AM »
          If you want help privately then do it through some other means.  This is a help forum.  All answers to a thread need to be posted here.  Makes absolutely no sense to get some of your help from a PM and some of it here in your thread.

          osvikvi

            Topic Starter


            Rookie

            Thanked: 2
            • Experience: Experienced
            • OS: Windows XP
            Re: get the sum of %errorlevel% at the end of a subroutine.
            « Reply #7 on: February 22, 2012, 09:31:12 AM »
            what the

            Why do you think I post my question on this forum? I didn't pm myself to give this suggestion, some other guy did. I was just being open by posting his pm to the public as I figured it could also be useful  to others. and I wanted some more explanation on his thought.

            so why are you dissing me with this shite, I just want some advice from experienced users, but it seems to me they aren't here..... the room is full of old tarts!
            « Last Edit: February 22, 2012, 10:18:23 AM by patio »

            Raven19528



              Hopeful
            • Thanked: 30
              • Computer: Specs
              • Experience: Experienced
              • OS: Windows 7
              Re: get the sum of %errorlevel% at the end of a subroutine.
              « Reply #8 on: February 22, 2012, 10:18:46 AM »
              the room is full of old tarts!

              Yeah, that's a really good way to get people to help. Labeling the whole based on the replies of the few. Please forgive me if I'm not my usual helpful, explanatory self.

              If you want one check after the list of ips has run through, try this:

              Code: [Select]
              ping -n 1 %1
              IF %ERRORLEVEL% EQU 1 (
                ECHO    **  # %1 - %2 is not responding     ** >> %tnm%
                mplayerc.exe /minimized /play /close "dingdong.wav"
                MSG "%username%" /TIME:600 %2 - %1 is not responding
                set erry=1
              )

              Then after your REN command in label3, put:

              Code: [Select]
              ren "testtemp.txt" "testpingme.txt"
              if "%erry%"=="1" (cscript yesno.vbs)
              goto :eof

              Alternatively, try this in your subroutine if you want it to check after each time the subroutine runs:

              Code: [Select]
              ping -n 1 %1
              IF %ERRORLEVEL% EQU 1 (
                ECHO    **  # %1 - %2 is not responding     ** >> %tnm%
                mplayerc.exe /minimized /play /close "dingdong.wav"
                MSG "%username%" /TIME:600 %2 - %1 is not responding
                cscript yesno.vbs
              )

              Next time you are looking for help, try not to call the person/people capable of helping you "old tarts." Usually, it doesn't get you the results you are hoping for. :-\
              "All things that are
              Are with more spirit chased than enjoy'd" -Shakespeare

              patio

              • Moderator


              • Genius
              • Maud' Dib
              • Thanked: 1769
                • Yes
              • Experience: Beginner
              • OS: Windows 7
              Re: get the sum of %errorlevel% at the end of a subroutine.
              « Reply #9 on: February 22, 2012, 10:18:57 AM »
              Keep it clean...we have Members of all ages here.
              " Anyone who goes to a psychiatrist should have his head examined. "

              Squashman



                Specialist
              • Thanked: 134
              • Experience: Experienced
              • OS: Other
              Re: get the sum of %errorlevel% at the end of a subroutine.
              « Reply #10 on: February 22, 2012, 11:11:37 AM »
              and I wanted some more explanation on his thought.
              I have no idea what they were thinking.  I am not omniscient. Maybe you should ask the person to post here and explain themselves better.

              osvikvi

                Topic Starter


                Rookie

                Thanked: 2
                • Experience: Experienced
                • OS: Windows XP
                Re: get the sum of %errorlevel% at the end of a subroutine.
                « Reply #11 on: February 22, 2012, 11:32:46 AM »
                That's why I posted his pm here in the open. I couldn't reply to his pm for some reason.

                Anyway, Thanks Raven19528 I'll give it a try when I return to the office. to set things straight I said "it seems..."

                Sorry Patio, you may clean-out the irrelevant stuff, I would do it myself but I can't edit my posts.. Thanks for your support

                patio

                • Moderator


                • Genius
                • Maud' Dib
                • Thanked: 1769
                  • Yes
                • Experience: Beginner
                • OS: Windows 7
                Re: get the sum of %errorlevel% at the end of a subroutine.
                « Reply #12 on: February 22, 2012, 01:36:18 PM »
                No problem...
                " Anyone who goes to a psychiatrist should have his head examined. "

                osvikvi

                  Topic Starter


                  Rookie

                  Thanked: 2
                  • Experience: Experienced
                  • OS: Windows XP
                  Re: get the sum of %errorlevel% at the end of a subroutine.
                  « Reply #13 on: February 23, 2012, 03:39:20 AM »
                  HOORAY works like a charm, thanks Raven19528.

                  As usual the solution is simple, you just have to come up with it!
                   Now I have only one messageprompt and at the end of the cycle. giving me the option to open the logfile containing the errors.

                  Maybe I can show these errors in the vbs messagebox, is there any way to port %erry% to another script?

                  Thanks a lot

                  Salmon Trout

                  • Guest
                  Re: get the sum of %errorlevel% at the end of a subroutine.
                  « Reply #14 on: February 23, 2012, 04:39:06 AM »
                  Quote
                  is there any way to port %erry% to another script?

                  I hope it's not bad manners of me to jump in here, but you can pass parameters to a VBScript on the command line like so

                  (I always use //nologo)

                  cscript //nologo Scriptname.vbs "p1" "p2" "p3" (etc) (quotes are stripped in the script)

                  and in the VBScript there is an object called WScript.Arguments that acts like a zero-based array so that:

                  WScript.Arguments(0) will be p1
                  WScript.Arguments(1) will be p2
                  WScript.Arguments(2) will be p3

                  You are not limited to 9 parameters like in batch, but total command line string length cannot exceed 8192 characters.