Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Find random number in .txt file, divide it, and display in msbox


  • Please log in to reply
12 replies to this topic
crazymonkeh
  • Members
  • 9 posts
  • Last active: Nov 11 2015 06:56 PM
  • Joined: 05 Jul 2014

I'm trying to obtain the download rate using netstat -e in command, and divide by 1000 to obtain the result in KB.

I'm using a script I managed to find on the forums, created by "TLM" admin? located here:
http://autohotkey.co...-low-bandwidth/

nstFile = % A_Desktop "\bytes.txt"

Gui, Add, Text,, Current Bandwidth:
Gui, Add, Text, W140 vbytes, Loading...
Gui, Show, NA, BW

Loop,
{
    RunWait %comspec% /c ""Netstat" "-e" >"%nstFile%"",, Hide
    FileReadLine, bytesLine, % nstFile, 5
	StringReplace, bytesLine, bytesLine, Bytes,
	bytesData = %bytesLine%
	StringSplit, bytesData, bytesData, % A_Space
	    GuiControl, Text, bytes, % "In Bytes: " bytesData1
}

GuiClose:
ExitApp

It works, but displays the result in bytes. I've tried to modify the result like this, but failed:

nstFile = % A_Desktop "\bytes.txt"

Gui, Add, Text,, Current Bandwidth:
Gui, Add, Text, W140 vbytes, %bytesData%
Gui, Show,

Loop,
{
    RunWait %comspec% /c ""Netstat" "-e" >"%nstFile%"",, Hide
    FileReadLine, bytesLine, % nstFile, 5
	StringReplace, bytesLine, bytesLine, Bytes,

	bytesData := bytesLine/1000

	StringSplit, bytesData, bytesData, % A_Space
	    GuiControl 1: , bytes, %bytesData%
}
GuiClose:
ExitApp

