Coldfusion insert script from an external text file

Here is my problem and I cant seem to figure out why this is happening.  I had this all working but the person that maintains my text file made some changes and took out field number 8 so I had to modify my code:

Here is my code:

<CFFILE ACTION="READ" FILE="#Path#" VARIABLE="datavar">


<CFSET crlf = "~">
<CFLOOP INDEX="orec" LIST="#datavar#" DELIMITERS="#crlf#">

<cfif len(trim(orec)) >

<cfquery name="getjobnumberInfo" datasource="TEST" dbtype="TEST">
      select max(jid) as NaxNum from Jobs
</cfquery>




<CFSCRIPT>
         Jobnumber = ListGetAt(orec, 1,"|");
         Jobtitle = ListGetAt(orec, 2,"|");
         Location = ListGetAt(orec, 3,"|");
         State = ListGetAt(orec, 4,"|");
         Company = ListGetAt(orec, 5,"|");
         Jobfield2 = ListGetAt(orec, 6,"|");
         Website = ListGetAt(orec, 7,"|");
</CFSCRIPT>

HERE IS THE FILE IT COMES FROM:

10000|POSITION|CITY|STATE|COMPANY|JOBFIELD2|URL ~


ERROR IM GETTING:

The web site you are accessing has experienced an unexpected error.
Please contact the website administrator.

The following information is meant for the website developer for debugging purposes.  

Error Occurred While Processing Request  
Invalid list index 7.  
In function ListGetAt(list, index [, delimiters]), the value of index, 7, is not a valid as the first argument (this list has 6 elements). Valid indexes are in the range 1 through the number of elements in the list.  
 
The error occurred in D:\get_jobs.cfm: line 38
 
36 :          Company = ListGetAt(orec, 5,"|");
37 :          Jobfield2 = ListGetAt(orec, 6,"|");
38 :          Website = ListGetAt(orec, 7,"|");
39 : </CFSCRIPT>
40 :                  

 
 
LVL 2
ostashenpAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dgrafxCommented:
obviously - somewhere in the text file there is a list that does not have 7 entries as delimited by |

so you need to catch this error so you can continue

replace your cfscript with:
<cftry><cfset Jobnumber = ListGetAt(orec, 1,"|")><cfcatch><cfset Jobnumber = ""></cfcatch></cftry>
<cftry><cfset Jobtitle = ListGetAt(orec, 2,"|")><cfcatch><cfset Jobtitle = ""></cfcatch></cftry>
<cftry><cfset Location = ListGetAt(orec, 3,"|")><cfcatch><cfset Location = ""></cfcatch></cftry>
<cftry><cfset State = ListGetAt(orec, 4,"|")><cfcatch><cfset State = ""></cfcatch></cftry>
<cftry><cfset Company = ListGetAt(orec, 5,"|")><cfcatch><cfset Company = ""></cfcatch></cftry>
<cftry><cfset Jobfield2 = ListGetAt(orec, 6,"|")><cfcatch><cfset Jobfield2 = ""></cfcatch></cftry>
<cftry><cfset Website = ListGetAt(orec, 7,"|")><cfcatch><cfset Website = ""></cfcatch></cftry>
0
gdemariaCommented:

 Coldfusion ignore blanks in its lists.  So, if no entry is made in JobField2, the number of items on the list will be 6 not 7.  Therefore, your code will throw that error.

 The way around this is to preprocess every line to ensure there is a value for every item.   I do this by placing the word "NULL" between empty delimiters.  Like this:

 <cfset orec = replace(orec,"||,"|NULL|","all")>

Of course, when assigning into the variable, you have to take the extra step to look for NULL to get rid of it..

 <cfset Jobnumber = ListGetAt(orec, 1,"|")>
 <cfif  Jobnumber is "NULL">
     <cfset JobNumber  = "">
 </cfif>

Yes, It's a pain!


 (You can replace "NULL" with anything that's unlikely to be in the datafile such as  $E#M#P#T#Y$   - use a variable to make it easier :)

0
gdemariaCommented:

dgrafx,  I think your approach will allow the values to be misaligned.  If an element is missing in the 4th position, the 5th value will be assigned to the 4th element and so on..  The error won't occur until the 7th position.  I think your solution would make the 7th item empty while the other items would shift their values into the wrong variables.

0
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

dgrafxCommented:
what you say is correct gdemaria

the bottom line though is that this text file is formatted incorrectly.
It's not just a matter of "the 8th element has been removed"
Your "text file maintainer guy" #$%&@ it up ...
0
gdemariaCommented:

 Yes, it is entirely possible the text file is corrupt or not propperly structure.

 But don't count out the empty element, it would cause the error.  See this example..

 JOB87|President|Dallas|TX|ACME Corp||www.google.com

 The above data record shows there is no value for JobField2 (the second to last element).  The record is a valid data record, however to coldfusion there are only 6 elements in it.  To Excel, for example, there are 7.   So the work-around I described would be needed to "see" all seven elements.
 
 JOB87|President|Dallas|TX|ACME Corp|NULL|www.google.com

 Now there are 7 elements, the blank one is using a 'place holder'
0
dgrafxCommented:
good point
i was going off of when poster said that it was all working fine when they had 8 fields and now reworked so there are only 7
so i then assumed that this particular problem was a formatting error
0
ostashenpAuthor Commented:
This whole project is a total nightmare.... Let me try the upload again today and will go from there.
0
ostashenpAuthor Commented:
Ok, how to I tell it to skip that line in the text file if all 7 elements are not there ?
0
gdemariaCommented:

 Just wrap all your processing in this CFIF statement..

 <cfif listLen(orec,"|") eq 7>

 ......... process the record ...

 </cfif>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gdemariaCommented:

 can you open your text file and search for ||  (that's two pipes together)

 If you find them, you have empty values in your data and you need to run

 <cfset datavar = replace(datavar,"||","|NULL|","all")>


 If you don't find them, then the CFIF will handle "bad" data records.
0
ostashenpAuthor Commented:
Thank you very much for your time and patience.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Servers

From novice to tech pro — start learning today.