Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Parsing a Text File -- Solution here

1 view
Skip to first unread message

Alex

unread,
Sep 26, 2003, 11:17:42 AM9/26/03
to
Hello,

I'd been trying to find a quick and simple way to parse a text file
which contains variables, but I've been unable to find any command in
Cold Fusion that does this easily. There might be one out there, and
if so, please someone reply. But until then, I've written some code
that does the dirty work.

Below is the text file it parses. In my example, it's variables.txt:
Title=KPMG File Directory&
Description=This directory contains data which can be read-in via a
web browser and viewed.
And this is still part of the Description variable.&
Email=ale...@totallynerd.com

Note, there is an '&' at the end of all variable data except the last
one. I'll probably write something to resolve this later, but this is
quick and dirty for now.

And here's my code to parse this:

<cfoutput>
<!-- Get physical Path on Server into variable FILE_PATH -->
<cfset file_length = #len(CGI.PATH_TRANSLATED)# -
#len(GetFileFromPath(GetBaseTemplatePath()))#>
<cfset file_path = #mid(CGI.PATH_TRANSLATED, 1, file_length)#>

<!-- Read-in variable file variables.txt -->
<cffile action="READ" file="#file_path#\variables.txt"
variable="TheFileContents">

<!-- Parse variables to allow loop to read output correctly -->
<cfset thefilecontents = replace(thefilecontents, chr(13),"|","ALL")>
<cfset thefilecontents = replace(thefilecontents, "&|", chr(13),
"ALL")>

<!-- Loop to assign data to variables from variables.txt -->
<cfloop index="ShowIt" list="#TheFileContents#" delimiters=#chr(13)#>

<!-- Find 'equal' in the line -->
<cfset locate = #find("=",trim(showit))#>

<!-- Find the Variable name -->
<cfset var_name = #trim(mid(trim(showit), 1, locate - 1))#>

<!-- Find the Data -->
<cfset data_length = #len(trim(showit))# - #len(var_name)#>
<cfset data = #right(trim(showit), data_length - 1)#>

<!-- Set the Variable to equal the Data -->
<cfset "#var_name#" = #data#>

</cfloop>
</cfoutput>

I wrote it so you can place this in any directory and it'll pick it
up. This prevents the need to modify the file as you place it in
other directories.

I know this might be ugly, but it's the only way I've been able to
successfully parse a text file. I hope someone can use this as I
found MANY unanswered requests for this when searching the newsgroups.

Take care,

Alex.

0 new messages