• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 390
  • Last Modified:

cfdirectory - files with the same name - different extensions

I am using the following to display files that are hyperlinks.  Some departments have a .doc and a .pdf file with the same name.  They only want to display the .pdf if it exists otherwise display the .doc file.  Thanks for any help!

<body>
<cfdirectory name="getLinks" directory="d:\inetpub\wwwroot\docs\documents\#passname#\">

<cfoutput query="getLinks">
  <a href="../documents/#passname#/#getLinks.Name#" target="_blank"">#listfirst(getLinks.Name,'.')#</a><br>
</cfoutput>
</body>
0
dmwelter
Asked:
dmwelter
1 Solution
 
kkhippleCommented:
http://livedocs.macromedia.com/coldfusion/6.1/htmldocs/functb14.htm#wp1107735

Have a look at ListLast dmwelter...  

haven't worked with CFDIRECOTRY myself ... but this should work
this assumes you have the file listing in alphabetical order.. then the doc file should show up first and then the pdf file...


<body>
<cfdirectory name="getLinks" directory="d:\inetpub\wwwroot\docs\documents\#passname#\">

<CFSET thisFilename = "">

<cfoutput query="getLinks">

   <!--- check filename if it equals current file then display pdf file --->
   <CFIF ListFirst(getLinks.Name,'.') EQ ListFirst(thisFilename,'.')>
       <a href="../documents/#passname#/#thisFilename#" target="_blank"">#listfirst(getLinks.Name,'.')#</a><br>
   <CFELSE>
    <a href="../documents/#passname#/#getLinks.Name#" target="_blank"">#listfirst(getLinks.Name,'.')#</a><br>
   </CFIF>

   <CFSET thisFilename = getLinks.Name><!--- set filename --->


</cfoutput>
</body>
0
 
pinaldaveCommented:
Hi dmwelter,


<cfloop query="getLinks">
                                                                    <cfloop index = "ListElement" list = "pdf,doc">
                                                                        <cfdirectory
                                                                         action = "list"
                                                                         directory = "d:\inetpub\wwwroot\docs\documents\#passname#\"
                                                                         name = "getLinks"
                                                                         filter = "#trim(getLinks.filename)#.#ListElement#"
                                                                         >
                                                                         
                                                                         <cfif getfilename.recordcount>
                                                                               <a href="../documents/#passname#/#getLinks.Name#" target="_blank"">#listfirst(getLinks.Name,'.')#</a><br>
                                                                      </tr>
                                                                         </cfif>  
<cfexit>          
                                                                    </cfloop>
  </cfloop>
Regards,
---Pinal
0
 
pinaldaveCommented:
Hi dmwelter,
you may need to change few thigns but that is main logic. if i find the file with pdf file i will exit from loop.

Regards,
---Pinal
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
dmwelterAuthor Commented:
I get the following error and don't know how to correct it.  I'm obviously very new to ColdFusion.

Attribute validation error for tag cfloop.  
The value of the attribute query, which is currently "getLinks", is invalid.  
0
 
Tacobell777Commented:
You will need to have a list of file names first.
Lets say that file "someFile.dpf" does not exist, then show the word document, you would need to know the name of the file, i.e. "someFile" before you can create a link to it.

Unless there is only one file in the directory? Example;
use cfdirectory to get the file with a pdf extension, if no recordcount returned then just show the file with extension ".doc", but this is asusming you only have one file in the directory, in which case you could just simply list the file no matter what.
0
 
mosphatCommented:
If I read correctly dmwelter doesn't want to display a link in case the file is a .doc and there already is an equally named pdf file. This should do that trick:

<cfdirectory name="getLinks" directory="d:\inetpub\wwwroot\docs\documents\#passname#\">
<!--- Build list of files for easier searching --->
<cfset listOfFiles = valueList(getLinks.name)>
<cfoutput query="getLinks">
  <!--- Create temp variable with extension of file changed to PDF --->
  <cfset PDFFile = listfirst(getLinks.Name, '.') & ".pdf">
  <!--- If extension is not equal to .doc or if it is and there's no equally named PDF file, display the link --->
  <cfif not (right(name, 4) eq ".doc" and listFindNoCase(listOfFiles, PDFFile))>
    <a href="../documents/#passname#/#getLinks.Name#" target="_blank"">#listfirst(getLinks.Name,'.')#</a><br>
  </cfif>
</cfoutput>
0
 
dmwelterAuthor Commented:
That's exactly what I wanted. Thanks mosphat!
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now