Coldfusion根据txt中的值对行进行排序

时间:2014-11-21 20:20:13

标签: coldfusion

我有一个txt文件,并希望根据每行中的值来排序所有行。

实现这一目标的最佳和最快方法是什么?

以下是我用来编译txt文档的代码:

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

我不确定如果我开始使用数组执行此操作会更好,如果是这样,请提供建议。

我不确定是否可以检查,因为我插入每一行将其插入正确的位置,我以前从未听说过。

如果需要,我还可以在流程结束时循环访问txt文件。

1 个答案:

答案 0 :(得分:4)

使用cfhttp读取csv文件。 name属性创建一个查询对象。您可以使用Q的Q进行排序,然后继续。

详情请参阅cfhttp的文档。