Page 1 of 1

My 1st batch to show whats accessing the internet

Posted: 04 May 2015 18:39
by alowe
My internet bandwidth is very limited so it annoys me when something in the background is eating it up and I don't know what it is. I thought, why download some software to tell me when XP already has that functionality? Also I wanted it to tell me the info with a single mouse click.

So, I wrote this little batch (called GetPids.bat) and added it to the quicklaunch.

Code: Select all

@echo off
setlocal enabledelayedexpansion

:: get rid of MpCmdRun.exe while we're at it
:start
taskkill /f /t /im mpcmdrun.exe
cls
ping 1.1.1.1 -n 1 -w 0.5 >nul
cls

:: display pids with net connection
netstat -o -n >pid.txt
type pid.txt
echo.
echo Process Name                 PID Session Name        Session#  Mem Usage Count
echo Please Wait...
for /f "skip=4 tokens=5*" %%i in (pid.txt) do tasklist /nh /fi "pid eq %%i" >>out.txt
cursorpos 0 -1

:: display process names removing duplicates
if not exist out.txt goto :prompt
echo.              >index.dat
for /f "tokens=*" %%i in (out.txt) do if "%%i" neq "!pid!" (
   set pid=%%i
   find "%%i" index.dat >nul
   if errorlevel 1 (
      find /c "%%i" out.txt >out2.txt
      for /f "tokens=3" %%i in (out2.txt) do set count=%%i
      echo !pid!  [!count!] >> index.dat
      set /a total=!total!+!count! ) )
type index.dat
echo                                                                   Total  [!total!]

:: clean up and repeat?
:prompt
echo.
set pid=
set count=
set total=
del /q pid.txt out.txt out2.txt index.dat
set /p temp="<- Return = Refresh ->"
goto :start


Yes I know my formatting is awful. It's my style. I hate lines that only have a ) or a (. They seem redundant to me.

The reason I clean up the environment variables at the end is cause if that doesn't happen the output gets messed up. If it wasn't looping the "set var=" lines could be removed as I noticed XP doesn't keep environment variables from one session of cmd to another.

The hardest bit was getting the "Please Wait..." line to be overwritten. Spent the whole day reading up and trying various solutions. None of them worked so I compromised by using Aacini's CursorPos.exe auxiliary program. So it's not pure XP cmd DOS but then it's emulated so not pure DOS anyway.

Let me know whatcha think of it

Re: My 1st batch to show whats accessing the internet

Posted: 05 May 2015 14:34
by josephf
alowe wrote:The hardest bit was getting the "Please Wait..." line to be overwritten. Spent the whole day reading up and trying various solutions. None of them worked so I compromised by using Aacini's CursorPos.exe auxiliary program. So it's not pure XP cmd DOS but then it's emulated so not pure DOS anyway.
Let me know whatcha think of it


Thanks for your contribution! I'm not being critical, I'll save that for others :) But, taking a couple lines from aGermans post in this thread http://www.dostips.com/forum/viewtopic.php?f=3&t=6362 allows you to use set /p to display the "Please Wait..." sans CR/LF. Then creating backspace characters and deleting what was written. Hence no need for another program.

Code: Select all

@echo off
setlocal enabledelayedexpansion

:: get rid of MpCmdRun.exe while we're at it
:start
taskkill /f /t /im mpcmdrun.exe
cls
ping 1.1.1.1 -n 1 -w 0.5 >nul
cls

