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 11828 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.

                  Squashman



                    Specialist
                  • Thanked: 134
                  • Experience: Experienced
                  • OS: Other
                  Re: get the sum of %errorlevel% at the end of a subroutine.
                  « Reply #15 on: February 23, 2012, 05:35:41 AM »
                  You are not limited to 9 parameters like in batch, but total command line string length cannot exceed 8192 characters.
                  But you can get around that using SHIFT.   Most of my batch files are drag and drop and most of them get more than 9 files dropped on them.  So I use SHIFT to keep reading in %1 to a variable.
                  Code: [Select]
                  @echo off
                  :_shift
                  If "%~1"=="" Goto :EOF
                  echo.%~1
                  Shift
                  Goto _Shift
                  The other option is to use a for loop to iterate through all the command line arguments.
                  Code: [Select]
                  @echo off
                  for %%G in (%*) do echo %%G

                  Salmon Trout

                  • Guest
                  Re: get the sum of %errorlevel% at the end of a subroutine.
                  « Reply #16 on: February 23, 2012, 05:58:59 AM »
                  Yes, I know about SHIFT in cmd and bat scripts. I was commenting on VBScript's lack of restriction on the number of command parameters.

                  osvikvi

                    Topic Starter


                    Rookie

                    Thanked: 2
                    • Experience: Experienced
                    • OS: Windows XP
                    Re: get the sum of %errorlevel% at the end of a subroutine.
                    « Reply #17 on: February 23, 2012, 07:09:32 AM »
                    I hope it's not bad manners of me to jump in here
                    Not at all, you're very welcome...

                    Now, If I add this code to my vbs file, I get an error on the first character. I presume I have to call for it. (I'm a complete newbie with vbs)
                    Quote
                    Code: [Select]
                    WScript.Arguments(0)
                    WScript.Arguments(1)
                    WScript.Arguments(2)

                    will this solution take the existing parameters (from colum 1 and colum 2 in testip.txt) or do I have to duplicate this list?

                    Salmon Trout

                    • Guest
                    Re: get the sum of %errorlevel% at the end of a subroutine.
                    « Reply #18 on: February 23, 2012, 08:04:20 AM »
                    show your vbs file

                    osvikvi

                      Topic Starter


                      Rookie

                      Thanked: 2
                      • Experience: Experienced
                      • OS: Windows XP
                      Re: get the sum of %errorlevel% at the end of a subroutine.
                      « Reply #19 on: February 23, 2012, 08:07:18 AM »
                      Code: [Select]
                      Option Explicit
                      Dim oShell, retCode
                      Set oShell = WScript.CreateObject("WScript.Shell")
                      WScript.Arguments(0)
                      WScript.Arguments(1)
                      WScript.Arguments(2)

                      retCode = oShell.Popup("Certain devices did not respond." & vbNewLine & "Open Logfile?", 10, "PINGME: Packets lost", 4 + 32)

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

                      Salmon Trout

                      • Guest
                      Re: get the sum of %errorlevel% at the end of a subroutine.
                      « Reply #20 on: February 23, 2012, 08:10:51 AM »
                      where is %erry% ?

                      osvikvi

                        Topic Starter


                        Rookie

                        Thanked: 2
                        • Experience: Experienced
                        • OS: Windows XP
                        Re: get the sum of %errorlevel% at the end of a subroutine.
                        « Reply #21 on: February 23, 2012, 08:35:54 AM »
                        I think we're not on the same wavelength....

                        Do I have to refer to %erry% in the vbs script?
                        I think not, I want to get %1 and %2

                        Code: [Select]
                        :Sub
                        ping -n 1 %1
                        IF %ERRORLEVEL% EQU 1 (
                        ECHO    **  # %1 - %2 is not responding ** >> %tnm%
                        set erry=1
                        )

                        then after the subroutine has finished I use %erry% to launch the vb script. Like so:
                        Code: [Select]
                        IF "%erry%"=="1" (
                        mplayer2.exe /minimized /play /close "dingdong.wav"
                        cscript //nologo yesno.vbs "p1" "p2" "p3"
                        )

                        Salmon Trout

                        • Guest
                        Re: get the sum of %errorlevel% at the end of a subroutine.
                        « Reply #22 on: February 23, 2012, 08:43:56 AM »
                        Code: [Select]
                        IF "%erry%"=="1" (
                           mplayer2.exe /minimized /play /close "dingdong.wav"
                           cscript //nologo yesno.vbs "%erry%"
                           )

                        Code: [Select]
                        Option Explicit
                        Dim oShell, retCode
                        Set oShell = WScript.CreateObject("WScript.Shell")
                        ErrCode=WScript.Arguments(0)

                        retCode = oShell.Popup("Certain devices did not respond." & vbNewLine & "Error code: " & ErrCode & vbNewLine & "Open Logfile?", 10, "PINGME: Packets lost", 4 + 32)

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

                        Why do you need to pass %erry% to your script? You know it is 1 already.

                        osvikvi

                          Topic Starter


                          Rookie

                          Thanked: 2
                          • Experience: Experienced
                          • OS: Windows XP
                          Re: get the sum of %errorlevel% at the end of a subroutine.
                          « Reply #23 on: February 23, 2012, 08:53:06 AM »
                          Sorry it seems I have put my question wrong.

                          what I actually meant was:
                          can the messagebox, which I call with the vbs script, display which ip(%1) and devicename(%2) where not responding to the ping.

                          Salmon Trout

                          • Guest
                          Re: get the sum of %errorlevel% at the end of a subroutine.
                          « Reply #24 on: February 23, 2012, 09:12:34 AM »
                          Code: [Select]
                          IF "%erry%"=="1" (
                             mplayer2.exe /minimized /play /close "dingdong.wav"
                             cscript //nologo yesno.vbs "%2" "%1"
                             )

                          Code: [Select]
                          Option Explicit
                          Dim oShell, retCode
                          Set oShell = WScript.CreateObject("WScript.Shell")
                          DevName    = WScript.Arguments(0)
                          IpAddress  = WScript.Arguments(1)

                          retCode = oShell.Popup("Certain devices did not respond." & vbNewLine & "Device name: " & DevName & vbNewLine & "IP Address: " & IpAddress & VbNewLine & "Open Logfile?", 10, "PINGME: Packets lost", 4 + 32)

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

                          osvikvi

                            Topic Starter


                            Rookie

                            Thanked: 2
                            • Experience: Experienced
                            • OS: Windows XP
                            Re: get the sum of %errorlevel% at the end of a subroutine.
                            « Reply #25 on: February 24, 2012, 02:40:37 AM »
                            Thanks,

                            but when I run the vbs script, from the batch file, I get this error:
                            Quote
                            D:\scripts\TEST_pingme\yesno.vbs(4, 1) Microsoft VBScript runtime error: Variable is undefined: 'DevName'

                            when I Run it standalone I get an error:
                            Quote
                            Line:4
                            Char:1
                            Error: Subscript out of range
                            Code: 800A0009
                            Source: Microsoft VBScript runtime error

                            Which could be normal (I don't know) because it doesn't know where to get these arguments. Right?

                            Salmon Trout

                            • Guest
                            Re: get the sum of %errorlevel% at the end of a subroutine.
                            « Reply #26 on: February 24, 2012, 02:43:51 AM »
                            Are the variables %1 and %2 blank in the batch file? Post the complete batch file.

                            osvikvi

                              Topic Starter


                              Rookie

                              Thanked: 2
                              • Experience: Experienced
                              • OS: Windows XP
                              Re: get the sum of %errorlevel% at the end of a subroutine.
                              « Reply #27 on: February 24, 2012, 02:50:03 AM »
                              When I was running my full pingtest.bat, I couldn't see where the script got stuck. So I wrote another script just taking the basics to trigger the yesno.vbs

                              Code: [Select]
                              @echo on
                              set fnm=testip.txt
                              pause
                              for /f "skip=5 tokens=1,2 delims=<TAB> eol=;" %%a in (%fnm%) do call :sub %%a %%b
                              pause
                              cscript //nologo yesno.vbs "%2" "%1"
                              pause

                              :sub
                              echo %1 - %2

                              They shouldn't be blank because they are echoed in the command window.

                               

                              Salmon Trout

                              • Guest
                              Re: get the sum of %errorlevel% at the end of a subroutine.
                              « Reply #28 on: February 24, 2012, 02:59:31 AM »
                              You need to call the vbs in the sub, %1 and %2 do not exist in the main part.


                              BC_Programmer


                                Mastermind
                              • Typing is no substitute for thinking.
                              • Thanked: 1140
                                • Yes
                                • Yes
                                • BC-Programming.com
                              • Certifications: List
                              • Computer: Specs
                              • Experience: Beginner
                              • OS: Windows 11
                              Re: get the sum of %errorlevel% at the end of a subroutine.
                              « Reply #29 on: February 24, 2012, 05:47:43 AM »
                              Option Explicit
                              Dim oShell, retCode
                              Set oShell = WScript.CreateObject("WScript.Shell")
                              DevName    = WScript.Arguments(0)
                              IpAddress  = WScript.Arguments(1)

                              Either remove the "Option Explicit" or change the Dim line to read:

                              Code: [Select]
                              Dim oShell, retCode,DevName,IpAddress

                              I was trying to dereference Null Pointers before it was cool.

                              Salmon Trout

                              • Guest
                              Re: get the sum of %errorlevel% at the end of a subroutine.
                              « Reply #30 on: February 24, 2012, 05:54:35 AM »
                              Option Explicit

                              To my shame, I did not spot that.

                              osvikvi

                                Topic Starter


                                Rookie

                                Thanked: 2
                                • Experience: Experienced
                                • OS: Windows XP
                                Re: get the sum of %errorlevel% at the end of a subroutine.
                                « Reply #31 on: February 24, 2012, 07:49:18 AM »
                                So I changed the Dim line as suggested, Thanks for that BC_Programmer.

                                When I run the test script (like the code in post 128525)
                                the vbs messagebox pops up but it doesn't show which devices didn't respond

                                You need to call the vbs in the sub, %1 and %2 do not exist in the main part.

                                you're right and wrong...
                                If I call it in the sub the message pops-up every cycle and this is what I want to avoid. (I want one box showing me where it all went wrong and giving me the option to show the complete log)

                                -However when I do so, it shows me which devices triggered the %erry%. (So actually you're right twice and I just stumbled into another problem.

                                « Last Edit: February 24, 2012, 08:25:36 AM by osvikvi »

                                osvikvi

                                  Topic Starter


                                  Rookie

                                  Thanked: 2
                                  • Experience: Experienced
                                  • OS: Windows XP
                                  Re: get the sum of %errorlevel% at the end of a subroutine.
                                  « Reply #32 on: February 29, 2012, 01:22:59 AM »
                                  bump -

                                  Anymore suggestions on how to make %1 and %2 available in the mainpart?

                                  Salmon Trout

                                  • Guest
                                  Re: get the sum of %errorlevel% at the end of a subroutine.
                                  « Reply #33 on: February 29, 2012, 10:51:25 AM »
                                  Anymore suggestions on how to make %1 and %2 available in the mainpart?

                                  for /f "skip=5 tokens=1,2 delims=<TAB> eol=;" %%a in (%fnm%) do (
                                      set v1=%%a
                                      set v2=%%b
                                      )


                                  osvikvi

                                    Topic Starter


                                    Rookie

                                    Thanked: 2
                                    • Experience: Experienced
                                    • OS: Windows XP
                                    Re: get the sum of %errorlevel% at the end of a subroutine.
                                    « Reply #34 on: March 01, 2012, 01:30:41 AM »
                                    Thanks Salmon Trout,

                                    Where should I place this, I reckon it loops through fnm and will set column A to v1 and column B to v2. Meaning I can call v1 and it will give me a list of all items in that column?

                                    And where should i place this? If I place it above the first loop and call the vbs script from the :sub. It results in showing me the same IP and devname over and over again. When I call the vbs after the :sub, I receive only one popup but it lists only one ip-address, while there should be 4.

                                    Is it correct that I call the vbs with the parameters like this:
                                    Code: [Select]
                                    cscript //nologo yesno.vbs "%v2%" "%v1%"
                                    Excuse me, I'm a novice!

                                    osvikvi

                                      Topic Starter


                                      Rookie

                                      Thanked: 2
                                      • Experience: Experienced
                                      • OS: Windows XP
                                      Re: get the sum of %errorlevel% at the end of a subroutine.
                                      « Reply #35 on: March 07, 2012, 01:18:22 AM »
                                      bump...