AutoHotkey Community

It is currently May 27th, 2012, 9:27 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 11 posts ] 
Author Message
PostPosted: June 5th, 2006, 3:44 am 
Offline

Joined: November 26th, 2005, 10:35 pm
Posts: 196
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 August 1st, 2007, 5:50 am, edited 9 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 5th, 2006, 7:29 am 
Offline

Joined: February 16th, 2006, 8:29 am
Posts: 42
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
   }


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 5th, 2006, 9:37 am 
Offline

Joined: January 31st, 2005, 9:50 am
Posts: 3910
Location: Bremen, Germany
Could I please ask you to change the title of your threat to a more specific one? Thanks

_________________
Ciao
toralf
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Re: Two simple functions
PostPosted: June 5th, 2006, 9:44 am 
Offline

Joined: December 27th, 2005, 1:46 pm
Posts: 6837
Location: France (near Paris)
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)

_________________
Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 6th, 2006, 1:49 am 
Offline

Joined: November 26th, 2005, 10:35 pm
Posts: 196
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. :)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 6th, 2006, 7:13 am 
Offline

Joined: November 26th, 2005, 10:35 pm
Posts: 196
Ok - fixed the GetTextLines function to return it's value, and made the ByRef fix - thanks. :)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 13th, 2006, 8:20 am 
Offline

Joined: November 26th, 2005, 10:35 pm
Posts: 196
6/13/06 - Just made an update to the ping function to give a good deal more information.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 24th, 2006, 6:22 am 
Offline

Joined: July 15th, 2006, 7:36 pm
Posts: 31
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! :)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 24th, 2006, 10:05 am 
toralf wrote:
Could I please ask you to change the title of your threat to a more specific one? Thanks

"threat"? -> Bedrohung :shock:
I suppose you meant "thread" :wink:


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: August 24th, 2006, 2:10 pm 
Offline

Joined: August 8th, 2006, 3:55 pm
Posts: 49
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 ü )


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 25th, 2006, 2:24 am 
Offline

Joined: November 26th, 2005, 10:35 pm
Posts: 196
Thanks for that conversion work, robiandi :)


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot], nomissenrojb, Stigg and 12 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group