SQL SERVER – Get Directory Structure using Extended Stored Procedure xp_dirtree
        Posted  
        
            by pinaldave
        on SQL Authority
        
        See other posts from SQL Authority
        
            or by pinaldave
        
        
        
        Published on Sat, 26 Nov 2011 01:30:29 +0000
        Indexed on 
            2011/11/26
            1:59 UTC
        
        
        Read the original article
        Hit count: 571
        
PostADay
|sql
|SQL Authority
|SQL Query
|SQL Server
|SQL Stored Procedure
|SQL Tips and Tricks
|SQLServer
|T SQL
|Technology
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.
xp_dirtree 'C:\Windows'
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
FROM #TempTable;
DROP TABLE #TempTable;
Reference: Pinal Dave (http://blog.SQLAuthority.com)
Filed under: Pinal Dave, PostADay, SQL, SQL Authority, SQL Query, SQL Server, SQL Stored Procedure, SQL Tips and Tricks, SQLServer, T SQL, Technology
© SQL Authority or respective owner