 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
TDMedia
Joined: 26 Nov 2005 Posts: 196
|
Posted: Mon Jun 05, 2006 2:44 am Post subject: Two simple functions (GetTextLines and Ping) |
|
|
Hey everyone
Just thought I'd share a couple of functions that I've made to simplify my own script-making process. The one thing that I've really needed for a long time is a ping command, so a function to do that is included. The other one is just a really simple line counter for text files. Hopefully they'll be helpful to someone. I searched for functions that do this already, but didn't find any, so sorry if something like this is already around.
| Code: | ; GetTextLines
; Gets the number of lines in a specified text file.
; Usage: GetTextLines(FilePath)
GetTextLines(FilePath)
{
Loop, Read, %FilePath%
TxtLength := A_Index
Return TxtLength
}
; Ping
; Pings a specified site, storing resulting stats to variables.
; SiteOrIP is the site's domain name or IP address.
; AverageVar, MinimumVar, and MaximumVar will store those respective stats.
; StatusVar will store either SUCCESS, ERROR (No response), TIMEOUT (one or more pings got no reply), DNS (could not find host),
; DNS-AS (could not find host, but ping successful on the alternate IP - implies a DNS problem),
; DNS-AF (could not find host, and no response from alternate IP - implies that the connection is dead.)
; LossVar will store how many packets were lost during the ping operation (unless there is no response.)
; Use AltIP to specify an IP address to ping if the domain name could not be resolved. If SiteOrIP is already an IP, this is useless.
; Include the PingCount parameter to specify the number of pings to send.
; In this case, the AverageVar variable contains the avarage response time.
; Usage: Ping(SiteOrIP, AverageVar, MinimumVar, MaximumVar, StatusVar, LossVar[, PingCount, AltIP])
Ping(SiteOrIP, ByRef AverageVar, ByRef MinimumVar, ByRef MaximumVar, ByRef StatusVar, ByRef LossVar, PingCount = 1, AltIP = 0, Timeout = 0)
{
If Timeout = 0
Timeout = 300
RunWait, %comspec% /c ping %SiteOrIP% -n %PingCount% -w %Timeout% > pingtemp.txt,, Hide
LineNum := Pingcount + 8
LossLineNum := PingCount + 6
FileReadLine, PingResult, pingtemp.txt, %LineNum%
FileRead, ReplyResult, *t pingtemp.txt
If AltIP = 0
AltIPVar = 0
Else
AltIPVar = 1
IfInString, ReplyResult, could not find host
{
StatusVar = DNS
LossVar = %PingCount%/%PingCount%
If AltIPVar = 1
{
RunWait, %comspec% /c ping %AltIP% -n 1 > pingtempalt.txt,, Hide
FileRead, AltIPResult, pingtempalt.txt
IfInString, AltIPResult, Reply from
StatusVar = DNS-AS
Else
StatusVar = DNS-AF
FileDelete, pingtempalt.txt
}
}
Else IfNotInString, ReplyResult, Reply from
{
StatusVar = ERROR
LossVar = %PingCount%/%PingCount%
}
Else IfInString, ReplyResult, Request timed out
{
StatusVar = TIMEOUT
FileReadLine, PingLoss, pingtemp.txt, %LossLineNum%
StringSplit, PingAvg, PingResult, =
StringSplit, PingStats, PingResult, %A_Space%
StringTrimLeft, PingAvg, PingAvg4, 1
StringTrimRight, PingAvg, PingAvg, 1
StringTrimRight, PingMin, PingStats7, 1
StringTrimRight, PingMax, PingStats10, 1
AverageVar = %PingAvg%
MinimumVar = %PingMin%
MaximumVar = %PingMax%
StringSplit, PingLoss, PingLoss, %A_Space%
LossVar = %PingLoss14%/%PingCount%
}
Else
{
StringSplit, PingAvg, PingResult, =
StringSplit, PingStats, PingResult, %A_Space%
StringTrimLeft, PingAvg, PingAvg4, 1
StringTrimRight, PingAvg, PingAvg, 1
StringTrimRight, PingMin, PingStats7, 1
StringTrimRight, PingMax, PingStats10, 1
AverageVar = %PingAvg%
MinimumVar = %PingMin%
MaximumVar = %PingMax%
StatusVar = SUCCESS
LossVar = 0/%PingCount%
}
FileDelete, pingtemp.txt
Return
} |
Last edited by TDMedia on Wed Aug 01, 2007 4:50 am; edited 9 times in total |
|
| Back to top |
|
 |
