Skip to main content
Known Participant
August 13, 2008
Question

Need Help finding a char from a flat file

  • August 13, 2008
  • 3 replies
  • 738 views
My CFSCRIPT need to find a character from a flat file.
The flat file look something like this:
20;193236;1;KUMAR;RETESTDEFECT215;;STRETE;;;SPRINGFIED;MA;01111;;;Contract_20080715-2_10093_88
20;193236;1;DIANE;SMITH;;STRETE;;;SPRINGFIED;MA;01111;;;Contract_20080715-2_10093_88
20;193236;1;ROBERT;REYES15;;STRETE;;;SPRINGFIED;MA;01111;;;Contract_20080715-2_10093_88
etc...

I need to determine whether number "20" in the file exist. How can I do this? can anyone help?

<cfscript>
IsRecordExist=Search("fileContent", 20) ?????
</cfscript>

<cfoutput>
#IsRecordexist#
</cfoutput>




This topic has been closed for replies.

3 replies

Inspiring
August 14, 2008
The substring is the first parameter in the find() syntax. I have used sstrg as a general search item below:

<cfsavecontent variable="fileContents">
20;193236;1;KUMAR;RETESTDEFECT215;;STRETE;;;SPRINGFIED;MA;01111;;;Contract_20080715-2_10093_8820;193236;1;DIANE;SMITH;;STRETE;;;SPRINGFIED;MA;01111;;;Contract_20080715-2_10093_8820;193236;1;ROBERT;REYES15;;STRETE;;;SPRINGFIED;MA;01111;;;Contract_20080715-2_10093_88
</cfsavecontent>

<!--- Specify the item you are searching for --->
<cfset sstrg = 20 />

<!--- Trim to take care of unwanted leading spaces --->
<cfscript>
IsRecordExist= Find(sstrg, LTrim(fileContents));
</cfscript>

<!--- Output result --->
<cfoutput>
<cfif IsRecordExist EQ 0>
<strong>#sstrg#</strong> does not exist in the file.
<cfelse>
The first occurrence of <strong>#sstrg#</strong> is at position #IsRecordExist# in the file.
</cfif>
</cfoutput>
aleckenAuthor
Known Participant
August 15, 2008
First I like to say thank you for the responses. I tried the suggestion and it is working but not as expected especially on case where the content also have char 20 somewhere lese for example in case like this:
<cfsavecontent variable="filecontents">
20;193236;1;KUMAR;REYES;;STR;;;SPRINGFIED;MA;01111;;;Contract_20080715-2_10093_2-88.pdf
</cfsavecontent

The char 20 is not only found in the begining of the string but may also exist in the middle of the string:Contract_ 20080715-2_10093_2-88.pdf and may be somewhere else.
I only need to detect the first char from my feed to make sure if this record exist.

Inspiring
August 14, 2008
Also, if would you like to have the number of occurences of the string "20" in your file, you may try this UDF,

http://cflib.org/udf/FindOccurrences
Inspiring
August 13, 2008
Won't any of the string functions find(), reFind(), listFind() or
listContains() do what you want done?
aleckenAuthor
Known Participant
August 13, 2008
I tried this but I got 0 result:

<cfsavecontent variable="fileContents">
20;193236;1;KUMAR;RETESTDEFECT215;;STRETE;;;SPRINGFIED;MA;01111;;;Contract_20080715-2_10093_88
20;193236;1;DIANE;SMITH;;STRETE;;;SPRINGFIED;MA;01111;;;Contract_20080715-2_10093_88
20;193236;1;ROBERT;REYES15;;STRETE;;;SPRINGFIED;MA;01111;;;Contract_20080715-2_10093_88
</cfsavecontent>

<cfscript>
IsRecordExist= Find("fileContents", 20);
</cfscript>

<cfoutput>
#IsRecordExist#
</cfoutput>

RESULT: 0