Skip to main content
June 19, 2013
Answered

Read files based on date selection

  • June 19, 2013
  • 1 reply
  • 574 views

Hi,

I have requirement such that on server there some files for example:  text20101231, text20111231, text20121231 and text (Note: Current year file does not have any suffix in its name i.e. text is current year file).

Now in UI user can select 2 dates: From Date and To Date.

According to the selected date files should read in my code.

For ex: if user selects From Date: 6/13/2011 and To Date: 11/31/2012 from the calender then my code should read only  text20111231 and text20121231 two files.

I will also check if year in To Date is equal to current year it will read the file in which it does not have any suffix in its name.

Kindly suggest how to read the concerned files based upon the date selection.

Thank you.

    This topic has been closed for replies.
    Correct answer duncancumming

    So I'd say start with a cfdirectory of the whole directory, to get all the files.  This returns a query object.  You can then use this to do a query-of-queries on, e.g. based on your date criteria. 

    <cfset form.yearFrom = 2010> 

    <cfset form.yearTo = 2012>

    <cfdirectory name="getAllFiles" directory="/your/folder/" type="file" filter="*.txt">

    <cfquery name="getYearFiles" dbtype="query">

      SELECT *

      FROM getAllFiles

      WHERE 1=0

      <cfloop index="i" from="#form.yearFrom#" to="#form.yearTo#">

        OR  name like 'text#i#%'

      </cfloop>

    </cfquery>

    <!--- loop over getYearFiles, read each file --->

    <!--- if year = current year, just read 'text' --->

    <cfif form.yearTo EQ year(now())>

      ...

    </cfif>

    1 reply

    duncancummingCorrect answer
    Participating Frequently
    June 19, 2013

    So I'd say start with a cfdirectory of the whole directory, to get all the files.  This returns a query object.  You can then use this to do a query-of-queries on, e.g. based on your date criteria. 

    <cfset form.yearFrom = 2010> 

    <cfset form.yearTo = 2012>

    <cfdirectory name="getAllFiles" directory="/your/folder/" type="file" filter="*.txt">

    <cfquery name="getYearFiles" dbtype="query">

      SELECT *

      FROM getAllFiles

      WHERE 1=0

      <cfloop index="i" from="#form.yearFrom#" to="#form.yearTo#">

        OR  name like 'text#i#%'

      </cfloop>

    </cfquery>

    <!--- loop over getYearFiles, read each file --->

    <!--- if year = current year, just read 'text' --->

    <cfif form.yearTo EQ year(now())>

      ...

    </cfif>

    June 20, 2013

    Thanks!!!

    It worked for me