Areilius
Joined: 16 Feb 2006 Posts: 42
|
Posted: Mon Jun 05, 2006 6:29 am Post subject: |
|
|
Handy functions!
A suggestion regarding:
| Quote: | ; Remember not to use any output variable names that are used in the functions.
; For example, in Ping, don't make your minimum variable "MinimumVar".
; The functions will not put out the variables if you do. |
Using Byref will solve this problem.
Example:
| Code: | GetTextLines(FilePath, Byref OutputVar)
{
Loop, Read, %FilePath%
TxtLength+=1
OutputVar = %TxtLength%
} |
This also makes it possible for another function to call this function and use a local variable as the OutputVar.
That said, it would be even better to make the function return the line count, since there is only one value to return. Eg:
| Code: | LineCount := GetTextLines("C:\test.txt")
return
GetTextLines(FilePath)
{
Loop, Read, %FilePath%
TxtLength+=1
return TxtLength
} |
|
|
| Back to top |
|
 |
toralf
Joined: 31 Jan 2005 Posts: 3910 Location: Bremen, Germany
|
Posted: Mon Jun 05, 2006 8:37 am Post subject: |
|
|
Could I please ask you to change the title of your threat to a more specific one? Thanks _________________ Ciao
toralf  |
|
| Back to top |
|
 |
PhiLho
Joined: 27 Dec 2005 Posts: 6836 Location: France (near Paris)
|
Posted: Mon Jun 05, 2006 8:44 am Post subject: Re: Two simple functions |
|
|
| TDMedia wrote: | | Code: | GetTextLines(FilePath, OutputVar)
{
Loop, Read, %FilePath%
TxtLength+=1
%OutputVar% = %TxtLength%
}
|
| To increment a variable, you can write simply TxtLength++, but remember that A_Index holds the loop count, so you can simplify this function as follow:
| Code: | GetTextLines(FilePath, OutputVar)
{
Loop, Read, %FilePath%
%OutputVar% := A_Index
} |
But I don't see why you don't use the capability of a function to return a value, that would be much easier to use:
| Code: | GetTextLines(FilePath)
{
Loop, Read, %FilePath%
OutputVar := A_Index
Return OutputVar
} | Use it as lineNb := GetTextLines(FilePath) _________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2") |
|
| Back to top |
|
 |
TDMedia
Joined: 26 Nov 2005 Posts: 196
|
Posted: Tue Jun 06, 2006 12:49 am Post subject: |
|
|
The reson that I didn't make GetTextLines return is that I wrote Ping first, and I was in the habit of assigning variables instead of returning. I'll fix it a little later. Also, Areilus: thanks for that ByRef comment. I was hoping that there was some way to accomplish that.  |
|
| Back to top |
|
 |
TDMedia
Joined: 26 Nov 2005 Posts: 196
|
Posted: Tue Jun 06, 2006 6:13 am Post subject: |
|
|
Ok - fixed the GetTextLines function to return it's value, and made the ByRef fix - thanks.  |
|
| Back to top |
|
 |
TDMedia
Joined: 26 Nov 2005 Posts: 196
|
Posted: Tue Jun 13, 2006 7:20 am Post subject: |
|
|
| 6/13/06 - Just made an update to the ping function to give a good deal more information. |
|
| Back to top |
|
 |
userabuser
Joined: 15 Jul 2006 Posts: 31
|
Posted: Thu Aug 24, 2006 5:22 am Post subject: |
|
|
This works nicely with this example usage. Thanks for posting!
| Code: |
Ping("google.com", Average, Minimum, Maximum, Status, Loss, "4, """,1500)
MsgBox, Average response = %Average%`nMinimum response = %Minimum%`nMaximum response = %Maximum%`nResult = %Status%`nLoss = %Loss% |
Note the timeout. Sattitlite's suck!  |
|
| Back to top |
|
 |
Guest
|
Posted: Thu Aug 24, 2006 9:05 am Post subject: |
|
|
| toralf wrote: | | Could I please ask you to change the title of your threat to a more specific one? Thanks |
"threat"? -> Bedrohung
I suppose you meant "thread"  |
|
| Back to top |
|
 |
robiandi
Joined: 08 Aug 2006 Posts: 49
|
Posted: Thu Aug 24, 2006 1:10 pm Post subject: |
|
|
I have Win2K, German. In the Function ping the following changes are necessary for the german language: | Quote: | could not find host -> Unbekannter Host
Reply from -> Antwort von
Request timed out -> berschreitung der Antwort
StringTrimRight, PingMax, PingStats10, 1 -> StringTrimRight, PingMax, PingStats11, 1
|
( berschreitung der Antwort means: Zeitüberschreitung der Antwort but AutoHotkey doesn't like ü ) |
|
| Back to top |
|
 |
TDMedia
Joined: 26 Nov 2005 Posts: 196
|
Posted: Fri Aug 25, 2006 1:24 am Post subject: |
|
|
Thanks for that conversion work, robiandi  |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|