Skip navigation
Currently Being Moderated

How to find a carriage return and Line feed?

Nov 11, 2011 6:04 AM

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

 
Replies
  • Currently Being Moderated
    Nov 11, 2011 6:58 AM   in reply to alecken

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

     
    |
    Mark as:
  • Dave Watts
    747 posts
    Mar 11, 2003
    Currently Being Moderated
    Nov 11, 2011 7:19 AM   in reply to alecken

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Nov 11, 2011 9:29 AM   in reply to alecken

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Nov 11, 2011 11:54 AM   in reply to alecken

    If your only problem is a missing carraige return on the last line of the file, you might be overengineering something.  If you do this:

     

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

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

     

    Then you will always get the what follows the last carraige return in the file.

     
    |
    Mark as:
  • Currently Being Moderated
    Nov 11, 2011 12:50 PM   in reply to Dan Bracuk

    First, make sure you're talking about the correct ASCII codes -

     

    CHR(10) = New Line Feed

    CHR(13) = Carriage Return

     

    Dan's solution should work for you as long as you do the following:

     

    1) Remove all chr(10)'s from your text using a replace statement

    2) Loop over your lines one at a time using the list/delimeters method that Dan suggested (I suggest using chr(13) as your delimeter)

    3) In each iteration of your loop, check the list length of you row using ListLen() with the delimeter set for "|".  If the item count in your list does not match your expected value, you know that either the data columns are messed up or someone forgot a line break somewhere.  Either way, you'll probably want to handle that error before you try and process it.

     
    |
    Mark as:
  • Currently Being Moderated
    Nov 24, 2011 8:24 AM   in reply to alecken

    <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>

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points