Count how many times a script has been run + loop through

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Johana
Posts: 189
Joined: 02 May 2017, 02:34

Count how many times a script has been run + loop through

20 Nov 2017, 03:30

I have a brilliant (at least in my head) idea!

I have now around 30 scritps that are used through a menu by our entire animal rescure hospice (yes! I AM PROUD!). An idea struck me that I would like to implement. I guess it has todo with IniWrite somehow.

The scripts are run over a network. What I want to accomplish is the following:

For all users:
Each time a script called "myscript" is run, to add a counter to a new file. Since there are 30 scripts I want either each script to create a new file or add to the same file a counter depending on different rows.

This file should be updated no matter who is running a script over the network.


For current user:
Place a file locally with how many times a certain script has been run.

Then I want to place in the menu so that when you click "statistics" you will see how many times overall a script has been run and locally how many times a user has run a script.

Ideas where to start and how?
User avatar
divanebaba
Posts: 805
Joined: 20 Dec 2016, 03:53
Location: Diaspora

Re: Count how many times a script has been run + loop through

20 Nov 2017, 03:52

A brilliant way for your brilliant idea, could be using FileAppend to store timestamps local or anywhere, everytime, you start a script.
FileAppend has to be in a top section, that the script executes at start. You can create csv-like file, with username, scriptname, operatingsystem and so on.
By clicking "statistics", you can read them out and display all timestamps as readable output, or you can count all rows and display only the number of startups.
You can even do it with IniWrite.
Einfach nur ein toller Typ. :mrgreen:
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Count how many times a script has been run + loop through

20 Nov 2017, 04:13

I'd go with CSV 9 of 10 times as it offers the option of being imported/converted to create charts!
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Count how many times a script has been run + loop through

20 Nov 2017, 04:47

You can store the count inside the script file itself, eg, like this, but please, make a backup of any script you try this on, it has been very briefly tested, with example,

Code: Select all

; v1
scriptCount(){
	static count := 0
	static wasIncremented := scriptCount() ; sets the script count
	local scriptCode, oldCount
	if !wasIncremented {
		fileRead scriptCode, % A_ScriptFullPath
		if !scriptCode
			return
		if !regexmatch(scriptCode, "O)\QscriptCount(){\E\s+static count := (\d+)", oldCount)
			return
		if !scriptCode:=regexreplace(scriptCode, oldCount[1], oldCount[1]+1, , 1, oldCount.pos(1))
			return
		fileDelete % A_ScriptFullPath
		fileAppend % scriptCode, % A_ScriptFullPath
		count++
		return true
	}
	return count
}
msgbox % "script run number: " scriptCount()	; gets the script count
To get the count, simply call scriptCount(), to increment the count, the function just needs to be present anywhere in the file.
I guess it will not work on compiled scripts.
ahk v2:

Code: Select all

scriptCount(){
	static count := 0
	static wasIncremented := scriptCount()
	local scriptCode, oldCount
	if !wasIncremented {
		scriptCode := fileRead(A_ScriptFullPath)
		if !scriptCode
			return
		if !regexmatch(scriptCode, "\QscriptCount(){\E\s+static count := (\d+)", oldCount)
			return
		if !scriptCode:=regexreplace(scriptCode, oldCount[1], oldCount[1]+1, , 1, oldCount.pos(1))
			return
		fileDelete(A_ScriptFullPath)
		fileAppend(scriptCode, A_ScriptFullPath)
		count++
		return true
	}
	return count
}
Cheers.
Odlanir
Posts: 659
Joined: 20 Oct 2016, 08:20

Re: Count how many times a script has been run + loop through

20 Nov 2017, 05:41

Another way to accomplish the task.
To avoid concurrent update of a common file for all the network users I'll do this way.
Every time a script is executed it writes ( or renames ) ,in a common network folder, a filename with the following format: scriptname_username_count.txt containing the execution timestamp ( optional ).
I've used this method at my jobsite and I've never get any problem.


In the auto execute section:

Code: Select all

NetworkFolder   := "C:\temp\tmp"  ; replace with your network folder name
ThisScript      := RegExReplace(A_ScriptName, "iDU)^(.*)\.(ahk|exe)$", "$1")
gosub WriteFileFlag
everywhere in your script

Code: Select all

