Skip to main content
Known Participant
November 11, 2011
Question

How to find a carriage return and Line feed?

  • November 11, 2011
  • 5 replies
  • 9596 views

I need to detect if a crriage retunr and line feed present on each row in my text file before allowing user to upload the text file

I know how to remove them from each line but what I need is to detect them no removing them from file.

Our concern is when the a row does not have carriage return and line feed.

Is there a way to find this using CF? Please help

thanks

This topic has been closed for replies.

5 replies

Participating Frequently
November 24, 2011

<cffile action="read" variable="theFileContents">

<cfloop list="#theFileContents#" delimiters="#chr(13)##chr(10)#" index="currentRow">

     <cfif listLen("#currentRow#","|") gt  11> <!---if  greater than the field count>

              Show error to user and abort

     <cfelse>

           Proceed to next step

     </cfif>

</cfloop>

aleckenAuthor
Known Participant
November 14, 2011

Thank you guys!

aleckenAuthor
Known Participant
November 11, 2011

That is exactly the problem Dave, if some rows do not have line feed, they're not being inserted into DB, it skips other steps and go right to finish. It looks

like the file is done processing.

So I need to do early rejection to this kind of file.

I used cfloop to catch anything else that do not follow the formatting rules.

Here is what I already done:

Right after the file got uploaded, I have these codes:   

<cfloop  index="x" file="#MyFile#" >

  <CFSET

MyLine = Replace(x,chr(9),"|", "ALL")>

  I'm using a few functions, such as, Left(), Mid(), Right(), etc to make sure certain rules are followed.

  Up till here, it's done and tested, everything is working.

 

  The only problem is when 1 or more rows don't end up with line feed and or carriage return.

  I plan to add some codes to check the end of each row for chr(10) since it is looping for every row anyway.

  If detected, process file normally otherwise stop processing.

</cfloop>

So this can't be done in CF?

Inspiring
November 11, 2011

That part can be done in cf.  In fact, we have a similar scheduled job in place.  Our problem is not so much missing carraige returns, but extra ones.

We do something like this:

read the file into a variable.  let's call it theFile

loop through the file and

get the position of what we expect to see at the start of each line.  In our case, it's a 7 digit number so we use refind().

Use left() to create a variable called thisRow for everything up to that point.

Use right() to take thisRow out of theFile

process this row.

If we get rows with bad data, we set them aside in another variable and then mail that variable to ourselves.

aleckenAuthor
Known Participant
November 11, 2011

Dan,

My row has to end up with either Chr(10) OR chr(13) & chr(10) and the difficulty that I'm currently facing is how to detect if chr(10) or chr(13) and chr(10) does not exist following the last item on the list.

The last item on the list is always a blank space or an emtpry string.

Ilustration 1,  is the structure of accepted row:

UU| value | value | value | value | value | value | value | value | value | blank (empty string) then follows by chr(10) or chr(13)chr(10)

Ilustration 2,  is the structure of not-accepted row:

UU| value | value | value | value | value | value | value | value | value | blank

So my logic should be:

If chr(10) is detected, Proceed to normal steps

else

show error to user and abort

My difficulty is on what should I use to detect chr(10). I tried to use getToken() but it doesn't seem to work

In your situation, your last item on the list may always have value. On my case, the last item on the row is always blank value

Community Expert
November 11, 2011

To add to Dan's absolutely correct answer, you can only do this with client-side technologies, and JavaScript typically can't do this either for security reasons. But why can't you just do it after the file has been uploaded? You could then report back to the user any errors.

Dave Watts, CTO, Fig Leaf Software

Dave Watts, Eidolon LLC
Inspiring
November 11, 2011

You don't get to inspect the file until after you upload it.