Reply to topic
FileSystem Object
eriweb


Joined: 06 Apr 2004
Posts: 68
Reply with quote
Hi all,

We have legal pdf documents that we allow our members to download. The naming convention that we use on these documents is <4 digit document #> _ + <document title> _ ".pdf".

The 4 digit document number is passed in as a query string and I am trying to grab those digits and search in a directory for that document. So something like "Search in this directory for a file with these 4 digits on its file name". Now I am not sure if there is a way to search using maybe FileSystemObject with Coldfusion or something else. Any help in this is really appreciated. Thanks in advance.
Jason101
Forum Regular

Joined: 14 Mar 2006
Posts: 548
Location: Harrisburg, PA
Reply with quote
You could use <cfdirectory>, and then query the returned query "FileName" Feild.

You can then use ListFirst(list [, delimiters ]) to get the first element (Your 4 digit number) Just use "_" as the delimiter.

If you have more than say 100 files in the directory, I would avoid using cfdirectory. I also use java objects to read from the filesystem it is 1000 times faster than cfdirectory. Let me know if you need example code.
FSO Object
eriweb


Joined: 06 Apr 2004
Posts: 68
Reply with quote
Thank you very much for your reply Jason.

Yea if you don't mind I would appreciate a sample code.

I tried the following code:
Code:

<cfdirectory action="list" filter="" directory="dir. path" name="getFiles">

<cfset strDelimeter = "0814_">

<cfoutput query="getFiles">
   #ListFirst(name, strDelimeter)#
</cfoutput>

This did return the one I wanted but it also returned other files that did have the "0814_" in the filename.
Correction
eriweb


Joined: 06 Apr 2004
Posts: 68
Reply with quote
Sorry on the previous post I meant to say there were returned results that did not have the "0814_" in the filename.
Re: FSO Object
Jason101
Forum Regular

Joined: 14 Mar 2006
Posts: 548
Location: Harrisburg, PA
Reply with quote
eriweb wrote:
I tried the following code:
Code:

<cfdirectory action="list" filter="" directory="dir. path" name="getFiles">

<cfset strDelimeter = "0814_">

<cfoutput query="getFiles">
   #ListFirst(name, strDelimeter)#
</cfoutput>

This did return the one I wanted but it also returned other files that did have the "0814_" in the filename.


Try to do a query or queries to get your results like as follows:

Code:

<cfquery name="FindFile" dbtype="query">
   SELECT
        Name,
        Directory
    FROM
          GetFiles
    WHERE
         Name LIKE '0814_%'
</cfquery>


That should bring you back what you want. Since you're queying the set of data for file LIKE 0814.
Jason101
Forum Regular

Joined: 14 Mar 2006
Posts: 548
Location: Harrisburg, PA
Reply with quote
Ok, I use java.io.File when i really need it. I have a directory with about 8,000+ images and it returns them in about 2 seconds. CFDirectory times out trying to return the same set.

Here you go..

Code:

<cfset Images = createObject("java", "java.util.Arrays").asList(createObject("java", "java.io.File").init(Directory).list()) />



That creates the java object. This will not return a fancy query like CFDirectory. It is just going to return a Coldfusion 1d array. It won't tell you if the item is a directory, or a file. Just try it and do a CFDump and see what it returns

I distinguish files from dirs by looking if there is a file extension. If there is not a file extension, I assume it is a directory. Like So: (Assuming your file extensions are only 3 characters long)

While Looping through the array

Code:

<cfloop index="a" from="1" to="#ArrayLen(Images)#">
     <cfset thisFileExt= Right(Images[a], 3)>
     <cfoutput>File Name: #Images[a]# - Ext: #thisFileExt#<br /></cfoutput>
</cfloop>



To Sort the array by file name (since it comes back unsorted)

Code:

<cfset tmp = ArraySort(Images, "textnocase", "asc")>


Hoep this helps.
Thanks...
eriweb


Joined: 06 Apr 2004
Posts: 68
Reply with quote
Thank you very much Jason, you rock dude Wink .
FileSystem Object
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All times are GMT  
Page 1 of 1  

  
  
 Reply to topic