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 11825 times)

0 Members and 1 Guest are viewing this topic.

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.