Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1962
  • Last Modified:

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

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
tomkinite
Asked:
tomkinite
1 Solution
 
tomkiniteAuthor Commented:
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
 
shooksmCommented:
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
 
shooksmCommented:
Oops, forgot to post which lines I modified. 11 and 18
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
James RodgersWeb Applications DeveloperCommented:
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
 
jonnygo55Commented:
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
 
anandkpCommented:
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
 
MauseCommented:
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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