Some Undocumented Stored Procedures

  • Comments posted to this topic are about the content posted at

  • Heh... judging from the number of forums you have this same article posted on, I'd say you have some work cut out for you in making a correction. 😉

    xp_DirTree actually takes 3 operands:

    1. The path to a folder. This may be a UNC or drive path.

    2. The number of levels below the path in the first operand. If missing, the default is "0" which equates to "all levels" which, as you know, can be terribly long. Levels above "0" operate as expected where Level 1 is the current directory only.

    3. There's a third operand that most are simply not aware of. The default for this operand is "0" which means "only list directories". If this operand is set to any non-zero value, the output will return a 3rd column named "File". It should have been called "IsFile" because a "1" in this column means the "Subdirectory" column actually contains a file name instead of a folder name.

    So, try running this and see what you get...

    [font="Arial Black"]EXEC Master.dbo.xp_DirTree 'C:\',1,1[/font]

    Think of it as an xp_FileExists on steroids. You can also capture the output for processing using something like the following:

    [font="Courier New"]CREATE TABLE #MyDir



    FolderFileName VARCHAR(128),

    Depth TINYINT,

    IsFile TINYINT



    (FolderFileName, Depth, IsFile)

    EXEC Master.dbo.xp_DirTree 'C:\',1,1

    SELECT * FROM #MyDir[/font]

    Of course, the first operand may be contained in a variable without any dynamic SQL being necessary.

    --Jeff Moden

    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply