![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | Add + count pattern occurencies in text file? Assume I have a text file called "dictionary" which contains lines with the scheme: <number> <pattern> example: dict.txt: 2 Paul 436 Karl Thomas 1034 something with $@:- special chars 33 Suzan Miller Now I want to create a vbscript script which takes a pattern as command line parm, then searches in this dict file and add it either (if already existing) by increasing the number by 1 or (if new) by adding a new line. Example: addpatt.vb "Karl Thomas" should result in a new dict.txt: dict.txt: 2 Paul 437 Karl Thomas 1034 something with $@:- special chars 33 Suzan Miller How can I code this with vbscript? Richard |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Add + count pattern occurencies in text file? "Richard Blackley" <blackboard@xxxxxx> wrote in message news:49a1aba6$0$31867$9b4e6d93@xxxxxx-online.net... Quote: > Assume I have a text file called "dictionary" which contains lines with > the scheme: > <number> <pattern> > example: > > dict.txt: > 2 Paul > 436 Karl Thomas > 1034 something with $@:- special chars > 33 Suzan Miller > > Now I want to create a vbscript script which takes a pattern as command > line parm, then searches in this dict file > and add it either (if already existing) by increasing the number by 1 or > (if new) by adding a new line. > > Example: > > addpatt.vb "Karl Thomas" > > should result in a new dict.txt: > > dict.txt: > 2 Paul > 437 Karl Thomas > 1034 something with $@:- special chars > 33 Suzan Miller > > How can I code this with vbscript? the parts that are giving you trouble. Here is how I would approach this: read the entire file for each line create a dictionary entry with the text info as the key and the numeric part as the value if an entry exists for the item supplied as parameter increment the value part else create a dictionary entry with the parameter as the key and a value of 1 end if open file for output for each entry in the dictionary object write to the output file the value, a space, and the key next A few issues: - suggest renaming the input file and creating a new copy with the old name to receive the output. - what should happen if the file contains: 123 Al 33 Bill 22 Al /Al |
My System Specs![]() |
| | #3 (permalink) |
| | Re: Add + count pattern occurencies in text file? "Richard Blackley" <blackboard@xxxxxx> wrote in message news:49a1aba6$0$31867$9b4e6d93@xxxxxx-online.net... Quote: > Assume I have a text file called "dictionary" which contains lines with > the scheme: > <number> <pattern> > example: > > dict.txt: > 2 Paul > 436 Karl Thomas > 1034 something with $@:- special chars > 33 Suzan Miller > > Now I want to create a vbscript script which takes a pattern as command > line parm, then searches in this dict file > and add it either (if already existing) by increasing the number by 1 or > (if new) by adding a new line. > > Example: > > addpatt.vb "Karl Thomas" > > should result in a new dict.txt: > > dict.txt: > 2 Paul > 437 Karl Thomas > 1034 something with $@:- special chars > 33 Suzan Miller > > How can I code this with vbscript? Using a text file for this purpose, with plain old VBScript and the scripting objects described in Script56.htm, is quite doable, but maybe not very efficient. As the text dictionary file grows, the script will become slower. The script could easily read the file, split it into an array of lines, split the number part of each line from the text part, and then build a dictionary with the text part as key and the count part as item associated with the key. A line like the following could both add a new dictionary item for new text and increase the count if the text already exists: oDic(sText) = oDic(sText) + 1 Then you would have to write the entire contents of oDic back to the text file. Microsoft provides ADO as a scriptable object for manipulating many kinds of databases. Having played with ADO a little, I think it would be relatively easy to use your dictionary text file as a database table, and use an SQL statement to find out whether the new text exists. Another few lines of ADO-related script could increment its count, or add a new table row for new text. I don't have the expertise to whip up an example, but having used ADO to search a 33 megabyte text file (but not modify it), I think ADO could do this kind of update much faster than plain script. -Paul Randall |
My System Specs![]() |
| | #4 (permalink) |
| | Re: Add + count pattern occurencies in text file? You can use sub. MyDict() Sub MyDict Set dictPhrase = CreateObject ("Scripting.Dictionary") dictPhrase .Add "text1", 1 : dictPhrase .Add "text1", 2 dictPhrase .Add "text1", 3 : dictPhrase .Add "text1", 4 dictPhrase .Add "text1", 5 : dictPhrase .Add "text1", 6 End Sub Maybe this will help. Regards, esska |
My System Specs![]() |
| | #5 (permalink) |
| | Re: Add + count pattern occurencies in text file? On Mar 3, 8:07*am, esska <es...@xxxxxx> wrote: Quote: > You can use sub. > * * * * MyDict() > > Sub MyDict > * * * * * * * * Set dictPhrase = CreateObject ("Scripting.Dictionary") > > * * * * * * * * dictPhrase .Add "text1", 1 * *: dictPhrase .Add "text1", 2 > * * * * * * * * dictPhrase .Add "text1", 3 * *: dictPhrase .Add "text1", 4 > * * * * * * * * dictPhrase .Add "text1", 5 * *: dictPhrase .Add "text1", 6 > End Sub > > Maybe this will help. > > Regards, > esska this subroutine will do nothing. That is, without a Dim statement in the main routine to make its scope global the variable's scope is confined to the subroutine. I prefer to minimize the use of such global definitions, so I would recommend using a function, instead. For example ... set theDict = MyDict(sText) Function MyDict(sText) Dim dictPhrase Set dictPhrase = CreateObject ("Scripting.Dictionary") dictPhrase .Add sText, 1 : dictPhrase .Add sText, 2 dictPhrase .Add sText, 3 : dictPhrase .Add sText, 4 dictPhrase .Add sText, 5 : dictPhrase .Add sText, 6 set MyDict = dictPhrase End Function Besides that, the logic of your subroutine eludes me (in relation to the OPs request). Tom Lavedas *********** http://there.is.no.more/tglbatch/ |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Search number of file which match a pattern | PowerShell | |||
| Convert hostname text file to IP text file | VB Script | |||
| How do I read a text file and sort text by fixed positions? | PowerShell | |||
| Deleting/Filtering lines of text based on a pattern | PowerShell | |||