I'm not very competant when it comes to complex scripting. I've tried to modify the script in a variety of ways from any data I could find, and failed. :(

The ultimate goal is to refresh the file every second and obtain the KB rate per second through some sort of mathematic equasion. I'll cross that bridge when I can get past this hickup first.

 

Help is appeciated, thanks in advance.



Xtra
  • Members
  • 954 posts
  • Last active: Jul 23 2016 09:04 PM
  • Joined: 29 Sep 2013

StringSplit returns a pseudo array as you can see in the 1st script using bytesData1

 

change your edited script :

	StringSplit, bytesData, bytesData, % A_Space
	    GuiControl 1: , bytes, %bytesData1%

HTH



crazymonkeh
  • Members
  • 9 posts
  • Last active: Nov 11 2015 06:56 PM
  • Joined: 05 Jul 2014

StringSplit returns a pseudo array as you can see in the 1st script using bytesData1

 

change your edited script :

	StringSplit, bytesData, bytesData, % A_Space
	    GuiControl 1: , bytes, %bytesData1%

HTH

 

Thanks for the swift reply.

I don't get any errors when I run the script, but there's no result or number shown in the GUI.

 

It's the same issue that's been occurring everytime I modify it. There's gotta be something I'm missing.



Masonjar13
  • Members
  • 1517 posts
  • Last active:
  • Joined: 16 Sep 2012

Working.

nstFile = % A_Desktop "\bytes.txt"
Gui, Add, Text,, Current Bandwidth:
Gui, Add, Text, W140 vbytes, Loading...
Gui, Show, NA, BW
Loop,
{
    RunWait %comspec% /c ""Netstat" "-e" >"%nstFile%"",, Hide
    FileReadLine, bytesLine, % nstFile, 5
    regExMatch(bytesLine,"Bytes\s+\K\d+",BytesData)
    GuiControl, Text, bytes, % "In Bytes: " BytesData//1000
}
GuiClose:
ExitApp

However, this isn't the active bandwidth, but bytes received (over x-time?). You'll notice the number never goes down. I don't know about Netstat though, never used it before.
I'd also recommend changing A_Desktop to A_Temp and have it deleted on exit.

Edit:
Quick transposition:

#singleinstance force
nstFile:=a_temp "\bytes.txt"
onExit,cleanup

;gui,add,text,,Current Bandwidth:
gui,add,text,w140 vbytes,Loading...
gui,show,NA,Received MB
setTimer,updateBytes,10
return

updateBytes:
runWait %comspec% /c ""Netstat" "-e" >"%nstFile%"",,hide
fileReadLine,bytesLine,% nstFile,5
regExMatch(bytesLine,"Bytes\s+\K\d+",bytesData)
guiControl,text,bytes,% "In Bytes: " bytesData//1000
return

guiClose:
exitApp

cleanup:
fileDelete,% nstFile
exitApp

OS: Windows 7 Ultimate / Windows 8.1 Pro | Editor: Notepad++


crazymonkeh
  • Members
  • 9 posts
  • Last active: Nov 11 2015 06:56 PM
  • Joined: 05 Jul 2014

Wow, thanks Masonjar13, it works quite well.

I've also noticed that the byte count in the file never goes down.

I can probably identify the rate at which it goes up and calculate that in KB per second using mathematical equasions.

 

That'll be my next step.



Masonjar13
  • Members
  • 1517 posts
  • Last active:
  • Joined: 16 Sep 2012

It sounded like fun, so..

#singleinstance force
nstFile:=a_temp "\bytes.txt"
onExit,cleanup

gui,add,text,,Current Bandwidth:
gui,add,text,w140 vbytes,Loading...
gui,+alwaysOnTop
gui,show,NA,Bandwidth Meter (KB)
setTimer,updateBytes,10
return

updateBytes:
runWait %comspec% /c Netstat -e >"%nstFile%",,hide
fileReadLine,bytesLine,% nstFile,5
regExMatch(bytesLine,"Bytes\s+\K\d+",bytesData)
bandwidth:=prevBytesData?bytesData-prevBytesData:""
prevBytesData:=bytesData
guiControl,text,bytes,% "In Bytes: " bandwidth//1000
return

guiClose:
exitApp

cleanup:
fileDelete,% nstFile
exitApp

OS: Windows 7 Ultimate / Windows 8.1 Pro | Editor: Notepad++


crazymonkeh
  • Members
  • 9 posts
  • Last active: Nov 11 2015 06:56 PM
  • Joined: 05 Jul 2014

 

It sounded like fun, so..

#singleinstance force
nstFile:=a_temp "\bytes.txt"
onExit,cleanup

gui,add,text,,Current Bandwidth:
gui,add,text,w140 vbytes,Loading...
gui,+alwaysOnTop
gui,show,NA,Bandwidth Meter (KB)
setTimer,updateBytes,10
return

updateBytes:
runWait %comspec% /c Netstat -e >"%nstFile%",,hide
fileReadLine,bytesLine,% nstFile,5
regExMatch(bytesLine,"Bytes\s+\K\d+",bytesData)
bandwidth:=prevBytesData?bytesData-prevBytesData:""
prevBytesData:=bytesData
guiControl,text,bytes,% "In Bytes: " bandwidth//1000
return

guiClose:
exitApp

cleanup:
fileDelete,% nstFile
exitApp

 

I understand the idea behind that equasion, i just don't understand the coding used.

Generates an error on line 16:

 

"Error: A ":" missing its "?""

 

bandwidth:=prevBytesData?bytesData-prevBytesData:""

I have no idea what that means. I'll fiddle with it a bit.



crazymonkeh
  • Members
  • 9 posts
  • Last active: Nov 11 2015 06:56 PM
  • Joined: 05 Jul 2014

I came up with this by modifying slightly.

However the result was unexpected. The number just jumps between 0-20k+ depending on the refresh timer. (Just seemed really inaccurate)

 

I modified the update timer to 1000 to refresh every second, and the result seems more plausible.

 

I might also look into other, more efficient ways.

#singleinstance force
nstFile:="C:\bytes.txt"
onExit,exit

Gui, Show, h100 w300, Bandwidth
Gui, Add, Progress, x50 y50 h15 w200 clime vbytesbar backgroundgray Range0-25
gui,add,text, x130 y70 h15 w140 vbytes,Loading...
setTimer,updateBytes,500
return

updateBytes:
    runWait %comspec% /c Netstat -e >"%nstFile%",,hide
    fileReadLine,bytesLine,% nstFile,5
    regExMatch(bytesLine,"Bytes\s+\K\d+",bytesData)
    bandwidth:=bytesData-prevBytesData
    prevBytesData:=bytesData
    guiControl,text,bytes,% bandwidth//1000 "Kbps"
    guiControl 1: , bytesbar,% bandwidth//1000000
    return

exit:
guiClose:
    FileDelete, C:\bytes.txt
    exitApp

The Progress bar would show the results in Mbps with a max of 25mbps.

I need to make an upload meter for the "Sent" bytes.

I need to read up on RegExMatch.



Masonjar13
  • Members
  • 1517 posts
  • Last active:
  • Joined: 16 Sep 2012

What vesion of AHK are you using and what encoding? You should be using AHK Unicode 1.1.2*.* with UTF8 with BOM. I say this, because I don't encounter the same error.


OS: Windows 7 Ultimate / Windows 8.1 Pro | Editor: Notepad++


crazymonkeh
  • Members
  • 9 posts
  • Last active: Nov 11 2015 06:56 PM
  • Joined: 05 Jul 2014

1.1.21 so it should work.

 

When I changed:

bandwidth:=prevBytesData?bytesData-prevBytesData:""

To:

bandwidth:=bytesData-prevBytesData

I don't know if or how it impacts the results, but the result seems viable.



Exaskryz
  • Members
  • 3249 posts
  • Last active: Nov 20 2015 05:30 AM
  • Joined: 23 Aug 2012

The first code that MasonJar originally had was ternary. It reads "If prevBytesData [has a value], then assign bandwidth the value of bytesData-prevBytesData. Else, bandwidth should get the value null."

 

Going through that, it sounds like it's unnecessary to do ternary there because if prevBytesData has no value, it'll be treated as zero, and bytesData-0 is bytesData. However, the end result is different: bandwidth in MasonJar's code has no value, while your change crazymonkeh gives bandwidth a value at all times.

 

I did not look back at Masonjar's original code to get context.



crazymonkeh
  • Members
  • 9 posts
  • Last active: Nov 11 2015 06:56 PM
  • Joined: 05 Jul 2014

Would something like this work to find the "Sent" bytes in a file that looks like this:

Interface Statistics

                           Received            Sent

Bytes                    1510341744       132712600

The Code i'm trying to use would implement {min, max} as explained in this Doc:

http://ahkscript.org...Ex-QuickRef.htm

fileReadLine,uploadLine,% nstFile,5
    regExMatch(uploadLine,"0-9{35,51}", uploadData)

The idea is to find any digits between 0 and 9 starting after the 35th character in the line, not exceeding the 51st character because there's nothing after that?

 

Somehow I get the idea that I did it wrong. I have yet to test it.

:



crazymonkeh
  • Members
  • 9 posts
  • Last active: Nov 11 2015 06:56 PM
  • Joined: 05 Jul 2014

Nvm I think I've found everything I need.

I ran several test with this function. Although not optimal, it does the trick:

	fileReadLine,uploadLine,% nstFile,5
	regExMatch(uploadLine,"Bytes\s+\d+\s+\K[0-9]+", uploadData)
	uploadSpeed:=uploadData-prevUploadData
	prevUploadData:=uploadData
	guiControl,text,upload,% uploadSpeed//1000 " Kbps"

It captures the Sent bytes accurately enough to fullfill its purpose.

If someone knows a better/more efficient way to do this, please make yourselves known. Thanks Masonjar13 and everyone else who helped.