:: display pids with net connection
netstat -o -n >pid.txt
type pid.txt
echo(
<nul set /p "=Please Wait..."


for /f "skip=4 tokens=5*" %%i in (pid.txt) do tasklist /nh /fi "pid eq %%i" >>out.txt

:: create a backspace character
for /f %%i in ('"prompt $H &for %%b in (1) do rem"') do set "bs=%%i"
for /l %%i in (1,1,14) do (<nul set /p "=%bs%")

:: display process names removing duplicates
echo Process Name                 PID Session Name        Session#  Mem Usage Count
if not exist out.txt goto :prompt
echo(              >index.dat
for /f "tokens=*" %%i in (out.txt) do if "%%i" neq "!pid!" (
   set pid=%%i
   find "%%i" index.dat >nul
   if errorlevel 1 (
      find /c "%%i" out.txt >out2.txt
      for /f "tokens=3" %%i in (out2.txt) do set count=%%i
      echo !pid!  [!count!] >> index.dat
      set /a total=!total!+!count! ) )
type index.dat
echo                                                                   Total  [!total!]

:: clean up and repeat?
:prompt
echo(
set pid=
set count=
set total=
del /q pid.txt out.txt out2.txt index.dat
set /p temp="<- Return = Refresh ->"
goto :start

Re: My 1st batch to show whats accessing the internet

Posted: 05 May 2015 15:01
by ShadowThief
alowe wrote:I know my formatting is awful.

Looks fine to me

Re: My 1st batch to show whats accessing the internet

Posted: 05 May 2015 18:04
by alowe
hi josephf,

Depends how one takes the criticism :)

I tried your modified version but the Please Wait... disappears before I have a chance to read it.
I thought maybe you'd made the next table display quicker. So I opened a load of pages at once to create many connections but Please Wait... still disappears too quickly.

I haven't looked at the code to figure out why, cause it's after midnight, but there should be a way to modify it further so the user can see Please Wait... (without CursorPos) while it's formatting the next table, rather than just before it prints.

Because I use a 48k tethered modem to connect to the internet I have enabled 16 connections for every tab. Sometimes the connections can be 100 to 200 it total. This speeds up downloads by stopping pages failing to load because of one failed element. For that reason it helps to say Please Wait... if there's a pause of more than 1 second. People with broadband or better connections might not notice any difference.

Re: My 1st batch to show whats accessing the internet

Posted: 05 May 2015 21:02
by foxidrive
ShadowThief wrote:
alowe wrote:I know my formatting is awful.

Looks fine to me


It's ok here too - the font you use will affect the rendering of the text, alowe.
Perhaps you are not using a mono-spaced font in your console window, like consolas which is what I use here.

Re: My 1st batch to show whats accessing the internet

Posted: 05 May 2015 22:35
by Aacini
alowe wrote:Yes I know my formatting is awful. It's my style. I hate lines that only have a ) or a (. They seem redundant to me.

Well, that formatting style was proposed by structured programming techniques in order to quickly identify if several control constructs are properly nested. For example:

Code: Select all

for /f "tokens=*" %%i in (out.txt) do if "%%i" neq "!pid!" (
   set pid=%%i
   find "%%i" index.dat >nul
   if errorlevel 1 (
      find /c "%%i" out.txt >out2.txt
      for /f "tokens=3" %%i in (out2.txt) do set count=%%i
      echo !pid!  [!count!] >> index.dat
      set /a total=!total!+!count!
   )
)
type index.dat

Previous code allows me to visually know that the ")" placed in the same column of previous "if" closes it, and the same thing happen with the "for" and its closing parentheses at column 1. This is a very simple way to identify errors derived from missing opening/closing parentheses that is particularly useful when the program is very large.

On the other hand, the closing parentheses belongs to the "if" and "for" commands, not to the "set" command where you placed they. If the "set" command would be moved to other place or any command would be inserted after it, you must remember to delete the parentheses from the "set" and insert they in the new command placed at bottom. The structured techniques aids to make these modifications in a simpler way that is less prone to cause errors.

Antonio

Re: My 1st batch to show whats accessing the internet

Posted: 06 May 2015 10:18
by Aacini
alowe wrote:hi josephf,

Depends how one takes the criticism :)

I tried your modified version but the Please Wait... disappears before I have a chance to read it.
I thought maybe you'd made the next table display quicker. So I opened a load of pages at once to create many connections but Please Wait... still disappears too quickly.

I haven't looked at the code to figure out why, cause it's after midnight, but there should be a way to modify it further so the user can see Please Wait... (without CursorPos) while it's formatting the next table, rather than just before it prints.

You may do that this way:

Code: Select all

:: Create a variable with CR (Ascii 13)
for /F %%a in ('copy /Z "%~f0" nul') do set "CR=%%a"

:: display pids with net connection
netstat -o -n >pid.txt
type pid.txt
echo/
echo Process Name                 PID Session Name        Session#  Mem Usage Count
:: Show the message and move the cursor to first column
set /P "=Please Wait...!CR!" < NUL
for /f "skip=4 tokens=5*" %%i in (pid.txt) do tasklist /nh /fi "pid eq %%i" >>out.txt

:: display process names removing duplicates

Antonio

Re: My 1st batch to show whats accessing the internet

Posted: 06 May 2015 12:03
by alowe
re Aacini,

I know about the standard for formatting. Was programming before it was written. I just prefer more compact code as I was used to writing on older platforms with more limited screen space. To me the lines with ) on their own look ugly. I accept that I am in the minority. Maybe I could use Lisp to convert code to the standard style when posting it on a forum.

