copy using xp_cmdshell

  • Can anyone tell why command 1 is working while command 2 is not working?

    1. exec xp_cmdshell 'copy C:\Test.xml c:\test.xls'

    2. exec xp_cmdshell 'copy C:\[Documents and Settings]\Lucky\[My Documents]\CKM\Test.xml c:\test.xls'

    -Lk

  • exec xp_cmdshell 'copy "C:\Documents and Settings\Lucky\My Documents\CKM\Test.xml" "c:\test.xls"'

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

  • depending in which OS this is running on as well, you will need to use the file path in this format:

    exec xp_cmdshell 'copy C:\Docume~1\Lucky\MyDocu~1\CKM\Test.xml c:\test.xls'

    that might do the trick 🙂

  • This was removed by the editor as SPAM

  • I find that running batch files through xp_cmdshell is a much better way to perform dos operations, also you might want to look into forfiles.

    Remember
    Without Change something sleeps inside of us that seldom awakens, the sleeper must awaken!!

  • Thanks one and all.

    lk

  • Try this query...,before set configures of xp_cmdshell

    declare @aaa varchar(40)

    set @aaa=(select @@servername)

    declare @bbb nvarchar(500)

    set @bbb='select name from sys.databases'

    exec(@bbb)

    declare @BCP nVarchar(500)

    set @BCP ='bcp "'+@bbb+'" queryout "D:\output.txt" -c -t, -T -S '+@aaa+''

    EXEC master..xp_cmdshell @BCP

  • Mr J (1/20/2010)


    I find that running batch files through xp_cmdshell is a much better way to perform dos operations, also you might want to look into forfiles.

    Kindred spirit here on both of those. FORFILES was a fairly "recent" and welcome addition to the command line.

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

  • Jeff Moden (6/16/2016)


    Mr J (1/20/2010)


    I find that running batch files through xp_cmdshell is a much better way to perform dos operations, also you might want to look into forfiles.

    Kindred spirit here on both of those. FORFILES was a fairly "recent" and welcome addition to the command line.

    That was recent. I've just always used "for /f" command to loop through files.

Viewing 9 posts - 1 through 8 (of 8 total)

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