gte Posted October 27, 2009 Posted October 27, 2009 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) HP OpenView ServiceCenter keep alive scriptRemote Desktop Login Script
PsaltyDS Posted October 27, 2009 Posted October 27, 2009 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
Moderators Melba23 Posted October 27, 2009 Moderators Posted October 27, 2009 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) NextM23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
Xenobiologist Posted October 27, 2009 Posted October 27, 2009 #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
gte Posted October 27, 2009 Author Posted October 27, 2009 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 HP OpenView ServiceCenter keep alive scriptRemote Desktop Login Script
gte Posted October 27, 2009 Author Posted October 27, 2009 Here is what it is saying by the wayRunning:(3.3.0.0):C:\Program Files\AutoIt3\autoit3.exe "C:\Program Files\AutoIt3\Examples\Helpfile\autoithelp\FileOpenDialog.au3" 1 - 02 - 03 - 04 - 05 - 06 - 07 - 08 - 09 - 010 - 011 - 012 - 0 HP OpenView ServiceCenter keep alive scriptRemote Desktop Login Script
PsaltyDS Posted October 27, 2009 Posted October 27, 2009 (edited) 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) NextThe same fix probably applies to Melba23's FileRead(). Edited October 27, 2009 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
Moderators Melba23 Posted October 27, 2009 Moderators Posted October 27, 2009 PSaltyDS, Spot on - that was the problem with my snippet! gte, Apologies for that error - comes of testing files in the same folder! M23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
gte Posted October 27, 2009 Author Posted October 27, 2009 (edited) Melba - no worries, I appreciate the help! Got it returning values to the console! Now maybe to a text file ... Edited October 27, 2009 by gte HP OpenView ServiceCenter keep alive scriptRemote Desktop Login Script
gte Posted October 27, 2009 Author Posted October 27, 2009 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) HP OpenView ServiceCenter keep alive scriptRemote Desktop Login Script
Xenobiologist Posted October 28, 2009 Posted October 28, 2009 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
gte Posted October 28, 2009 Author Posted October 28, 2009 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 HP OpenView ServiceCenter keep alive scriptRemote Desktop Login Script
Xenobiologist Posted October 28, 2009 Posted October 28, 2009 (edited) 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 October 28, 2009 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
gte Posted October 28, 2009 Author Posted October 28, 2009 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) HP OpenView ServiceCenter keep alive scriptRemote Desktop Login Script
gte Posted October 28, 2009 Author Posted October 28, 2009 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 HP OpenView ServiceCenter keep alive scriptRemote Desktop Login Script
gte Posted October 28, 2009 Author Posted October 28, 2009 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) HP OpenView ServiceCenter keep alive scriptRemote Desktop Login Script
Xenobiologist Posted October 28, 2009 Posted October 28, 2009 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
gte Posted October 28, 2009 Author Posted October 28, 2009 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 HP OpenView ServiceCenter keep alive scriptRemote Desktop Login Script
Xenobiologist Posted October 28, 2009 Posted October 28, 2009 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
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now