Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CFDirectory and counting contents of sub dir's

Status
Not open for further replies.

NickyJay

Programmer
Sep 1, 2003
217
0
0
GB
Hi all,
fresh back for mat leave and have forgotten most of my programming skills :(
im tring to provide a list of the sub dir's in a root folder and count the number of files inside each - how would i do this - got me baffled!! so far i have a query to output my main data, then have a cfdirectory call to match where foldername in query matches foldername in cfdir... all this does is show the number in the first sub for each row in my query.. help!

Many thanks
Nicola
 
all this does is show the number in the first sub for each row in my query

That sounds like a simple issue with nested loops. Can you post your code? Also, do you need to count files in the top level of a subdirectory .. or all subdirectories?

----------------------------------
 
Hiya,

All i need is a count of the files in each subdir - there are about 40 or so in my root folder and each only contain files.
my code:
Code:
<cfquery name="qryLaptops" datasource="StaffBadges" >
	SELECT *
	FROM LaptopUsers
	ORDER BY LapNo asc
</cfquery> 

<cfdirectory 
  ACTION="LIST" 
  DIRECTORY="\\hq-laps\message2\#qryLaptops.LapNo#" 
  NAME="the_dir" />
	
  <cfset messages_waiting = the_dir.recordcount />
  <cfset myFile = CreateObject("java", "java.io.File") /> 
  <cfset myFile.init("/path/to/file") />
  <cfset last_modified = myFile.lastModified() />

<!---heres the output part--->
<table border="1">
	<th>Name</th>
	<th>View or Edit Details</th>
	<th>CORE Lap No.</th>
	<th>Asset Tag</th>
<cfoutput query="qryLaptops">
 <tr><input name="StaffNo" type="hidden" value="#StaffNo#"/>
   <td>#BadgeName#</td> 
   <td align="center"><a href="./staff.cfm?staffNo=#StaffNo#">view</a> | <a href="./edit_staff.cfm?staffNo=#StaffNo#">edit</a></td>
   <td align="center">#LapNo#</td>
   <td>#computer_name#</td>
   <td><cfif the_dir.recordCount>#DateFormat(the_dir.dateLastModified, "dd mmmm yyyy")#</cfif></td>
   <td align="center"><cfoutput>#messages_waiting#</cfoutput></td>
  </tr>
</cfoutput>
</table>

Basically what im trying to do is get the number of files in each folder that relates to the laptop number pulled out by my query... i can do one per page but really need to show a list of the all to view at a glance type of thing

Thanks
Nicola
 
<cfdirectory
ACTION="LIST"
DIRECTORY="\\hq-laps\message2\#qryLaptops.LapNo#"
NAME="the_dir" />

<cfset messages_waiting = the_dir.recordcount />
<cfset myFile = CreateObject("java", "java.io.File") />
<cfset myFile.init("/path/to/file") />
<cfset last_modified = myFile.lastModified() />

Since that code is not inside a loop, it only grabs the contents of the first folder in the qryLaptops query. (BTW, what version are you using? I ask because the results in CF8 include DateLastModified).

One option is to move the cfdirectory call inside the query loop. Another option is to do a recursive list on the parent directory \\hq-laps\message2\. Then run a QoQ inside your output loop. (That latter would not be a good option if the parent directory contained millions of files).

The first option is self explanatory. Here is an example of the second.


<!--- may be CF 8 Specific --->
<!--- get files in all subdirectories --->
<cfdirectory directory="#TheBaseDirectory#" action="list" recurse="true" name="getAllFolders">

<cfoutput query="qryLaptops">
<!--- Use QoQ to count files in current directory --->
<cfquery name="getCurrentFolder" dbtype="query">
SELECT COUNT(*) AS NumberOfFiles
FROM getAllFolders
WHERE Lower(Directory) = Lower('#TheBaseDirectory##LaptopNo#')
AND Type = 'File'
</cfquery>
Folder: #LaptopNo# Number of Files: #val(getCurrentFolder.NumberOfFiles)#<br>
</cfoutput>





----------------------------------
 
Hi,

we are running mx 7, but i will give you suggestions above a go and see if i have any luck :)

Many Thanks

Nicola
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top