Home Forums SQL Server 2005 Development Pulling Filename from Windows directory into table RE: Pulling Filename from Windows directory into table

  • Michael Valentine Jones (7/22/2009)


    This is a farily simple method:

    declare @files table (cmdout nvarchar(100) )

    insert into @files ( cmdout )

    -- Get filenames excluding directories

    exec master.dbo.xp_cmdshell 'dir C:\ /b /a:-d'

    select cmdout from @files where cmdout is not null order by cmdout

    Results:

    cmdout

    -------------------

    AUTOEXEC.BAT

    BOOT.BAK

    boot.ini

    cmldr

    CONFIG.SYS

    IO.SYS

    license.txt

    MSDOS.SYS

    ntdetect.com

    ntldr

    pagefile.sys

    Simpler still is EXEC Master.dbo.xp_Dirtree 'path/filename',1,1 but that isn't what the OP asked for either. He wants the dates for each file, as well.

    --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)