Having problems with your account or logging in?
A lot of changes are happening in the community right now. Some may affect you. READ MORE HERE

How do I count the number of occurences of a string within a file?

How do I count the number of occurences of a string within a file?

The code as presented here is generic in that it can be used to search for any substring within a line inside any file that can be opened as a textfile. Appended to the end is a variation that allows you to narrow the search to identify two or more unique items within a single line of text.

[-] testcase CountAllTestcases() appstate none
[ ] //Name the variables
[ ] HFILE hFile //Handle to the file
[ ] STRING sFileName //Filename and path
[ ] STRING sLine //Any line within the file
[ ] STRING sSubString //Substring to search for within the line
[ ] FILEMODE fmMode //FileOpen mode
[ ] FILESHARE fShare //Sharing behaviour
[ ] INTEGER iExist //If greater than zero then the target is found
[ ] INTEGER iTotal //Total number of substring instances
[ ]
[ ] //Set the constants
[ ] sFileName="[path (if required) and filename]" // e.g. "c:\Silktest\my_testplan"
[ ] sSubString="[information to search for]" //e.g. "testcase:"
[ ] fmMode=FM_READ //Make the file read-only
[ ] fShare=FS_EXCLUSIVE //Deny access to anyone else
[ ] iTotal=0
[ ] iExist=0
[ ]
[ ] //Simple variant for when each substring target is guaranteed to be unique within a line
[ ] //Loop through the target file searching for instances of the substring
[ ] hFile=FileOpen(sFileName,fmMode,fShare)
[-] while (FileReadLine (hFile,sLine))
[ ] iExist=0
[ ] iExist=StrPos(sSubString,sLine)
[-] if iExist>0
[ ] iTotal++
[ ] FileClose(hFile)
[ ] print("The total number of [search targets] is: "+ str( iTotal))
[ ]
[ ] //Modified variant to search for two or more unique items within a line
[ ] //Loop through the target file searching for instances of the substring
[ ] //When it is found, search the line again for the second substring target
[ ] //Nest as many inner checks as needed to ensure the correct target is hit
[ ]
[ ] hFile=FileOpen(sFileName,fmMode,fShare)
[-] while (FileReadLine (hFile,sLine))
[ ] iExist=0
[ ] iExist=StrPos(sSubString,sLine)
[-] if iExist>0
[ ] iExist=StrPos(sSubString,sLine)
[-] if iExist>0
[ ] iTotal++
[ ] FileClose(hFile)
[ ] print("The total number of [search targets] is: "+ str( iTotal))
[ ]

Old KB# 21241

DISCLAIMER:

Some content on Community Tips & Information pages is not officially supported by Micro Focus. Please refer to our Terms of Use for more detail.
Top Contributors
Version history
Revision #:
1 of 1
Last update:
‎2013-02-15 19:18
Updated by:
 
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.