Counting files inside folder with different name + counting numbers inside filename

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

Counting files inside folder with different name + counting numbers inside filename

22 Jan 2018, 08:51

I have inside a folder textfiles that shows me how many times a script (name) has been run (0000) by a user (user).
Name_User_0000.txt
Basically I want to be able to make statistical use of this. I need to:
  • Count how many times a user has run a script
  • Count what scripts a user has run
  • Count how many scripts a script has been run by all users
This should all be insterted into an Excelfile.

I don't know where to start or adress this. Specially since I don't know how to make a loop nor combine two variables and present them. Any and all help appreciated. Thanks in Advanced,
J
User avatar
divanebaba
Posts: 805
Joined: 20 Dec 2016, 03:53
Location: Diaspora

Re: Counting files inside folder with different name + counting numbers inside filename

22 Jan 2018, 09:40

You can start with the Example #3 in Loop (files & folders).
When you are able to list and display all files, you can make second step, which could be creating a Gui for displaying desired data.
Good luck.
Einfach nur ein toller Typ. :mrgreen:
Johana
Posts: 189
Joined: 02 May 2017, 02:34

Re: Counting files inside folder with different name + counting numbers inside filename

24 Jan 2018, 03:54

divanebaba wrote:You can start with the Example #3 in Loop (files & folders).
When you are able to list and display all files, you can make second step, which could be creating a Gui for displaying desired data.
Good luck.
Not getting it to work, it's not displaying the files :S

Code: Select all

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

NetworkFolder := "XX"  ; replace with your network folder name
loop, % NetworkFolder "\*.txt" {
   scrName := RegExReplace(A_LoopFileName, "^(.*)_.*_.*", "$1")
   scrList .= scrName "`n"
}
sort, scrList, U
scrList := "Visa||" 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 h600 vTx
gui,Show
return

Scr:
	cnt := 0
	if ( A_GuiControl = "Visa") 
		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::
GuiClose:
GuiEscape:
ExitApp
return
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Counting files inside folder with different name + counting numbers inside filename

24 Jan 2018, 06:12

I'd parse for the filename ...
a) extracting the username
b) creating an array named after the extracted username
c) push all upcoming details that are assigned to that specific user into its array
d) once the file filtering has been finished create a CSV file based on the array data (or trigger Excel directly using COM)
Johana
Posts: 189
Joined: 02 May 2017, 02:34

Re: Counting files inside folder with different name + counting numbers inside filename

24 Jan 2018, 07:47

BoBo wrote:I'd parse for the filename ...
a) extracting the username
b) creating an array named after the extracted username
c) push all upcoming details that are assigned to that specific user into its array
d) once the file filtering has been finished create a CSV file based on the array data (or trigger Excel directly using COM)
Ah yeah that sounds clever! Since we're chatting about and having fun here, how would YOU do it? :)
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Counting files inside folder with different name + counting numbers inside filename

24 Jan 2018, 14:44

Code: Select all

Loop 9
   FileAppend, test,% A_ScriptName "_BoBo_000" A_Index ".txt"   ; create 9 files named "testscript_username_0009.txt" for testing


F10::
Loop, Files,% A_ScriptDir "\*_*_*.txt"
    {
    fArray := StrSplit(SubStr(A_LoopFileName,1,-4),"_")   ; push the different elements of the filename into the "filename"array and get rid of file extension.
    MsgBox % fArray[1]   ; show the first element of the "filename"array (probably the scriptname)
    }

uArray := fArray[2]   ; name the "username"Array after the content of the second element of the "filename"Array
%uArray% := []   ; create the dynamic "username"array
%uArray%.Push(fArray[1],fArray[3])   ; push the content of the first and third element of the "filename"array to the "username"array

