I am using SQL Server 2005, I have a single table called Docs with a single column called Path.
Path column contains file paths:
"C:\MyDocs\1.txt"
"C:\MyDocs\Folder1\3.txt"
I need my query to retrieve in a single query how many children each folder and subfolder has.
In my example above I need the query to retrieve:
3 children (1 folder and 2 files) for “MyDocs”
1 child (1 file) for “Folder1”.
Any ideas?
Thank you.
You can solve this using string manipulation and, for clarity, two CTEs:
You might have to tweak this a little bit depending on your data. If your table lists only files, this query should be fine. If it lists also folders with a trailing backslash, subtract 1 from the count.
How to get a listing of all folders and files contained in them
Not tested!