Team

 

Came across this component below a while back off of one of the many MM Blogs.   One of the developer’s in house posted it to their blog – but I can’t remember… I believe Sean Corfield made note of it on his blog – but can’t find reference to it there either.

 

Just trying to give credit where credit is due – Ive been using it in an application Im developing and it peforms beautifully -  ;)

 

Best Regards

 

 

 

<cfcomponent>

 

<cffunction access="remote" name="getDirList" displayName="getDirList" hint="Get a directory listing" returnType="query" output="false">

  <cfargument name="directory" type="string" required="false" default="C:\" displayName="directory" hint="The directory to list">

 

  <cfscript>

      // Create and initialize a File object

      files = CreateObject( "Java", "java.io.File" );

      files.init( directory );

 

      // Get a list of all the files in this directory

      list = files.list();

 

      // Build a query to hold the list in a native CF format

      query = QueryNew( "attributes, datelastmodified, mode, name, size, type" );

 

      // Prepare Java objects that give additional data

      date = CreateObject( "Java", "java.util.Date" );

 

      modify = CreateObject( "Java", "java.util.Calendar" );

      cal = modify.getInstance();

 

      // Loop over the provided listing

      for( f = 1; f LTE ArrayLen( list ); f = f + 1 ) {

        // File in current iteration

        files.init( directory & "\" & list[f] );

 

        // Make sure this is a directory and not a file

        if( files.isDirectory() ) {

          // Get the date last modified

          date.init( files.lastModified() );

 

          // Prepare to get specific data from the stamp

          cal.setTime( date );

 

          // Use Calendar object methods to get specific elements

          modify = CreateDateTime( cal.get( 1 ), cal.get( 2 ) + 1, cal.get( 5 ), cal.get( 11 ), cal.get( 12 ), cal.get( 13 ) );

 

          // Add the data to the query object

          hold = QueryAddRow( query );

          hold = QuerySetCell( query, "attributes", "" );

          hold = QuerySetCell( query, "datelastmodified", DateFormat( modify, "mm/dd/yyyy" ) & " " & TimeFormat( modify, "hh:mm:ss tt" ) );

          hold = QuerySetCell( query, "mode", "" );

          hold = QuerySetCell( query, "name", list[f] );

          hold = QuerySetCell( query, "size", files.length() );

          hold = QuerySetCell( query, "type", "Directory" );

        }

      }

    </cfscript>

 

    <!--- Return the finished and populated query --->

    <cfreturn query>

</cffunction>

 

 

 

<cffunction access="remote" name="getFileList" displayName="getFileList" hint="Get a file listing" returnType="query" output="false">

    <cfargument name="directory" type="string" required="false" default="C:\" displayName="directory" hint="The directory to list">

    <cfargument name="filter" type="string" required="false" default="*.*" displayName="filter" hint="The type of files to list">

 

    <!--- Since this gives us only files on Windows, it works fine here --->

    <cfdirectory directory="#directory#" action="" name="list" filter="#filter#">

 

    <!--- Return the list of files --->

    <cfreturn list>

</cffunction>

 

 

 

<cffunction access="remote" name="getFullList" displayName="getFullList" hint="Get a file data listing" returnType="query" output="false">

  <cfargument name="directory" type="string" required="false" default="C:\" displayName="directory" hint="The directory to list">

  <cfargument name="filter" type="string" required="false" default="*.*" displayName="filter" hint="The type of files to list">

 

  <!--- Run the existing functions to get the separate data --->

  <cfset dirs = getDirList( directory )>

  <cfset files = getFileList( directory, filter )>

 

  <!--- Create a new query in which to store the combined data --->

  <cfset merge = QueryNew( "name, type, full, size, datelastmodified" )>

 

  <!--- Loop over the directory results --->

  <!--- I added a "full" option for my particular uses --->

  <cfloop query="dirs">

    <cfset hold = QueryAddRow( merge )>

    <cfset hold = QuerySetCell( merge, "name", name )>

    <cfset hold = QuerySetCell( merge, "type", "directory" )>

    <cfset hold = QuerySetCell( merge, "full", directory & "\" & name )>

  </cfloop>

 

  <!--- Loop over the files results --->

 <cfloop query="files">

    <cfset hold = QueryAddRow( merge )>

    <cfset hold = QuerySetCell( merge, "name", name )>

    <cfset hold = QuerySetCell( merge, "size", size)>

    <cfset hold = QuerySetCell( merge, "datelastmodified", DateFormat( modify, "mm/dd/yyyy" ) & " " & TimeFormat( modify, "hh:mm:ss tt" ))>

    <cfset hold = QuerySetCell( merge, "type", filter )>

    <cfset hold = QuerySetCell( merge, "full", directory & "\" & name )>

  </cfloop>

 

  <!--- Return the combined results --->

  <cfreturn merge>

</cffunction>

 

</cfcomponent>

This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you have received this email in error please notify the system manager. This message contains confidential information and is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail.

Reply via email to