MsgBox % %uArray%[2]   ; show the content of the second element of the "username"array. 
Return
Proof of concept ... (see Xtra's more sophisticated outcome below!)
User avatar
divanebaba
Posts: 805
Joined: 20 Dec 2016, 03:53
Location: Diaspora

Re: Counting files inside folder with different name + counting numbers inside filename

24 Jan 2018, 16:47

I showed you following example from help file.

Code: Select all

; Example #3: Retrieve file names sorted by name (see next example to sort by date):
FileList =  ; Initialize to be blank.
Loop, C:\*.*
    FileList = %FileList%%A_LoopFileName%`n
Sort, FileList, R  ; The R option sorts in reverse order. See Sort for other options.
Loop, parse, FileList, `n
{
    if A_LoopField =  ; Ignore the blank item at the end of the list.
        continue
    MsgBox, 4,, File number %A_Index% is %A_LoopField%.  Continue?
    IfMsgBox, No
        break
}
Instead of using suggested simple beginner friendly code, you choosed a difficult coded version with many of such rows: scrName := RegExReplace(A_LoopFileName, "^(.*)_.*_.*", "$1").
When you choose such difficult code, have in mind that even support will be difficult and you will need high professional supporter instead of normal advanced supporter.
I'm sorry. Your code is too much for me. I don't have any experience with RegEx.
Good luck :mrgreen: :mrgreen:
Einfach nur ein toller Typ. :mrgreen:
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: Counting files inside folder with different name + counting numbers inside filename

24 Jan 2018, 20:03

Code: Select all

#NoEnv
#SingleInstance, force
SetBatchLines, -1

CreateTestFiles()
myObj := {}
NetworkFolder := A_ScriptDir  ; replace with your network folder name

loop, % NetworkFolder . "\*_*_*.txt"
{
    script := name := "" , count := 0
    Loop, Parse, A_LoopFileName, _
        (A_Index = 1) ? (script := A_LoopField) : (A_Index = 2) ? (name := A_LoopField) : (count := LTrim(SubStr(A_LoopField,1,-4),"0"))
    if myObj.HasKey(script)
        myObj[script] += count 
    else
        myObj[script] := count
    scrList .= InStr(scrList,script) ?  "" : (A_Index=1 ? script . "|" : "|" . script)
    AppendCSV(script,name,count)
}

Gui, Font, s10 Normal
Gui, Add, Text, x10 w50,Scripts
Gui, Add, DropDownList, yp-5 x70 gScr vScr, %scrList%
gui, font,s15
Gui, Add,text, x10 w600 h600 vTx
gosub, Scr
gui,Show
return

Scr:
    Gui, Submit, NoHide
    GuiControl,,Tx, % "The script: """ . Scr . """ has been run " . myObj[Scr] . " times!"
return

Esc::
GuiClose:
ExitApp

AppendCSV(script,name,count)
{
    FileAppend, % script . "`," . name . "`," . count . "`n", CSV_Output.csv
}

CreateTestFiles()
{
    ;test files:
    FileAppend,,%A_ScriptDir%\Script1_Johana_0027.txt
    FileAppend,,%A_ScriptDir%\Script2_Johana_0047.txt
    FileAppend,,%A_ScriptDir%\Script2_thatguy_0021.txt
    FileAppend,,%A_ScriptDir%\Script3_ahkrocks_0014.txt
    FileAppend,,%A_ScriptDir%\Script4_testuser_0187.txt
}
HTH
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Counting files inside folder with different name + counting numbers inside filename

25 Jan 2018, 02:31

Xtra wrote:

Code: Select all

#NoEnv
#SingleInstance, force
SetBatchLines, -1

CreateTestFiles()
myObj := {}
NetworkFolder := A_ScriptDir  ; replace with your network folder name

loop, % NetworkFolder . "\*_*_*.txt"
{
    script := name := "" , count := 0
    Loop, Parse, A_LoopFileName, _
        (A_Index = 1) ? (script := A_LoopField) : (A_Index = 2) ? (name := A_LoopField) : (count := LTrim(SubStr(A_LoopField,1,-4),"0"))
    if myObj.HasKey(script)
        myObj[script] += count 
    else
        myObj[script] := count
    scrList .= InStr(scrList,script) ?  "" : (A_Index=1 ? script . "|" : "|" . script)
    AppendCSV(script,name,count)
}

Gui, Font, s10 Normal
Gui, Add, Text, x10 w50,Scripts
Gui, Add, DropDownList, yp-5 x70 gScr vScr, %scrList%
gui, font,s15
Gui, Add,text, x10 w600 h600 vTx
gosub, Scr
gui,Show
return

Scr:
    Gui, Submit, NoHide
    GuiControl,,Tx, % "The script: """ . Scr . """ has been run " . myObj[Scr] . " times!"
return

Esc::
GuiClose:
ExitApp

AppendCSV(script,name,count)
{
    FileAppend, % script . "`," . name . "`," . count . "`n", CSV_Output.csv
}

CreateTestFiles()
{
    ;test files:
    FileAppend,,%A_ScriptDir%\Script1_Johana_0027.txt
    FileAppend,,%A_ScriptDir%\Script2_Johana_0047.txt
    FileAppend,,%A_ScriptDir%\Script2_thatguy_0021.txt
    FileAppend,,%A_ScriptDir%\Script3_ahkrocks_0014.txt
    FileAppend,,%A_ScriptDir%\Script4_testuser_0187.txt
}
HTH
That's really great :thumbup:
... well, I've to admit that I've shrunk your Gui to death :shh:
Spoiler

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: fiendhunter and 233 guests