Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7839037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:18:04+00:00 2026-06-02T15:18:04+00:00

There is a SQL Server undocumented extended stored procedure called xp_dirtree , which can

  • 0

There is a SQL Server undocumented extended stored procedure called xp_dirtree, which can return all files and folders name (include subfolders) in a table format. To practice my understanding of recursive CTE, I decide to use it to get the full path of all files in a specified folder(include subfolders). However, after an hour of head scratch I still can’t figure out the correct way to do it. The following code is what I have currently. Can this purpose be implemented with recursive CTE?

DECLARE @dir NVARCHAR(260) ;
SELECT  @dir = N'c:\temp' ;

IF RIGHT(@dir, 1) <> '\' 
    SELECT  @dir = @dir + '\' ;

IF OBJECT_ID('tempdb..#dirtree', 'U') IS NOT NULL 
    DROP TABLE #dirtree ;
CREATE TABLE #dirtree
(
 id INT PRIMARY KEY
        IDENTITY,
 subdirectory NVARCHAR(260),
 depth INT,
 is_file BIT
) ;

INSERT  INTO #dirtree
        EXEC xp_dirtree 
            @dir,
            0,
            1 ;

SELECT  *
FROM    #dirtree ;

WITH    files
          AS (
              SELECT    id,
                        subdirectory,
                        depth,
                        is_file, subdirectory AS path
              FROM      #dirtree
              WHERE     is_file = 1
                        AND depth <> 1
   UNION ALL
               -- ...
             )
    SELECT  *
    FROM    files ;

Suppose the xp_dirtree output is:

/*
id  subdirectory   depth   is_file
--- -------------- ------- -------
1   abc.mdf        1       1
2   a              1       0
3   a.txt          2       1
4   b.txt          2       1
5   a.rb           1       1
6   aaa.flv        1       1
*/

What I want is:

/*
path
------------------
c:\temp\abc.mdf
c:\temp\a\a.txt
c:\temp\a\b.txt
c:\temp\a.rb
c:\temp\aaa.flv
*/
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T15:18:06+00:00Added an answer on June 2, 2026 at 3:18 pm

    If I understand you correct you want something like this:

    Test data:

    CREATE TABLE #dirtree
    (
        id INT,
        subdirectory NVARCHAR(260),
        depth INT ,
        is_file BIT,
        parentId INT
    )
    
    INSERT INTO #dirtree(id,subdirectory,depth,is_file)
    VALUES
        (1,'abc.mdf',1,1),(2,'a',1,0),(3,'a.txt',2,1),
        (4,'b.txt',2,1),(5,'a.rb',1,1),(6,'aaa.flv',1,1)
    

    Updated the parent id

    UPDATE #dirtree
    SET ParentId = (SELECT MAX(Id) FROM #dirtree
          WHERE Depth = T1.Depth - 1 AND Id < T1.Id)
    FROM #dirtree T1
    

    Query

    ;WITH CTE
    AS
    (
        SELECT
            t.id,
            t.subdirectory,
            t.depth,
            t.is_file
        FROM
            #dirtree AS t
        WHERE
            is_file=0
        UNION ALL
        SELECT
            t.id,
            CAST(CTE.subdirectory+'\'+t.subdirectory AS NVARCHAR(260)),
            t.depth,
            t.is_file
        FROM
            #dirtree AS t
            JOIN CTE
                ON CTE.id=t.parentId
        )
    SELECT
        'c:\temp\'+CTE.subdirectory AS [path]
    FROM
        CTE
    WHERE
        CTE.is_file=1
    UNION ALL
    SELECT
        'c:\temp\'+t.subdirectory
    FROM
        #dirtree AS t
    WHERE
        is_file=1
        AND NOT EXISTS
        (
            SELECT
                NULL
            FROM
                CTE
            WHERE
                CTE.id=t.id
        )
    

    Result

    path
    ---------------
    c:\temp\a\a.txt
    c:\temp\a\b.txt
    c:\temp\abc.mdf
    c:\temp\a.rb
    c:\temp\aaa.flv
    

    EDIT

    Changed the tables used in the example to more look like the ones in your question

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In SQL Server 2005, there is a feature called row_number() which makes pagination very
Hey all, is there any SQL Server 2005 guru that can tell me a
In sql-server there is function to auto-increment guid fields. CREATEGUID() how can I do
In SQL Server there is the ability to INSERT all of the data from
I heard in Microsoft SQL Server there are multiple ways to find worst stored
SQL Server 2005. Is there a sql query that will return a text field
Is there MS SQL Server function that counts the number of times a particular
In SQL Server there is a way to join tables from multiple sql servers
Is there any API SQL Server 2008 offers for .net application to create and
In connection string to SQL Server there is max pool size option. My question

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.