Jump to content

Trying to count the number of instances of a string, in files inside of a folder


gte
 Share

Recommended Posts

Here is the code I have so far, it counts the number of instances of ".XML" in the file c:\temp\testxmls\test.xml.

I'd like it to do that for every .xml file in a folder (maybe to an array) and then substitute "test.xml" for the name of each file in a loop (maybe a for statement)?

Any ideas?

#include <File.au3>


$xmlinstancecount = _countxmlsinstances()


func _countxmlsinstances()


Local $xmlcount = 0


For $linenumber = 1 to _FileCountLines("c:\temp\testxmls\test.xml") Step 1 

if stringinstr(FileReadLine("c:\temp\testxmls\test.xml", $linenumber), ".XML", 1) <> 0 Then
    $xmlcount = $xmlcount + 1
EndIf


Next





Return $xmlcount

EndFunc


MsgBox(0, ".XML count", $xmlinstancecount)
Link to comment
Share on other sites

StringReplace() returns a count in @extended. To count instances of a string:

$sFile = "c:\temp\testxmls\test.xml"
$sData = FileRead($sFile)
StringReplace($sData, ".xml", "")
$iCnt = @extended
MsgBox(64, "Count", "The text '.xml' occurred " & $iCnt & " times in the file " & $sFile)

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

  • Moderators

gte,

Unless you have only 1 ".XML" per line,m you will get the wrong answer with StringInStr. :)

Try something like this:

#include <File.au3>

$sString = ".XML"

$aFileList = _FileListToArray("Folder_Path", "*.xml", 1)

For $i = 1 To $aFileList[0]

    $iCount = UBound (StringRegExp(FileRead($aFileList[$i]), "(?i)(" & $sString & ")", 3))
    ConsoleWrite($i & " - " & $iCount & @CRLF)

Next

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

#include <File.au3>
Local $FileList = _FileListToArray(@ScriptDir, '*.xml', 1)
For $i = 0 To UBound($FileList) - 1
    StringReplace(FileRead($FileList[$i]), 'a', 'a')
    ConsoleWrite($FileList[$i] & ' = ' & @extended & @CRLF)
Next

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

You guys are the best, psalty and melba always saving my butt!

Thanks also Xeno!

Melba,

Something isn't processing right with this line, as it's reading "0" instances, and I have multiple instances in these text files, any ideas? I tried changing around a bunch of things, but could not get it to work. If you want to see what I'm working with, I put my folder "testxmls" into a zip file and attached it to this post.

$iCount = UBound (StringRegExp(FileRead($aFileList[$i]), "(?i)(" & $sString & ")", 3))

Psalty,

How do I have your code read more then 1 file (read the entire contents of the folder?)

testxmls.zip

Link to comment
Share on other sites

Psalty,

How do I have your code read more then 1 file (read the entire contents of the folder?)

It doesn't. That was only a demo of counting instances in one file. Xenobiologist put the same technique (using StringReplace()) into a loop that would do multiple file.

Note that _FileListToArray() only returns the file name without the directory path, so his should probably be:

#include <File.au3>

Local $FileList = _FileListToArray(@ScriptDir, '*.xml', 1)

For $i = 0 To UBound($FileList) - 1

StringReplace(FileRead(@ScriptDir & "\" & $FileList[$i]), 'a', 'a')

ConsoleWrite($FileList[$i] & ' = ' & @extended & @CRLF)

Next

The same fix probably applies to Melba23's FileRead().

:)

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

  • Moderators

PSaltyDS,

Spot on - that was the problem with my snippet! :)

gte,

Apologies for that error - comes of testing files in the same folder! :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Ok,

Now that I have them writing to a file, how can I add the contents of the file together mathematically?

Should I read them back into an array and use _addarray ? Or filereadline with some sort of for statement?

I apologize, but I'm a newb and both arrays and for statements are my biggest weaknesses (but I'm working hard on understanding them completely) :)

#include <File.au3>


$sString = ".XML"

$aFileList = _FileListToArray("c:\temp\testxmls", "*.xml", 1)


FileDelete("c:\temp\testxmls\results.txt")

