Many years ago I wrote article SQL SERVER – Get a List of Fixed Hard Drive and Free Space on Server where I demonstrated using undocumented Stored Procedure to find the drive letter in local system and available free space. I received question in email from reader asking if there any way he can list directory structure within the T-SQL. When I inquired more he suggested that he needs this because he wanted set up backup of the data in certain structure.
Well, there is one undocumented stored procedure exists which can do the same. However, please be vary to use any undocumented procedures.
Execution of the above stored procedure will give following result. If you prefer you can insert the data in the temptable and use the same for further use.
Here is the quick script which will insert the data into the temptable and retrieve from the same.
CREATE TABLE #TempTable (Subdirectory VARCHAR(512), Depth INT);
INSERT INTO #TempTable (Subdirectory, Depth)
EXEC xp_dirtree 'C:\Windows'
SELECT Subdirectory, Depth
DROP TABLE #TempTable;
Reference: Pinal Dave (http://blog.SQLAuthority.com)