Hi. I would like to know how to count the number of occurances of a word in a text file.
I am using Microsoft Access VBA.
Any ideas?
7 8275 @vgnadeau
I'll keep the Logic parallel to your previous Thread: - 'Set a Reference to the Microsoft Scripting Runtime Library
-
Dim objFSO As FileSystemObject
-
Dim ts As TextStream
-
Dim strText As String
-
Dim strSearchText As String
-
Dim strFileName As String
-
Dim lngPos As Long
-
Dim lngStringCount As Long
-
-
strSearchText = "cb: TRINGLE"
-
-
strFileName = CurrentProject.Path & "\Test.txt"
-
-
'Create instance of FileSystemObject.
-
Set objFSO = CreateObject("Scripting.FileSystemObject")
-
Set ts = objFSO.OpenTextFile(strFileName, ForReading)
-
-
'Read entire contents of file, save to strText variable
-
strText = ts.ReadAll
-
-
lngPos = 1
-
-
Do
-
lngPos = InStr(lngPos, strText, strSearchText)
-
If lngPos > 0 Then
-
lngStringCount = lngStringCount + 1
-
lngPos = lngPos + Len(strSearchText)
-
End If
-
Loop Until lngPos = 0
-
-
MsgBox "The String [" & strSearchText & "] appears " & lngStringCount & " time(s) in the " & _
-
"File " & strFileName & "!"
You are awesome, thanks so much for your help!
Ok. Last problem of the day. Now I want to be able to repeat the cycle. I've counted the numner of occurances of "cb:TRINGLE" and divided the result by 4 to give me the number of times I need to repeat the Find/replace. So, I want to replace the first and fourth insances and then the first and fourth again ...
Example:
cb:MATERIAL
cb:TRINGLE
cb:TRINGLE
cb:MATERIAL
cb:MATERIAL
cb:TRINGLE
cb:TRINGLE
cb:MATERIAL
cb:MATERIAL
cb:TRINGLE
cb:TRINGLE
cb:MATERIAL
Here's my code so far; -
Function FindAndReplace()
-
Dim sSearchText As String
-
Dim sReplaceText As String
-
Dim sFileName As String
-
Dim lngPosition As Long
-
Dim intNumOfMatches As Integer
-
Dim strText As String
-
Dim strNew As String
-
Dim lngNewPos As Long
-
Dim strMsg As String
-
Dim intCount As Integer
-
-
Const ForReading = 1
-
Const ForWriting = 2
-
-
sSearchText = "cb:TRINGLE"
-
sReplaceText = "cb:MATERIAL"
-
-
sFileName = "C:\test.txt"
- intCount = CountWordOccurances(sSearchText, sFileName)
-
intCount = intCount \ 4
-
-
-
Set objFSO = CreateObject("Scripting.FileSystemObject")
-
Set ts = objFSO.OpenTextFile(sFileName, ForReading)
-
strText = ts.ReadAll
-
-
-
lngPosition = InStr(strText, sSearchText)
-
If lngPosition = 0 Then Exit Function
-
-
strNew = Left$(strText, lngPosition - 1) & sReplaceText & _
-
Mid$(strText, lngPosition + Len(sSearchText))
-
-
Starting
-
intNumOfMatches = intNumOfMatches + 1
-
lngNewPos = lngPosition + 1
-
-
Do
-
If InStr(lngNewPos, strText, sSearchText) <> 0 Then
-
lngPosition = InStr(lngNewPos, strText, sSearchText)
-
intNumOfMatches = intNumOfMatches + 1
-
lngNewPos = lngPosition + 1
-
If intNumOfMatches = 4 Then '4th occurrence
-
strNew = Left$(strNew, lngPosition - 1) & "/" & sReplaceText & _
-
Mid$(strNew, lngPosition + Len(sReplaceText))
-
End If
-
End If
-
Loop Until intNumOfMatches = 4 Or lngPosition = 0
-
-
Set objFile = objFSO.OpenTextFile(sFileName, ForWriting)
-
objFile.WriteLine strNew
-
-
objFile.Close
-
-
End Function
-
-
Function CountWordOccurances(WordToCount As String, Filename As String) As Integer
-
Dim strText As String
-
Dim strSearchText As String
-
Dim strFileName As String
-
Dim lngPos As Long
-
Dim lngStringCount As Long
-
-
strSearchText = WordToCount
-
strFileName = Filename
-
-
Set objFSO = CreateObject("Scripting.FileSystemObject")
-
Set ts = objFSO.OpenTextFile(strFileName, 1)
-
-
strText = ts.ReadAll
-
-
lngPos = 1
-
-
Do
-
lngPos = InStr(lngPos, strText, strSearchText)
-
If lngPos > 0 Then
-
lngStringCount = lngStringCount + 1
-
lngPos = lngPos + Len(strSearchText)
-
End If
-
Loop Until lngPos = 0
-
-
End Function
-
I don't know what to do next ...
- You should have clearly stated your Primary Objective up front and saved me some extra effort. I am referring to this new Cycle Requirement which I have heard for the first time.
- This Thread is clearly related to your initial Thread on this Topic, and you should not have split it off.
- The above being said, you do not need to calculate the number of occurrences of one String in another. I am referring to the Routine that I created for you which really is not needed.
- The simplest approach is to build the Logic into the Loop itself, namely: if a Match Number MOD 4 = 0 it gets Replaced. Match 1 is handled independently, but if a Match Number is 5, 9, 13, 17 ..., etc. it also gets replaced since it is the start of a New Cycle of 4.
- Study the revised Code Segment which currently allows for 6 Cycles, and download the New Attachment. Again, Unzip 'both' Files to the same Directory.
- Do
-
If InStr(lngNewPos, strText, sSearchText) <> 0 Then
-
lngPosition = InStr(lngNewPos, strText, sSearchText)
-
intNumOfMatches = intNumOfMatches + 1
-
lngNewPos = lngPosition + 1
-
If intNumOfMatches Mod 4 = 0 Or intNumOfMatches = 5 Or intNumOfMatches = 9 Or intNumOfMatches = 13 _
-
Or intNumOfMatches = 17 Or intNumOfMatches = 21 Then
-
strNew = Left$(strNew, lngPosition - 1) & sReplaceText & " " & _
-
Mid$(strNew, lngPosition + Len(sSearchText) + 1)
-
End If
-
Else
-
lngPosition = 0
-
End If
-
Loop Until lngPosition = 0
Well ...
1) I did not know that I would have to 'cycle' until after I had originally posted my question. The XML file I was given was a sample. It's the sort of situation where if I ask the question I get the answer, if I don't then no one voluteers the info.
2) I was actually thinking about the usefulness of the answers for someone who was searching for a specific topic (Counting the number of occurances is not the same as find/replace).
3) I do need to calculate the number of ocurrances because I do not know how many times <cb:TRINGLE> will be in the file. I only want to change the first and fourth, first and fourth, etc. That's why I thought I should count the number of occurances and devide by 4. After that I did not know how to use this info to properly cycle through your code.
I do appreciate your help but ... well if you don't like accumulative questions from newbies perhaps you shoulden't answer them? I am learning something from each of your answers.
Yes, I have typos in my previous message. I think common sense will tell you what I meant.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Simon Verona |
last post by:
Not sure if this is the best group... it may be better off in one of the ADO
groups, but I'm sure somebody here knows the answer:
I'm trying to load up a text file using ADO.net, as follows:
...
|
by: Praveen_db2 |
last post by:
Hi all
Db2 8.1.3 windows
Is there any way to write data into a text file using a stored procedure?
The way we return a cursor output to the calling application, can we
return data in a text...
|
by: asenthil |
last post by:
Hai,
i'm a beginner to java...
just now i had tried to read and write files using java...
and then i had tried to connect a database using jdbc...
now i want to export the data's from a...
|
by: varsha desai |
last post by:
Hello there,
I want to change some data(which is in one line only) of text file using VB 6.0. Which is the best method for it? Another question is I want to delete last two, three lines...
|
by: Phil |
last post by:
I want to add a button to a form to import a text delimited file. The
File is delimited by "^" and so I have created a specification file by
manually impoting the text file, using the advanced...
|
by: charlesbritto |
last post by:
A C++ program for counting individual string in a text file using file handling functions.,
for ex:
if the text file contains,
am a boy,am studying +2,am from chennai
Now the result shoud...
|
by: sajidali |
last post by:
i am trying to append a text file using c#. when i executes my application i writes to the text file using more then one functions. i want to append file only during execution of programe. next time...
|
by: Thiem Teddy |
last post by:
Dear all
I use VB6 to export data into xls or text file, I followed this example ( http://bytes.com/topic/visual-basic/answers/530866-how-export-table-data-into-xls-text-file-using-vb6 ). but I...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
| |