For $i = 1 To $aFileList[0]
    ; MsgBox(0, 2, StringRegExp(FileRead($aFileList[$i]), "(?i)(" & $sString & ")", 3))
    $iCount = UBound (StringRegExp(FileRead("c:\temp\testxmls\" & $aFileList[$i]), "(?i)(" & $sString & ")", 3))
    
    FileWrite("c:\temp\testxmls\results.txt", $iCount & @CRLF)
    ConsoleWrite($i & " - " & $iCount & @CRLF)
    
Next

maybe something like this?

For $aDotXmlCount = FileReadLine("c:\temp\testxmls\results.txt", 0) To FileReadLine("c:\temp\testxmls\results.txt", 999) Step 1

Next

MsgBox(0,5, $aDotXmlCount)
Link to comment
Share on other sites

You mean something like this?

#include <File.au3>
Local $FileList = _FileListToArray(@ScriptDir, '*.xml', 1)
Local $tmp = '', $tmp1 = '', $all = ''

FileDelete(@ScriptDir & '\all.xml')
FileDelete(@ScriptDir & '\found.xml')

For $i = 1 To UBound($FileList) - 1
    $tmp = FileRead($FileList[$i])
    $all &= ' - - - File ' & $FileList[$i] & ' - - - ' & @CRLF & $tmp & @CRLF
    StringReplace(@ScriptDir & '\' & $tmp, 'a', 'a')
    $tmp1 &= $FileList[$i] & ' = ' & @extended & @CRLF
Next
FileWrite(@ScriptDir & '\all.xml', $all)
FileWrite(@ScriptDir & '\found.xml', $tmp1)

Besides: StringReplace is much faster than StringRegExp in this case!

Mega

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

Hey Mega,

Thanks for the reply!

I'm trying to decipher the different variables in your code. For some reason it's still returning '0 instances found' ... so as soon as I can figure out some of the functions that are new to me, I will hopefully be able to report back with success.

You mean something like this?

#include <File.au3>
Local $FileList = _FileListToArray(@ScriptDir, '*.xml', 1)
Local $tmp = '', $tmp1 = '', $all = ''

FileDelete(@ScriptDir & '\all.xml')
FileDelete(@ScriptDir & '\found.xml')

For $i = 1 To UBound($FileList) - 1
    $tmp = FileRead($FileList[$i])
    $all &= ' - - - File ' & $FileList[$i] & ' - - - ' & @CRLF & $tmp & @CRLF
    StringReplace(@ScriptDir & '\' & $tmp, 'a', 'a')
    $tmp1 &= $FileList[$i] & ' = ' & @extended & @CRLF
Next
FileWrite(@ScriptDir & '\all.xml', $all)
FileWrite(@ScriptDir & '\found.xml', $tmp1)

Besides: StringReplace is much faster than StringRegExp in this case!

Mega

Link to comment
Share on other sites

Of course it does. There went somethig wrong :-)

#include <File.au3>

FileDelete(@ScriptDir & '\all.xml')
FileDelete(@ScriptDir & '\found.xml')

Local $FileList = _FileListToArray(@ScriptDir, '*.xml', 1)
Local $tmp = '', $tmp1 = '', $all = ''

For $i = 1 To UBound($FileList) - 1
    $tmp = FileRead(@ScriptDir & '\' & $FileList[$i])
    $all &= ' - - - File ' & $FileList[$i] & ' - - - ' & @CRLF & $tmp & @CRLF
    StringReplace($tmp, 'a', 'a')
    $tmp1 &= $FileList[$i] & ' = ' & @extended & @CRLF
Next
FileWrite(@ScriptDir & '\all.xml', $all)
FileWrite(@ScriptDir & '\found.xml', $tmp1)
Edited by Xenobiologist

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

Ok, am I understanding this correctly for the sake of trying to learn? (this is the old code, I'll look at the tweaked code now)

From Xenobiologist, on 28 October 2009 - 02:24 AM

Local $FileList = _FileListToArray("c:\temp\testxmls", '*.xml', 1)      ; variable filelist equals filelist to array, file type of xml, files only
    Local $tmp = '', $tmp1 = '', $all = ''                                  ; local variables


    FileDelete("c:\temp\testxmls" & '\all.xml')                             ; deletion of old files
    FileDelete("c:\temp\testxmls" & '\found.xml')                           ; deletion of old files

    For $i = 1 To UBound($FileList) - 1                                     ; variable $i to size of array $filelist
        
        $tmp = FileRead($FileList[$i])                                      ; variable temp equals fileread an array
        
        $all &= ' - - - File ' & $FileList[$i] & ' - - - ' & @CRLF & $tmp & @CRLF   ; variable all = filelist array and some formatting characters
        StringReplace("c:\temp\testxmls" & '\' & $tmp, 'a', 'a')            ; search for "a" in the filelist array and replace with "a" ?
        $tmp1 &= $FileList[$i] & ' = ' & @extended & @CRLF                  ; variable tmp1 = filelist arrary and some formatting characters
    Next

    FileWrite("c:\temp\testxmls" & '\all.xml', $all)                        ; write new files
    FileWrite("c:\temp\testxmls" & '\found.xml', $tmp1)                     ; write new files

Of course it does. There went somethig wrong :-)

#include <File.au3>
Local $FileList = _FileListToArray(@ScriptDir, '*.xml', 1)
Local $tmp = '', $tmp1 = '', $all = ''

FileDelete(@ScriptDir & '\all.xml')
FileDelete(@ScriptDir & '\found.xml')

For $i = 1 To UBound($FileList) - 1
    $tmp = FileRead(@ScriptDir & '\' & $FileList[$i])
    $all &= ' - - - File ' & $FileList[$i] & ' - - - ' & @CRLF & $tmp & @CRLF
    StringReplace($tmp, 'a', 'a')
    $tmp1 &= $FileList[$i] & ' = ' & @extended & @CRLF
Next
FileWrite(@ScriptDir & '\all.xml', $all)
FileWrite(@ScriptDir & '\found.xml', $tmp1)

Link to comment
Share on other sites

The found xml is still saying?

all.xml = 0

Copy (10) of test.XML = 0

Copy (11) of test.XML = 0

Copy (2) of test.xml = 0

Copy (3) of test.xml = 0

Copy (4) of test.xml = 0

Copy (5) of test.xml = 0

Copy (6) of test.xml = 0

Copy (7) of test.xml = 0

Copy (8) of test.XML = 0

Copy (9) of test.XML = 0

Copy of test.xml = 0

found.xml = 0

test.xml = 0

Link to comment
Share on other sites

I was able to get an average based on this code

$fileread = FileRead("c:\temp\testxmls\results.txt")
MsgBox(0,1,$fileread)
$out = StringReplace($fileread, @CRLF, "+")
If StringRight($out,1) = "+" Then
    $out = StringLeft($out,StringInStr($out,"+",0,-1)-1)
EndIf
MsgBox(1,"$out",$out)
$sum = Execute($out)
MsgBox(1,"$sum",$sum)

$cleanup = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\", "Delay - Processed")
MsgBox(0, 2, $cleanup)

$average = Round(($sum / $cleanup),3)

MsgBox(0, 3, $average)
Link to comment
Share on other sites

No, :). Sorry for the confusion!

Try this.

#include <File.au3>
FileDelete("c:\temp\testxmls" & '\all.xml') ; deletion of old files
FileDelete("c:\temp\testxmls" & '\found.xml') ; deletion of old files

Local $FileList = _FileListToArray("c:\temp\testxmls", '*.xml', 1) ; variable filelist equals filelist to array, file type of xml, files only
Local $tmp = '', $tmp1 = '', $all = '' ; local variables

For $i = 1 To UBound($FileList) - 1 ; variable $i to size of array $filelist
    $tmp = FileRead("c:\temp\testxmls\" & $FileList[$i]) 
    $all &= ' - - - File ' & $FileList[$i] & ' - - - ' & @CRLF & $tmp & @CRLF 
    StringReplace($tmp, 'a', 'a') ; replace a with what you are searching for!
    $tmp1 &= $FileList[$i] & ' = ' & @extended & @CRLF 
Next

FileWrite("c:\temp\testxmls" & '\all.xml', $all) ; write new files
FileWrite("c:\temp\testxmls" & '\found.xml', $tmp1) ; write new files

Change the a to your search pattern!

Mega

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

Thanks Mega ... you're the man! :)

No, :). Sorry for the confusion!

Try this.

#include <File.au3>
FileDelete("c:\temp\testxmls" & '\all.xml') ; deletion of old files
FileDelete("c:\temp\testxmls" & '\found.xml') ; deletion of old files

Local $FileList = _FileListToArray("c:\temp\testxmls", '*.xml', 1) ; variable filelist equals filelist to array, file type of xml, files only
Local $tmp = '', $tmp1 = '', $all = '' ; local variables

For $i = 1 To UBound($FileList) - 1 ; variable $i to size of array $filelist
    $tmp = FileRead("c:\temp\testxmls\" & $FileList[$i]) 
    $all &= ' - - - File ' & $FileList[$i] & ' - - - ' & @CRLF & $tmp & @CRLF 
    StringReplace($tmp, 'a', 'a') ; replace a with what you are searching for!
    $tmp1 &= $FileList[$i] & ' = ' & @extended & @CRLF 
Next

FileWrite("c:\temp\testxmls" & '\all.xml', $all) ; write new files
FileWrite("c:\temp\testxmls" & '\found.xml', $tmp1) ; write new files

Change the a to your search pattern!

Mega

Link to comment
Share on other sites

No problem! Hope that this was not only step 1 for your task :)

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...