1

I have to loop over .htm files and search for specific fields in each file. Once field is detected I need to pull the data for that field. Here is example of the data:

Name: John, Miller

and code in htm file looks like this:

<tr>
  <td><u>Name</u></td>
</tr>
<tr>
  <td>John, Mille</td>
</tr>

I tried to use cffile read:

 <cffile action="read" file="\files\someFile.htm" variable="myData">
 <cfoutput>#myData#</cfoutput>

Code above outputted .htm file on the screen. Is there a way to loop over the data in .htm file?

I have tried this with the Coldfusion 9:

<cfset myFile = "\files\someFile.htm">

<cfloop file="#myFile#" index="i" item="line">
    <cfoutput>
        #i#:#line#
    </cfoutput>
</cfloop>

I got this error:

 Attribute validation error for tag CFLOOP.
It has an invalid attribute combination: file,index,item. Possible combinations are:
Required attributes: 'file,index'. Optional attributes: 'charset,from,to'.
Required attributes: 'index,list'. Optional attributes: 'delimiters'.
Required attributes: 'group'. Optional attributes: 'endrow,groupcasesensitive,startrow'.
Required attributes: 'group,query'. Optional attributes: 'endrow,groupcasesensitive,startrow'.
Required attributes: 'query'. Optional attributes: 'endrow,startrow'.
Required attributes: None. Optional attributes: None.
Required attributes: 'array,index'. Optional attributes: None.
Required attributes: 'characters,file,index'. Optional attributes: 'charset'.
Required attributes: 'collection,item'. Optional attributes: None.
Required attributes: 'condition'. Optional attributes: None.
Required attributes: 'from,index,to'. Optional attributes: 'step'. 

If anyone knows how to loop over the data in htm file please let me know. Thank you.

James A Mohler
  • 11,060
  • 15
  • 46
  • 72
espresso_coffee
  • 5,980
  • 11
  • 83
  • 193

1 Answers1

1

I think the problem is CF9 not supporting the ITEM attribute. ITEM is fairly new.

No troubles. You can parse this thing the old fashioned way. Load the file as a list, using the line breaks as the delimiter.

<cfloop index="line" list="#myFile#" delimiters="#chr(10)##chr(13)#">
<cfoutput>
    #line#
</cfoutput>
</cfloop>
  • `Item` is not new, It's been available when looping over collections and structures since at least version 4.5, the one I started with. – Dan Bracuk Oct 11 '17 at 23:33
  • 1
    To clarify, specifically the Item attribute when looping over a file – The Megatron Man Oct 12 '17 at 01:54
  • Is there a way to point to a specific row and td cell to get the data? – espresso_coffee Oct 12 '17 at 13:05
  • Make a counter variable and add 1 inside the loop, you can use that to refer to specific line numbers. If you need to grab specific cells, that would really be its own question to explain in depth. Could use string functions Find() and Mid(), or, could load the string into another list CFLOOP, using as delimiter. – The Megatron Man Oct 12 '17 at 19:20
  • Can you provide an example for any of the methods you have mentioned above? – espresso_coffee Oct 12 '17 at 19:52