Skip to main content
Participant
December 2, 2008
Question

cfloop within a cfif tag

  • December 2, 2008
  • 5 replies
  • 877 views
Is it possible to nest a cfloop inside a cfif tag?

I'm outputting a directory listing of files, but only want to allow certain file types. I can do something such as:

<cfif dir.name CONTAINS ".cfm" OR dir.name CONTAINS ".html"...>

However, it would be much nicer if I could just store a list of file extensions in a variable and have a cfif statement that loops through them. That would make it much easier to add or remove extensions from the list of allowed file types. Is this doable? If not, is there something similar I could do?
    This topic has been closed for replies.

    5 replies

    Inspiring
    December 4, 2008
    <cfset myfileextlist = "cfm,cfc,htm,html">
    <cfset myfilename = "hello.cfm">
    <cfoutput>#listfindnocase(myfileextlist, listlast(myfilename,
    "."))#</cfoutput>

    Azadi Saryev
    Sabai-dee.com
    http://www.sabai-dee.com/
    Inspiring
    December 3, 2008
    You can nest tags like this:
    <cftag1>
    <cftag2>
    </cftag2>
    </cftag1>

    but not like this
    <cftag1 <cftag2></cftag2>></cftag1>

    For your specific problem, one of the following cold fusion functions might be useful
    find
    findnocase
    refind
    refindnocase
    contains

    Usage is described in the cfml reference manual. If you don't have one, the internet does.
    cstrodtbAuthor
    Participant
    December 3, 2008
    Neither of those are quite doing what I want.

    I don't want to not display certain files, so using cfdirectory isn't going to cut it.

    It seems that listFind can't find a string from the list WITHIN a string, so if I have a list of "cfm,cfc,html,js" and use listFind against a filename such as "hello.cfm", it returns false since "hello.cfm" doesn't EQUAL one of the list elements.

    I could create a variable that only pulls the last three characters of the filename (which is effectively the extension), but this isn't quite what I want, since my list of conditions includes more than just file extensions (it also includes specific files, such as index.cfm and Application.cfm).
    Inspiring
    December 3, 2008
    you know that <cfdirectory> tag has a FILTER attribute, where you can
    define multiple filters for the files you want returned by <cfdirectory>
    query?
    like this:
    <cfdiretory name="mydirquery" directory="#expandpath('.')#"
    filter="*.cfm|*.htm*">

    he above will return all .cfm, .htm and .html files from current
    directory (the one the calling page is in).

    Azadi Saryev
    Sabai-dee.com
    http://www.sabai-dee.com/
    Inspiring
    December 2, 2008
    Hi,

    You don't even need a loop, you can use listfind().

    Here's an example for what I usually use.

    cheers,
    fober



    <cfset application.upload_allow= "txt,doc,xls,ppt,mdb,htm,html,gif,tif,jpg,jpeg,png,zip,pdf,rtf">


    <cfif isdefined("file_upload") IS TRUE>
    <cfif evaluate("len(file_upload)")>
    <cffile action="UPLOAD"
    filefield="file_upload"
    destination="#application.app_content_folder#"
    nameconflict="MAKEUNIQUE">
    <cfif listfind(application.upload_allow, cffile.clientFileExt) Is 0>
    ...delete the file and error message
    </cfif>
    </cfif>
    </cfif>