access denied message while executing xp_cmdshell

  • Hi,

    I am trying to execute below simple copy statement using xp_cmdshell

    EXECUTE xp_cmdshell 'copy C:\test\*.txt C:\"Program Files\"'

    it gives an error as 'Access is denied'

    even I get same error while creating sub directory or creating text files in program files directory.

    but if same command is run by providing any other directory name like-

    EXECUTE xp_cmdshell 'copy C:\test\*.txt C:\"New folder\"' it executes.

    Can any one explain why is it so, and how it can be achieved.

  • You need to run as administrator when accessing the program files folder. When starting the app (andhave permissions) you can right click and select 'run as asminstrator'.

  • sandy-833685 (4/10/2013)


    Hi,

    I am trying to execute below simple copy statement using xp_cmdshell

    EXECUTE xp_cmdshell 'copy C:\test\*.txt C:\"Program Files\"'

    it gives an error as 'Access is denied'

    even I get same error while creating sub directory or creating text files in program files directory.

    but if same command is run by providing any other directory name like-

    EXECUTE xp_cmdshell 'copy C:\test\*.txt C:\"New folder\"' it executes.

    Can any one explain why is it so, and how it can be achieved.

    First, you must enclose the entire path in the double quotes so that it inbludes the drive letter.

    Second, why on this good green Earth are you trying to write text files to the Program Files directory?

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

  • Thanks sharky,

    Hi Jeff, I am creating an application which creates the database using script and application gets installed in program files, which contains application install log file, I want to create log file for database objects which will log success or failure for creation database objects.

  • Most company policies have a strict guideline about keeping xp_cmshell turned off. Not allowing access to that from anything. Also, most companies never use the Program files group for storing the binaries of SQL Sever, and it is never installed on the same drive as the OS. I know that I never have.

    Andrew SQLDBA

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

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