I do realize that this thread is 5 years old but I thought I'd post another non-xpCmdShell, non-CLR alternative. Details are in the following code and it's pretty simple stuff.
--===== Define the path and populate it.
-- This could be a parameter in a proc
DECLARE @pPath VARCHAR(512);
SELECT @pPath = 'C:\Temp';
--===== Create a table to store the directory information in
CREATE TABLE #DIR
(
RowNum INT IDENTITY(1,1),
ObjectName VARCHAR(512),
Depth TINYINT,
IsFile BIT,
Extension AS RIGHT(ObjectName,CHARINDEX('.',REVERSE(ObjectName))) PERSISTED
)
;
--===== Get the directory information and remember it
INSERT INTO #DIR
(ObjectName,Depth,IsFile)
EXEC xp_DirTree 'C:\Temp',1,1
;
--===== Now do whatever it is you need to do with it
SELECT * FROM #DIR;