WriteFileFlag:
if fileexist(NetworkFolder "\" ThisScript "_" A_UserName "*") {
   loop, % NetworkFolder "\" ThisScript "_" A_UserName "*" {   
      RegExMatch(A_LoopFileName, "O)(.*)_(\d+)\.txt",tmp)      
      newName := NetworkFolder "\" tmp[1] "_" format("{:04}",tmp[2] + 1) ".txt"
      FileMove, % NetworkFolder "\" A_LoopFileName , % newName      
      FileAppend, % a_now "`n", % newName  ;  <------- optional
   }
} else {
   FileAppend, % a_now "`n", % NetworkFolder "\" ThisScript "_" A_UserName "_" format("{:04}",1) ".txt"
}   
return
You can simply get the script execution statistics by this ...

Code: Select all

GetScriptStats:
   count := 0
   loop, % NetworkFolder "\" ThisScript "*" {
      RegExMatch(A_LoopFileName, "O)(.*)_(\d+)\.txt",tmp)      
      count += tmp[2]
   }
   MsgBox % "The script has been executed : " count " times."
return
... and the user execution statistics by this

Code: Select all

GetUserStats:
   count := 0
   loop, % NetworkFolder "\*_" A_UserName "_*" {
      RegExMatch(A_LoopFileName, "O)(.*)_(\d+)\.txt",tmp)      
      count += tmp[2]
   }
   MsgBox % "The user has executed : " count " scripts."
return
Cheers.
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Count how many times a script has been run + loop through

20 Nov 2017, 05:56

Your approach seems better Odlanir :thumbup: . Thanks for sharing.
Johana
Posts: 189
Joined: 02 May 2017, 02:34

Re: Count how many times a script has been run + loop through

20 Nov 2017, 06:55

Thanks for all the answers! I'll try this and get back to you!
Johana
Posts: 189
Joined: 02 May 2017, 02:34

Re: Count how many times a script has been run + loop through

21 Nov 2017, 15:38

Seems to work like a charm! This code is however above my understanding so not quite sure what it's doing so I need help :)

The script is creating 1 file for each script and user. PERFECT!

Now I want a gui to display to the user how many time he/she used ALL Scripts, but ONLY those he/she have used. So if I have a script that is not used, I don't want it to be displayed. Would that be possible? I want to be able to write my self the name like instead of it taking the script name (i could use the script name too but I rather not). Thanks again!

You have used:
Script 1: xx times
Script 2: xx times

All of us have used:
Display stats of all scripts, also here I want to write the scripts name manually.
Odlanir wrote:Another way to accomplish the task.
To avoid concurrent update of a common file for all the network users I'll do this way.
Every time a script is executed it writes ( or renames ) ,in a common network folder, a filename with the following format: scriptname_username_count.txt containing the execution timestamp ( optional ).
I've used this method at my jobsite and I've never get any problem.


In the auto execute section:

Code: Select all

NetworkFolder   := "C:\temp\tmp"  ; replace with your network folder name
ThisScript      := RegExReplace(A_ScriptName, "iDU)^(.*)\.(ahk|exe)$", "$1")
gosub WriteFileFlag
everywhere in your script

Code: Select all

WriteFileFlag:
if fileexist(NetworkFolder "\" ThisScript "_" A_UserName "*") {
   loop, % NetworkFolder "\" ThisScript "_" A_UserName "*" {   
      RegExMatch(A_LoopFileName, "O)(.*)_(\d+)\.txt",tmp)      
      newName := NetworkFolder "\" tmp[1] "_" format("{:04}",tmp[2] + 1) ".txt"
      FileMove, % NetworkFolder "\" A_LoopFileName , % newName      
      FileAppend, % a_now "`n", % newName  ;  <------- optional
   }
} else {
   FileAppend, % a_now "`n", % NetworkFolder "\" ThisScript "_" A_UserName "_" format("{:04}",1) ".txt"
}   
return
You can simply get the script execution statistics by this ...

Code: Select all

GetScriptStats:
   count := 0
   loop, % NetworkFolder "\" ThisScript "*" {
      RegExMatch(A_LoopFileName, "O)(.*)_(\d+)\.txt",tmp)      
      count += tmp[2]
   }
   MsgBox % "The script has been executed : " count " times."
return
... and the user execution statistics by this

Code: Select all

