how to direct the output of master.dbo.xp_cmdshell to a table

Posted on 2007-10-21
Last Modified: 2012-06-21

is this possible? eg to get a dir listing
master.dbo.xp_cmdshell 'DIR /B "D:\mypath\"'
..and then send this list to a table.

one PAQ suggests it is (

Question by:xenium
    LVL 142

    Expert Comment

    by:Guy Hengel [angelIII / a3]
    please check out this:

    insert into #temp_table
    exec master.dbo.xp_cmdshell 'cmd / c DIR /B "D:\mypath\"'


    Author Comment

    Ooops, the link given in my question is now working!

    AngelIII, btw the 'cmd/ c' seems to cause a problem (the DIR does not exec, only the dos prompt), but the original solution now seems to work:

    insert into #temp_table
    exec master.dbo.xp_cmdshell 'DIR /B "D:\mypath\"'
    LVL 142

    Accepted Solution

    looks like I posted with a space between the / and the C ...

    insert into #temp_table
    exec master.dbo.xp_cmdshell 'cmd /c DIR /B "D:\mypath\"'

    Author Comment

    Great, thanks, this works too.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
    This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
    Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
    Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now