xp_cmdshell

  • Hi Folks

    I have a table of file names that I want to loop through, and copy the physical file to another location using xp_cmdshell.

    Code in my stored procedure:

    set @Cmd = N'copy c:\SQLOldFiles\"' + @FileName+ '" c:\SQLNewFiles\, NO_OUTPUT'

    EXEC MASTER.DBO.xp_cmdshell @Cmd

    This results in The syntax of the command is incorrect. when executing the query

    When I copy and paste the @Cmd string copy c:\SQLOldFiles\"TestRemainderFile.txt" c:\SQLNewFiles\to command line, it works fine.

    What am I doing wrong?

    Thanks in advance

  • i think the entire path+file must be in dbl quotes, not just the filename;

    copy "c:\SQLOldFiles\TestRemainderFile.txt" c:\SQLNewFiles

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Hi,

    Thanks for the reply.

    The filename has embedded spaces in some cases, hence the double quotes around the filename only

  • Try this

    set @Cmd = N'copy "c:\SQLOldFiles\' + @FileName+ '" "c:\SQLNewFiles\"'

    Muthukkumaran Kaliyamoorthy
    https://www.sqlserverblogforum.com/

  • Many many thanks!

    I was starting to get into dynamic sql and char(39)'s...

    Havn't tested yet, but will this also work with apostrophes in the file name, E.g. "I'll be watching.mp3"

Viewing 5 posts - 1 through 4 (of 4 total)

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