Dave
Dave

Reputation: 977

Coldfusion Ordering rows according to a value in a txt

I have a txt file and would like to order all the rows according to a value in each row.

What is the best and fastest possible way to achieve this?

Below is the code that I use to compile my txt documents:

<!---CSV FILE--->
<cffile action="read" file="C:/ColdFusion10/cfusion/wwwroot/kelly2/debitorders.csv" variable="csvfile">

<cfoutput>
<!---LOOP THROUGH CSV FILE--->
<cfloop index="index" list="#csvfile#" delimiters="#chr(10)##chr(13)#">

        <!---SET VALUES--->
        <cfset accountholder = "#listgetAt('#index#',1)# #listgetAt('#index#',2)#">
        <cfset accountholderlname = "#listgetAt('#index#',2)#">
        <cfset accountnumber = "#listgetAt('#index#',3)#">
        <cfset accounttype = "#listgetAt('#index#',4)#">
        <cfset bankname = "#listgetAt('#index#',5)#">
        <cfset branch = "#listgetAt('#index#',6)#">
        <cfset amount = "#listgetAt('#index#',7)#">
        <cfset date = "#listgetAt('#index#',8)#">

       <!---SET INITIAL--->
       <cfset initial = "#left(accountholder,1)#">

       <!---SET LAST NAME--->
       <cfset lname_final = "#replace("#accountholderlname#"," ","","all")#">

       <!---GET AND SET ACC TYPE--->
       <cfif accounttype eq "cheque">
            <cfset accounttype_final = "CH">
       <cfelseif accounttype eq "savings">
            <cfset accounttype_final = "SAV">
       <cfelseif accounttype eq "credit">
            <cfset accounttype_final = "CR">
       <cfelse>
            <cfset accounttype_final = "OTH">
       </cfif>

       <!---SET AMOUNT--->
       <cfset amount_final = #round(amount * 100)#>
       <cfset amount_final = #NumberFormat(amount_final,"0000000")#>

       <!---SET DATE--->
       <cfset date_final = "#DateFormat(Date,"ddyyyymm")#">

       <!---TRIM VALUES--->
       <cfset initial = "#Left(initial, 1)#">
       <cfset lname_final = "#Left(lname_final, 14)#">
       <cfset accountnumber = "#Left(accountnumber, 13)#">
       <cfset accounttype_final = "#Left(accounttype_final, 3)#">
       <cfset branch = "#Left(branch, 9)#">
       <cfset amount_final = "#Left(amount_final, 7)#">
       <cfset date_final = "#Left(date_final, 8)#">

       <!---SET STRING LENGTH FOR EACH--->
       <cfset initial = "#LJustify(initial, 1)#">
       <cfset lname_final = "#LJustify(lname_final, 15)#">
       <cfset accountnumber = "#LJustify(accountnumber, 14)#">
       <cfset accounttype_final = "#LJustify(accounttype_final, 3)#">
       <cfset branch = "#LJustify(branch, 10)#">
       <cfset amount_final = "#LJustify(amount_final, 7)#">
       <cfset date_final = "#LJustify(date_final, 8)#">

       <!---SET TOTAL STRING--->
       <cfset total_string = "#initial##lname_final##accountnumber##accounttype_final##branch##amount_final##date_final#">






       <pre> 
       #accountholder#<br>
       #accountnumber#<br>
       #accounttype#<br>
       #bankname#<br>
       #branch#<br>
       #amount#<br>
       #date#<br>
       #initial#<br>
       #lname_final#<br />
       #accounttype_final#<br>
       #amount_final#<br />
       #date_final#<br>
       123456789012345678901234567890123456789012345678901234567890<br />
       #total_string#<br>
       <br />
       <br />
       </pre>





       <!---IF FILE FOR BANK EXISTS--->
        <cfif FileExists(ExpandPath("#listgetAt('#index#',5)#.txt"))>

            <!---READ EXISTING FILE HEADER--->
            <cffile action="read" file="C:/ColdFusion10/cfusion/wwwroot/kelly2/#bankname#.txt" variable="bankheader">
            <!---SPLIT UP THE HEADER TO ADD NEW VALUES ONTO IT--->
            <cfset numericvalue = listfirst(bankheader,chr(13))>
            <cfset numericvalue = #Right(numericvalue, 13)#>

            <cfset RecordCountvalue = #Left(numericvalue, 3)#>
            <cfset RecordCountvalue = #RecordCountvalue# + 1>
            <cfset RecordCountvalue = #NumberFormat(RecordCountvalue,"000")#>
            <cfset RecordCountvalue = #Left(RecordCountvalue, 3)#>
            <cfset RecordCountvalue = #RJustify(RecordCountvalue, 3)#>

            <cfset TotalRecordvalue = #Right(numericvalue, 10)#>
            <cfset TotalRecordvalue = (#TotalRecordvalue# + #amount#) * 100000>
            <cfset TotalRecordvalue = #NumberFormat(TotalRecordvalue,"0000000000")#>
            <cfset TotalRecordvalue = #Left(TotalRecordvalue, 10)#>
            <cfset TotalRecordvalue = #RJustify(TotalRecordvalue, 10)#>

            <!---SET HEADER FOR FILE--->
            <cfset fileheader_bank = "#UCase(bankname)#">
            <cfset fileheader_bank = "#Left(fileheader_bank, 15)#">
            <cfset fileheader_bank = "#LJustify(fileheader_bank, 16)#">
            <cfset newfile_header = "#fileheader_bank##RecordCountvalue##TotalRecordvalue#">

            <pre>
            #numericvalue#<br />
            #RecordCountvalue#<br />
            #TotalRecordvalue#<br />
            #newfile_header#
            </pre>
            <!---APPEND FILE AND ADD UPDATED HEADER--->
            <cfset bankheader = listSetAt(bankheader,1,"#newfile_header#","#chr(13)#")>
            <cffile action="write" 
            fixnewline="no"
            addnewline="no" 
            file="#getDirectoryFromPath(getTemplatePath())#/#listgetAt('#index#',5)#.txt" 
            output="#bankheader#">

            <!---APPEND FILE AND ADD NEW ENTRY--->
            <cffile action = "append"
            fixnewline="no"  
            file = "C:/ColdFusion10/cfusion/wwwroot/kelly2/#listgetAt('#index#',5)#.txt"  
            output = "#total_string#">


        <!---IF FILE FOR BANK DOES NOT EXIST--->
        <cfelse>
            <!---SET HEADER FOR FILE--->
            <cfset fileheader_bank = "#UCase(bankname)#">
            <cfset fileheader_bank = "#Left(fileheader_bank, 15)#">
            <cfset fileheader_bank = "#LJustify(fileheader_bank, 16)#">
            <cfset newfile_header = "#fileheader_bank#001000#amount_final#">

            <!---CREATE NEW FILE WITH BANK NAME--->
            <cffile action = "write"
            fixnewline="no"  
            file = "C:/ColdFusion10/cfusion/wwwroot/kelly2/#listgetAt('#index#',5)#.txt"  
            output = "#newfile_header#">

            <!---APPEND FILE AND ADD NEW ENTRY--->
            <cffile action = "append"
            fixnewline="no"  
            file = "C:/ColdFusion10/cfusion/wwwroot/kelly2/#listgetAt('#index#',5)#.txt"  
            output = "#total_string#">

        </cfif>
</cfloop>
</cfoutput>

I am not sure if it would be better if I start using an Array to do this, if so please advise.

I am not sure if it is possible to check as I insert each row to insert it into the correct place, I have never heard of this before.

I could also loop through the txt files at the end of the process if need be.

Upvotes: 0

Views: 67

Answers (1)

Dan Bracuk
Dan Bracuk

Reputation: 20804

Read the csv file using cfhttp. The name attribute creates a query object. You can use Q of Q to do your sort and then carry on.

The details are in the documentation for cfhttp.

Upvotes: 4

Related Questions