GetUserStats:
   count := 0
   loop, % NetworkFolder "\*_" A_UserName "_*" {
      RegExMatch(A_LoopFileName, "O)(.*)_(\d+)\.txt",tmp)      
      count += tmp[2]
   }
   MsgBox % "The user has executed : " count " scripts."
return
Cheers.
User avatar
divanebaba
Posts: 805
Joined: 20 Dec 2016, 03:53
Location: Diaspora

Re: Count how many times a script has been run + loop through

22 Nov 2017, 05:55

For this new case, I would recommend, that a userscript not only generates one statistic file with timestamps. Generating a second file, with special name for every user, which only exists, when a script is running, would be the easiest choice.
If the user quits his script, the script has to delete the second file.
So you can easily see, if a script is running or not.
Einfach nur ein toller Typ. :mrgreen:
Odlanir
Posts: 659
Joined: 20 Oct 2016, 08:20

Re: Count how many times a script has been run + loop through

22 Nov 2017, 10:03

Some like this ? Really basic but should give you a starting point.

Code: Select all

#SingleInstance, force
#Persistent
#NoEnv
SetBatchLines, -1
SetWinDelay, -1
SetTitleMatchMode 2

NetworkFolder   := "C:\temp\tmp"  ; <-----  Change to suit your network folder
loop, % NetworkFolder "\*.txt" {
   userName := RegExReplace(A_LoopFileName, "^.*_(.*)_.*", "$1")
   usrList .= userName "`n"
   scrName := RegExReplace(A_LoopFileName, "^(.*)_.*_.*", "$1")
   scrList .= scrName "`n"
}
sort, usrList, U
sort, scrList, U
usrList := "none||" RegExReplace(usrList, "`n", "|")
scrList := "none||" RegExReplace(scrList, "`n", "|")
Gui, Font, s15 bold 
Gui, Add, Text, x10 w200 center ,Script Statistics
Gui, Font, s10 Normal
Gui, Add, Text, x10 y60 w100,Users
Gui, Add, DropDownList, yp-5 x70 gUsr,%usrlist%
Gui, Add, Text, x10 w50,Scripts
Gui, Add, DropDownList, yp-5 x70 gScr,%scrList%
gui, font,, Courier
Gui, Add,Edit, x10 r30 w300 vEdi
gui,Show
return

ExitApp   

Usr:   
   scr := ""
   scr .= "Execution by User : " A_GuiControl "`n"
   scr .= "---------------------------------`n"
   scr .= "Script`t`tCount`n`n"
   loop, % NetworkFolder "\*.txt" {
      if ( RegExMatch(A_LoopFileName, A_GuiControl)) {
         scriptName := RegExReplace(A_LoopFileName, "^(.*)_.*_.*", "$1")
         scr .= scriptName "`t`t" format("{:5}",RegExReplace(A_LoopFileName, ".*_(.*)\.txt", "$1") "`n")
      }
   }
   GuiControl,,Edi, %scr%
return
Scr:
   scr := ""
   scr .= "Execution by Script : " A_GuiControl "`n"
   scr .= "---------------------------------`n"
   scr .= "User`t`tCount`n`n"
   loop, % NetworkFolder "\*.txt" {
      if ( RegExMatch(A_LoopFileName, A_GuiControl)) {
         userName := RegExReplace(A_LoopFileName, ".*_(.*)_.*", "$1")
         scr .= userName "`t`t" format("{:5}",RegExReplace(A_LoopFileName, ".*_(.*)\.txt", "$1") "`n")
      }
   }
   GuiControl,,Edi, %scr%
return

Esc::
   ExitApp

GuiClose:
GuiEscape:
   ExitApp
ExitApp
Cheers.
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
Johana
Posts: 189
Joined: 02 May 2017, 02:34

Re: Count how many times a script has been run + loop through

01 Dec 2017, 10:31

Hello Odlanir!

I've used your code and now the past 10 Days I've tried to read about each line in help forums and so on trying to understand it. Very hard to understand, but I'm getting there.

This is doing almost exactly as I want. However I would like to my self tell it how to sum on each row.

I.e:

I want to tell it to show a gui only with "Script 1" and show stats from script 1. I dont want users, i just want the total amount it has been run.

Here is a example:
Script 1 has been run X times!
Appreciate the help with no way to offer you my graditute. Thank you for Everything.

Odlanir wrote:Some like this ? Really basic but should give you a starting point.

Code: Select all

