Search code examples
listcoldfusioncoldfusion-9

How to find and display directory files by partial name


I have a directory with the following files:

1245_cat.txt
6356_cat.txt
3564_it.txt
9867_it.txt
8437_xp.txt
6252_xp.txt

I am looking for a way to search and display file(s) based on partial name. For instance, if a user enters 1245, I would like to display 1245_cat.txt.

Here is what I have so far:

<cfset fileLocation = "\\doc_1\LOAD">
<cfdirectory directory = "#fileLocation#" 
    name = "archive" 
    type="file"
    filter="*.txt" 
    action = "list">

Do I then create a list: archive_list of all returned files and use listContains(archive_list, "1245_cat.txt")?


Solution

  • Make the filter dynamic:

    Let's say the search field is named "file_name", then

    <cfparam name="form.file_name" type="string" default="">
    <cfset myFilter = form.file_name & "*.txt">
    <cfdirectory directory = "#fileLocation#" name="archive" type="file" 
       filter="#myFilter#" action = "list">