1.

Solve : get the sum of %errorlevel% at the end of a subroutine.?

Answer»

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! 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.Who did you recieve a PM from ? ?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?....Do as you like...

I have a good reason for asking.Okay, I'm still not with you but nevertheless...
Do you have any suggestions?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.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! Quote from: osvikvi on February 22, 2012, 09:31:12 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. Keep it clean...we have Members of all ages here. Quote from: osvikvi on February 22, 2012, 09:31:12 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.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 supportNo problem...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 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.


Discussion

No Comment Found