#SingleInstance, force
#Persistent
#NoEnv
SetBatchLines, -1
SetWinDelay, -1
SetTitleMatchMode 2

NetworkFolder   := "C:\temp\tmp"  ; <-----  Change to suit your network folder
loop, % NetworkFolder "\*.txt" {
   userName := RegExReplace(A_LoopFileName, "^.*_(.*)_.*", "$1")
   usrList .= userName "`n"
   scrName := RegExReplace(A_LoopFileName, "^(.*)_.*_.*", "$1")
   scrList .= scrName "`n"
}
sort, usrList, U
sort, scrList, U
usrList := "none||" RegExReplace(usrList, "`n", "|")
scrList := "none||" RegExReplace(scrList, "`n", "|")
Gui, Font, s15 bold 
Gui, Add, Text, x10 w200 center ,Script Statistics
Gui, Font, s10 Normal
Gui, Add, Text, x10 y60 w100,Users
Gui, Add, DropDownList, yp-5 x70 gUsr,%usrlist%
Gui, Add, Text, x10 w50,Scripts
Gui, Add, DropDownList, yp-5 x70 gScr,%scrList%
gui, font,, Courier
Gui, Add,Edit, x10 r30 w300 vEdi
gui,Show
return

ExitApp   

Usr:   
   scr := ""
   scr .= "Execution by User : " A_GuiControl "`n"
   scr .= "---------------------------------`n"
   scr .= "Script`t`tCount`n`n"
   loop, % NetworkFolder "\*.txt" {
      if ( RegExMatch(A_LoopFileName, A_GuiControl)) {
         scriptName := RegExReplace(A_LoopFileName, "^(.*)_.*_.*", "$1")
         scr .= scriptName "`t`t" format("{:5}",RegExReplace(A_LoopFileName, ".*_(.*)\.txt", "$1") "`n")
      }
   }
   GuiControl,,Edi, %scr%
return
Scr:
   scr := ""
   scr .= "Execution by Script : " A_GuiControl "`n"
   scr .= "---------------------------------`n"
   scr .= "User`t`tCount`n`n"
   loop, % NetworkFolder "\*.txt" {
      if ( RegExMatch(A_LoopFileName, A_GuiControl)) {
         userName := RegExReplace(A_LoopFileName, ".*_(.*)_.*", "$1")
         scr .= userName "`t`t" format("{:5}",RegExReplace(A_LoopFileName, ".*_(.*)\.txt", "$1") "`n")
      }
   }
   GuiControl,,Edi, %scr%
return

Esc::
   ExitApp

GuiClose:
GuiEscape:
   ExitApp
ExitApp
Cheers.
Johana
Posts: 189
Joined: 02 May 2017, 02:34

Re: Count how many times a script has been run + loop through

11 Dec 2017, 08:16

Still haven't figured it out.. Anyone?
Odlanir
Posts: 659
Joined: 20 Oct 2016, 08:20

Re: Count how many times a script has been run + loop through

11 Dec 2017, 09:29

Code: Select all

#SingleInstance, force
#Persistent
#NoEnv
SetBatchLines, -1
SetWinDelay, -1
SetTitleMatchMode 2

NetworkFolder   := "C:\temp\tmp"  ; <-----  Change to suit your network folder
loop, % NetworkFolder "\*.txt" {
   scrName := RegExReplace(A_LoopFileName, "^(.*)_.*_.*", "$1")
   scrList .= scrName "`n"
}
sort, scrList, U
scrList := "select||" RegExReplace(scrList, "`n", "|")
Gui, Font, s10 Normal
Gui, Add, Text, x10 w50,Scripts
Gui, Add, DropDownList, yp-5 x70 gScr,%scrList%
gui, font,s15
Gui, Add,text, x10 w600 vTx
gui,Show
return

ExitApp   


Scr:
	cnt := 0
	if ( A_GuiControl = "select") 
		return
	loop, % NetworkFolder "\*.txt" {
		if ( RegExMatch(A_LoopFileName, A_GuiControl)) {
			cnt += RegExReplace(A_LoopFileName, ".*_(.*)\.txt", "$1")
		}
	}
	GuiControl,,Tx, % "The script: """ trim(A_GuiControl) . """ has been run " cnt " times!"
return

Esc::
   ExitApp

GuiClose:
GuiEscape:
   ExitApp
ExitApp
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 169 guests