• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

CF10 - CFLoop with a File - Only read in first row

New Here ,
Jul 30, 2013 Jul 30, 2013

Copy link to clipboard

Copied

Hi,

I'm trying to read the header row from a very large(83k) tab delmited file.  I want to automate an upload to a temp table to append data, but I need to know the column headers to set up the table for Bulk Insert.

How can I read in the only the first row?  CFSpreadsheet doesn't work with XSLX files, especially 83k row ones, it would just crap out.  So I'm going the tab delmited route here.

Thanks

Views

1.7K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jul 30, 2013 Jul 30, 2013

Copy link to clipboard

Copied

Use CFLOOP and exit after the first row.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 30, 2013 Jul 30, 2013

Copy link to clipboard

Copied

I'm trying not to read the entire file into memory.

<cfloop index="line" file"file.txt" from="0" to="1">

                    #i#<br>

          </cfloop>

This doesn't seem to work.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jul 30, 2013 Jul 30, 2013

Copy link to clipboard

Copied

wrong CFLOOP syntax - you want FILE= attribute.  it will loop through the file one row one each pass, so if you exit the loop after the first row, all it will read is the first row.  It definitely does not read the entire file into memory.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 30, 2013 Jul 30, 2013

Copy link to clipboard

Copied

LATEST

SGroty wrote:

I'm trying not to read the entire file into memory.

<cfloop index="line" file"file.txt" from="0" to="1">

                    #i#<br>

          </cfloop>

This doesn't seem to work.

You need the full path to the text file. You should also break out of the loop immediately after, as you only need the first line.

<cfloop index="line" file="#expandPath('.')#\file.txt">

<cfoutput>#line#</cfoutput>

<cfbreak>

</cfloop>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jul 30, 2013 Jul 30, 2013

Copy link to clipboard

Copied

Is the file in a web-accessible location?  If so, a quick way is to use CFHTTP to read the CSV directly into a ColdFusion query object. Then you can use that query object to populate your temp table.  See Dave Fergusons's blog entry on this: http://blog.dkferguson.com/index.cfm/2011/9/28/CSV-File-Reading-using-cfhttp.

Otherwise, open the file using CFFILE and loop throught the contents (exiting after the first row if necessary).

HTH,

-Carl V.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 30, 2013 Jul 30, 2013

Copy link to clipboard

Copied

83,000 rows is too much to loop over.

That's why I want to grab the header row, dynamically build a SQL Statement to build a table(temporary one), then pass a BULK INSERT query to the SQL server to import the tab deliminited file.  Append the necessary data to the table, export it to the user, then DROP the table.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation