Excluding items from a list in coldfusion by type

293 views Asked by At

Is there a way to exclude certain items by filetype in a list in Coldfusion? Background: I just integrated a compression tool into an existing application and ran into the problem of the person's prior code would automatically grab the file from the upload destination on the server and push it to the Network Attached Storage. The aim now is to stop their NAS migration code from moving all files to the NAS, only those which are not PDF's. What I want to do is loop through their variable that stores the names of the files uploaded, and exclude the pdf's from the list then pass the list onto the NAS code, so all non pdf's are moved and all pdf's uploaded remain on the server. Working with their code is a challenge as no one commented or documented anything and I've been trying several approaches.

    <cffile action="upload" destination= "c:\uploads\" result="myfiles" nameconflict="makeunique" >

    <cfset fileSys = CreateObject('component','cfc.FileManagement')>

    <cfif Len(get.realec_transactionid)>
        <cfset internalOnly=1 >
    </cfif>     
    **This line below is what I want to loop through and exclude file names 
    with pdf extensions **
    <cfset uploadedfilenames='#myfiles.clientFile#' >

    <CFSET a_insert_time = #TimeFormat(Now(), "HH:mm:ss")#>
    <CFSET a_insert_date = #DateFormat(Now(), "mm-dd-yyyy")#>
    **This line calls their method from another cfc that has all the file 
    migration methods.**
    <cfset new_file_name = #fileSys.MoveFromUploads(uploadedfilenames)#>
    **Once it moves the file to the NAS, it inserts the file info into the 
    DB table here**
    <cfquery name="addFile" datasource="#request.dsn#">
        INSERT INTO upload_many (title_id, fileDate, filetime, fileupload)
                VALUES('#get.title_id#', '#dateTimeStamp#', '#a_insert_time#', '#new_file_name#')
    </cfquery>


<cfelse>    
    <cffile action="upload" destination= #ExpandPath("./uploaded_files/zip.txt")# nameconflict="overwrite" >
</cfif>

Update 6/18 Trying the recommended code helps with the issue of sorting out filetypes when tested outside of the application, but anytime its integrated into the application to operate on the variable uploadedfilenames the rest of the application fails and the multi-file upload module just throws a status 500 error and no errors are reported in the CF logs. I've found that simply trying to run a cfloop on another variable not related to anything in the code still causes it to error.

2

There are 2 answers

3
Anurag On BEST ANSWER

As per my understanding, you want to filter-out file names with a specific file type/extension (ex: pdf) from the main list uploadedfilenames. This is one of the easiest ways:

<cfset lFileNames = "C:\myfiles\proj\icon-img-12.png,C:\myfiles\proj\sample-file.ppt,C:\myfiles\proj\fin-doc1.docx,C:\myfiles\proj\fin-doc2.pdf,C:\myfiles\proj\invoice-temp.docx,C:\myfiles\proj\invoice-final.pdf" />
<cfset lResultList = "" />
<cfset fileExtToExclude = "pdf" />
<cfloop list="#lFileNames#" index="fileItem" delimiters=",">
<cfif ListLast(ListLast(fileItem,'\'),'.') NEQ fileExtToExclude>
    <cfset lResultList = ListAppend(lResultList,"#fileItem#") />
</cfif>
</cfloop>

Using only List Function provided by ColdFusion this is easily done, you can test and try the code here. I would recommend you to wrap this code around a function for easy handling. Another way to do it would be to use some complex regular expression on the list (if you're looking for a more general solution, outside the context of ColdFusion).

Now, applying the solution to your problem:

<cfset uploadedfilenames='#myfiles.clientFile#' >
<cfset lResultList = "" />
<cfset fileExtToExclude = "pdf" />
<cfloop list="#uploadedfilenames#" index="fileItem" delimiters=",">
  <cfif ListLast(ListLast(fileItem,'\'),'.') NEQ fileExtToExclude>
      <cfset lResultList = ListAppend(lResultList,fileItem) />
  </cfif>
</cfloop>
<cfset uploadedfilenames = lResultList />
<!--- rest of your code continues --->

The result list lResultList is copied to the original variable uploadedfilenames.

0
TRose On

I hope I'm not misunderstanding the question, but why don't you just wrap all of that in an if-statement that reads the full file name? Whether the files are coming one by one or through a delimited list, it should be easy to work around.

<cfif !listContains(ListName, '.pdf')>

OR

<cfif FileName does not contain '.pdf'>

then

all the code you posted