Solved

coldfusion read plain text file

Posted on 2013-05-28
9
532 Views
Last Modified: 2013-05-28
Here is my situation. Windows 2008, Coldfusion 10. Trying to create some type of basic document management system.

My Xerox 7500 series multi-function machine can scan docs to a network share. The scanned pdf as well as a .XST job log file is saved. The XST file is plain text and contains some info about the job I would like to read. Can coldfusion read this text file??

There is a line MetaDataValue = "123456"

Any ideas? I've read about cffile and maybe using cfloop, just not sure if these are good to use, or what is easiest or best. Thanks for nay help!
0
Comment
Question by:cb_it
  • 5
  • 4
9 Comments
 
LVL 52

Expert Comment

by:_agx_
ID: 39202415
Sure you can read it if it's a text file.  What's the typical file size? As long as it's not a huge file, looping through it is fine. You just need the right delimiter and pattern.

    > There is a line MetaDataValue = "123456"

Is it always in that format ie MetaDataValue = "(something different here)"  ?  Do any other lines start with "MetaDataValue  = "? If it's just the one, you could do:


<cfset fileContent = FileRead("c:\path\to\yourFile.xst")>

<cfset lineToFind = "">
<cfloop list="#fileContent#" index="line" delimiters="#chr(10)##chr(13)#">
      <cfset firstCol = getToken(trim(line), 1, "=")>
      <cfif trim(firstCol) eq "MetaDataValue">
            <cfset lineToFind = line>
      </cfif>
</cfloop>

<cfoutput>
lineToFind = #lineToFind#
</cfoutput>
0
 

Author Comment

by:cb_it
ID: 39202439
Just that 1 line is called MetaDataValue. The number it contains is a number the user is forced to type in before they can scan. For example they would have to input 987654 into the machine for the loan number they are about to scan. Hope that makes sense.

The xst file is small maybe 3-4KB. Here is part of it. There will be many xst files, can CF loop through ALL of these files, and then read the MetaDataValue??

[description xrx_dscrpt_metadata]
1{
* string MetaDataPrompt = "LOAN_NUMBER";
* string MetaDataFieldName = "LOAN_NUMBER";
* boolean MetaDataRequiredEntry = TRUE;
* string MetaDataType = "string";
string MetaDataValue = "123456";
boolean MetaDataValidationRequired = FALSE;
enum_metadatamaskingrequired MetaDataMaskingRequired = NONE;
}
end


I will try your code and report back. Thanks.
0
 

Author Comment

by:cb_it
ID: 39202462
The file is NOT a csv file, I think, so not sure if you are trying to break apart or trim some columns with your code??

I ran your code and just got lineToFind =

Nothing else.
0
 
LVL 52

Expert Comment

by:_agx_
ID: 39202557
It doesn't have to be CSV. You can use cfloop with any delimiter, like a new line in my example. But it won't work anyway, because the line doesn't start with "MetaDataValue" it starts with "string MetaDataValue".

If all the files are that small, I'd recommend a regex instead.  I'm not very good at those. This works with the example above, but I'd suggest getting an opinion from a real regex guru instead :)

<cfset theValue  = "">
<cfset match = reMatchNoCase('MetaDataValue\s+=\s+"([^"]+)"', fileContent)>
<cfif arrayLen(match)>
	<cfset theValue = getToken(match[1], 2, '"')>
</cfif>
<cfoutput>#theValue#</cfoutput>

Open in new window

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 52

Expert Comment

by:_agx_
ID: 39202585
> can CF loop through ALL of these files, and then read the MetaDataValue??


You'd need something like DirectoryList to get the files you want to process. Then loop through the query and run the code above for each file.

       <cfset fileArray = DirectoryList("c:\yourDirectory", false, "path", "*.xst ")>
        <cfloop array="#fileArray#" index="filePath">
              <cfset fileContent = FileRead(filePath)>
              ... code to parse content ....
       </cfloop>

If possible this should be done each time you scan, rather than after the fact ie processing a whole directory of files. If that is not possible, try and do it in small batches, because anything that iterates thousands of times takes a while.
0
 

Author Comment

by:cb_it
ID: 39202607
That works! Incredible, just a fe wlines of code. Here is my test code so far. I'm pulling 2 pieces of info out of the XST file and it's working. This is just reading the 1 xst file called cffile1.xst. How can this read all of the xst files in that one directory??

<cfset fileContent = FileRead("F:\SCANS\cffile1.xst")>

<cfset theValue1  = "">
<cfset match = reMatchNoCase('MetaDataValue\s+=\s+"([^"]+)"', fileContent)>
<cfif arrayLen(match)>
      <cfset theValue1 = getToken(match[1], 2, '"')>
</cfif>

<cfset theValue2  = "">
<cfset match = reMatchNoCase('DocumentObjectName\s+=\s+"([^"]+)"', fileContent)>
<cfif arrayLen(match)>
      <cfset theValue2 = getToken(match[1], 2, '"')>
</cfif>

<cfoutput>#theValue1# #theValue2#</cfoutput>
0
 
LVL 52

Accepted Solution

by:
_agx_ earned 500 total points
ID: 39202637
Just do a directory listing. It'll return an array of files. Then loop through the array, and put your code inside the loop.  Not sure if you saw this, but see here
 

        <cfset fileArray = DirectoryList("F:\SCANS", false, "path", "*.xst ")>
        <cfloop array="#fileArray#" index="filePath">
              <cfset fileContent = FileRead(filePath)>
               
              <cfset theValue1  = "">
              <cfset match = reMatchNoCase('MetaDataValue\s+=\s+"([^"]+)"', fileContent)>
              .... etc...
 
       </cfloop>

Though I'm pretty sure one of the regex guys could come up with something way cooler than my lame expression ;-)
0
 

Author Comment

by:cb_it
ID: 39202691
Woops, sorry, reading too fast and didnt see all of your post above. That array worked like a champ, thanks. Started using CF back in the 90's and have been out of the web game for quite some time.
0
 
LVL 52

Expert Comment

by:_agx_
ID: 39202785
Welcome back. You'll pick it back up in no time.  It's amazing how much its grown. If you want a laugh, take a look at the function list for CF4.5 vs CF9 some time. Boy what a difference!
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hi, I will be creating today a basic tutorial on how we can create a Mail Custom Function and use it where ever we want. The main advantage about creating a custom function is that we can accommodate a range of arguments to pass to the Function and …
PROBLEM: How to add your own buttons to the bottom toolbar with paging info ( result count ). While creating a cfgrid, I ran into an issue where I wanted to embed my own custom buttons where the default ones ( insert / delete / etc… ) are for aes…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now