Skip to main content
Inspiring
Jan 10, 2017 7:32 PM UTC
Answered

CFDIRECTORY - Count files in each directory

I want to count the number of files in each and every directory in my website. I don't want to list the files but just get a count of all files in each directory.

Not a sub directory count but a file count in each directory

For example using my C:\Windows\ directory as the directory where the files in each and every directory are to be counted

Like this

Directory NameFile CountDate Last File Modified
C:\Windows\ADFS\ar112-12-2016
C:\Windows\ADFS\bg212-12-2016
...................
C:\Windows\WinSxS8112-14-2016
C:\Windows\WinSxS\x86_microsoft-windows-deviceaccess.resources_31bf3856ad364e35_6.3.9600.16384_en-us_21effc1389c6f3e1112-14-2016

I need the count of the files in each directory. Every row in the table would be the next directory as you drill down.

This topic is closed to new replies. Start a new post to keep the conversation going.
WolfShade
WolfShadeCorrect answer
Legend
Jan 11, 2017 5:48 PM UTC

Okay.. finally got it..

<cfdirectory action="list" recurse="yes" directory="C:\wwwroot" name="folders" sort="directory ASC" />

<cfoutput query="folders" group="directory">

   #folders.directory#<cfset thisCount = 0 />

   <cfoutput><cfif lcase(trim(folders.type)) eq "file"><cfset thisCount++ />  - #folders.name#<br /></cfoutput>

   #thisCount# files...<br /><br />

</cfoutput>

This will list each directory, which will be followed by a list of each file in that directory, followed by the number of files in that directory.

HTH,

^_^

7 replies

WolfShade
Legend
Jan 11, 2017 1:55 PM UTC

I forgot to add the recurse="yes" attribute to the CFDIRECTORY tag, so it gets all child folders.

^_^

Translate
weezerboyAuthor
Inspiring
Jan 11, 2017 4:46 PM UTC

Thank you for your help.

I am getting this error?

Error Executing Database Query.<br><b>Query Of Queries syntax error.</b><br> Encountered ""SELECT. Incorrect Select List, The specific sequence of files included or processed is: E:\inetpub\wwwroot\cf\directory.cfm, line: 12

Is (SELECT COUNT(z.name) FROM folders z WHERE z.name = f.name and type='file') as filecount 
proper SQL syntax?

Translate
WolfShade
Legend
Jan 11, 2017 5:41 PM UTC

Not sure why you're getting that error.  I'm looking into a super simple way to do what you want, but it's not co-operating with me. 

BRB,

^_^

Translate
WolfShade
Legend
Jan 10, 2017 8:39 PM UTC

One way you could do it is with a query-of-a-query, I suppose.  When you use CFDIRECTORY to list files and folders, the object that is returned is a query-like object (or a query object, I can never remember.)  But you can use CFDIRECTORY to recursively get all files and folders under a specific directory, which would return everything, then use a QoQ to selectively get that information.

So, for example, if you were to recursively look in C:\wwwroot.  You would get an object that can be accessed like a query object.

<cfdirectory action="list" directory="C:\wwwroot" name="folders" />

<cfquery dbtype="query" name="QueryOfDirectories">

SELECT DISTINCT f.name, (SELECT COUNT(z.name) FROM folders z WHERE z.name = f.name and type='file') as filecount

FROM folders f

WHERE f.type='dir'

GROUP BY f.name, filecount

ORDER BY f.name

</cfquery>

This isn't tested, I don't have that much free time, but this should get you in the right direction.

HTH,

^_^

Translate