Solved

Using CFDIRECTORY, CFLOOP, CFQUERY causes the NAME variable to become static inside a CFQUERY.

Posted on 2003-10-23
7
1,920 Views
Last Modified: 2013-12-24
00  <cfdirectory action="list" directory="\\teletran\stockphotography" name="StockDirectories">
01  <cfloop query="StockDirectories">
02   <cfif StockDirectories.Type is "DIR">
03   <cfoutput>#Name#</cfoutput><br>
04    <cfdirectory action="list" filter="*.jpg" directory="\\teletran\stockphotography\#StockDirectories.Name#\HighResolutionVersions" name="CurrentSelectedDirectory">
05  
06    <cfoutput query="CurrentSelectedDirectory">
07  
08    <cfquery name="LookForFile" datasource="#dn#" username="#un#" password="#pw#">
09    select *
10    from Stock
11    where ImageFileName='#Name#' and SourceFolder='#Name#'
12    </cfquery>
13              
14    <cfif LookForFile.RecordCount is 0>
15                    
16     <cfquery name="InsertTheFile" datasource="#dn#" username="#un#" password="#pw#">
17     insert into Stock (ImageFileName,SourceFolder,FileSize)
18     values ('#Name#','#StockDirectories.Name#','#Size#')
19     </cfquery>
20                          
21     <cfset FilesInserted=FilesInserted+1>
22     <cfelse>
23      <cfset FilesIgnored=FilesIgnored+1>
24     </cfif>
25              
26     </cfoutput>
27    </cfif>
28   </cfloop>
0
Comment
Question by:tomkinite
7 Comments
 
LVL 1

Author Comment

by:tomkinite
ID: 9608149
Line numbering would be an awesome feature for EE! But, that's not my problem right now.

Here's the application.

On line 00 I read the contents of the directory (stockphotography). In this directory contains hundreds of sub directories, each contain hundreds of JPEGS.  Through each loop of the StockDirectories query (CFDIRECTORY) I then do another loop to list JPEGS (CFDIRECTORY).  

The loop checks to see if the file exists in the database (line 08 to 12) and if it doesn't, it inserts it into the database (line 16 to 19).  

Here's the problem.

On line 03 you can see that I output the #Name# variable from StockDirectories perfectly fine.  The output is exactly what is expected.  On line 18 I use this very same variable but it now mysteriously static to the first folder's name in the CFDIRECTORY query.  Why is it looping as normal anywhere else (I have put the output of #NAME# inside of the loop as well) but when I use CFQUERY to insert the value of #NAME# it thinks it is the first folder?
0
 
LVL 9

Expert Comment

by:shooksm
ID: 9608242
What happens when you scope the Name variable with the query name?  IE:

06    <cfoutput query="CurrentSelectedDirectory">
07  
08    <cfquery name="LookForFile" datasource="#dn#" username="#un#" password="#pw#">
09    select *
10    from Stock
11    where ImageFileName='#CurrentSelectedDirectory.Name#' and SourceFolder='#StockDirectories.Name#'
12    </cfquery>
13            
14    <cfif LookForFile.RecordCount is 0>
15                
16     <cfquery name="InsertTheFile" datasource="#dn#" username="#un#" password="#pw#">
17     insert into Stock (ImageFileName,SourceFolder,FileSize)
18     values ('#CurrentSelectedDirectory.Name#','#StockDirectories.Name#','#Size#')
19     </cfquery>
20                      
21     <cfset FilesInserted=FilesInserted+1>
22     <cfelse>
23      <cfset FilesIgnored=FilesIgnored+1>
24     </cfif>
25            
26     </cfoutput>
0
 
LVL 9

Expert Comment

by:shooksm
ID: 9608256
Oops, forgot to post which lines I modified. 11 and 18
0
New My Cloud Pro Series - organize everything!

With space to keep virtually everything, the My Cloud Pro Series offers your team the network storage to edit, save and share production files from anywhere with an internet connection. Compatible with both Mac and PC, you're able to protect your content regardless of OS.

 
LVL 25

Expert Comment

by:James Rodgers
ID: 9608285
save the following as a cfm called

dirInsertRecursive.cfm

<cfif isDefined('attributes.dirPath')>
      <cftry>
            <cfset path=#expandPath(#attributes.dirPath#)#>
      <cfcatch>
            <cfset path=#attributes.dirPath#>
      </cfcatch>
      </cftry>
      <cfif findNoCase(rootPath,#getDirectoryFromPath(path)#,1)>
      <cfif right(path,1) eq '\'>
            <cfset path=left('#attributes.dirPath#',len('#attributes.dirPath#')-1)>
      <cfelse>
            <cfset path=#attributes.dirPath#>
      </cfif>
<cfelse>
      <cfset path=rootPath>
</cfif>
</cfif>
<cfdirectory action="list" directory="#path#" name="fileList" sort="type,name">
<cfloop query="fileList">
<cfif #type# is "dir" and #name# neq "." and #name# neq "..">
<CF_dirInsertRecursive dirPath= "#attributes.dirPath#\#name#">
<cfdirectory action="delete"  directory="#attributes.dirPath#\#name#">
<cfelseif #type# eq "file">
#name#<br>
<!---
      <cfquery name="LookForFile" datasource="#dn#" username="#un#" password="#pw#">
    select *
    from Stock
    where ImageFileName='#Name#' and SourceFolder='#Name#'
   </cfquery>
           
    <cfif LookForFile.RecordCount is 0>
                 
    <cfquery name="InsertTheFile" datasource="#dn#" username="#un#" password="#pw#">
     insert into Stock (ImageFileName,SourceFolder,FileSize)
     values ('#Name#','#StockDirectories.Name#','#Size#')
     </cfquery>
                     
     <cfset FilesInserted=FilesInserted+1>
     <cfelse>
      <cfset FilesIgnored=FilesIgnored+1>
     </cfif>
       --->    
     </cfoutput>

</cfif>

</cfloop>

and call it from another cfm as

<cf_dirInsertRecursive.cfm dirPath="\\teletran\stockphotography">

test it in a small directory structure first, this will just list the contents, to do the insert remove the comments and delete or comment out the  #name#<br> line

0
 
LVL 2

Accepted Solution

by:
jonnygo55 earned 500 total points
ID: 9608355
I have come across this before...
I cfoutput of a query inside a cfloop causes the cfloop not to advance as expected it seems...

one thing that worked for me was after the cfloop and before the cfoutput to set the qurey variable into another temp variable:

<cfloop query="StockDirectories">
<cfset thisDir = StockDirectories.name>

and then to refrerence #thisDir# on line 18 within the cfoutput of the query...
0
 
LVL 17

Expert Comment

by:anandkp
ID: 9608862
i agree with jonnygo - i had done a similar workaround & it did the trick !

u cld try changing the code in ur line 6 from CFOUTPUT to CFLOOP & see if it helps
else what jonnygo suggested shld help u out for sure.

K'Rgds
Anand
0
 
LVL 10

Expert Comment

by:Mause
ID: 9609196
Also had that problem and solved it like jonnygo55 did

Anand cfloop inside a cfloop will still give the same problem....
well in CF5, not test it in MX yet.

Mause
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Forbidden errors 5 125
Unsearchable in Google,Yahoo and Bing. 6 58
System Analysis 5 52
exchange 2010 turning off 3des ciphers 2 127
Article by: kevp75
Hey folks, 'bout time for me to come around with a little tip. Thanks to IIS 7.5 Extensions and Microsoft (well... really Windows 8, and IIS 8 I guess...), we can now prime our Application Pools, when IIS starts. Now, though it would be nice t…
Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now