I do not claim that the ) belongs to the set command even though it is on the same line. The way I know that the for loop and if statements end where they do is by looking at the indentation.

e.g. (without the comments)

Code: Select all

for loop (
   stuff
   stuff
   if condition (
      stuff
      stuff ) )
   :: end of if condition
:: end of for loop


It's an eccentricity I hold onto perhaps at the expense of others reading ease.

As for the font I use for the console that is Raster font 8x12 which is mono-spaced. For notepad I use the System font in bold size 10 which is variably-spaced. There is no option for me to select Raster font for notepad. Not sure why. I've never had an issue with it as it doesn't effect the indentation I use to differentiate program structures. Maybe Raster is not a file font but a font built in to the console. I'm guessing it is internally Rasterised, hence the name.

Re: My 1st batch to show whats accessing the internet

Posted: 06 May 2015 12:18
by alowe
re Aacini,

Tried the latest modification and it still immediately removes the Please Wait... line, even with 42 connections taking over 1 second to display the 2nd table.

It's ok to be honest. I'm happy to use CursorPos as it's such a simple batch that if anyone seriously wants to use it they can probably rewrite it better themselves. I mostly posted it to see if it was a good example of someone's first attempt at a batch.

Also, CursorPos is a total Godsend for anyone wanting to write games for DOS. I don't want to make it look like I'm trying to avoid this very useful program which should have been in the command list from the getgo. I guess Microsoft and the likes never figured DOS would be used for anything other than a blog style output window.

Re: My 1st batch to show whats accessing the internet

Posted: 06 May 2015 21:47
by foxidrive
alowe wrote:It's an eccentricity I hold onto


Yep, that's it. :D

As for the font I use for the console that is Raster font 8x12 which is mono-spaced. For notepad I use the System font in bold size 10 which is variably-spaced. There is no option for me to select Raster font for notepad.


Why did you bring Notepad into this?

You mentioned that the formatting was ugly and so the font in your console was suspected, when it looks ok for a couple of us here - why do you say that that it's ugly on your console window?

Or is it the netstat output that makes you say it's ugly?

Re: My 1st batch to show whats accessing the internet

Posted: 07 May 2015 01:28
by alowe
Oh no sorry I meant the batch code, not the output.

I program the batch using notepad.

Re: My 1st batch to show whats accessing the internet

Posted: 07 May 2015 01:44
by ShadowThief
Honestly, with the exception of not having close parentheses on their own lines and using lower case variables in for loops, it looks almost exactly like how the code would look if I wrote it.

Re: My 1st batch to show whats accessing the internet

Posted: 07 May 2015 04:21
by foxidrive
alowe wrote:Oh no sorry I meant the batch code, not the output.


My mistake.

I program the batch using notepad.


Syntax highlighting in an editor is useful IMO. Notepad is very very basic - no column editing etc.

You'd find one of the free editors useful with regexp and more, if you write a fair amount of text.

Re: My 1st batch to show whats accessing the internet

Posted: 10 May 2015 20:24
by Ed Dyreen
alowe wrote: To me the lines with ) on their own look ugly.
irrelevant, reducing the risk of mistakes caused by poor formatting is important.
alowe wrote:It's an eccentricity I hold onto perhaps at the expense of others reading ease.
and eventually your own

Code: Select all

(
       (
              (
                     (
                            ()
                            ()
                            ()
                     )
              )
              ()
       )
       ()
)

Code: Select all

((((()()()))())())
your argument for violating the FURPS model is worth nothing

Supportability (Serviceability, Maintainability, Sustainability, Repair Speed) - Testability, Flexibility (Modifiability, Configurability, Adaptability, Extensibility, Modularity), Installability, Localizability