Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"Cannot redirect" error from xp_cmdshell

Status
Not open for further replies.

RichardHayes

Programmer
May 1, 2002
28
0
0
GB
I am trying to run a simple DOS command using xp_cmdshell, which works every time when typed directly into a command prompt.

The command is designed to convert an RTF file to text and is along the lines of:

rtfconv -crtf -ciso-1 notes.rtf txt

I get the error "Cannot redirect" and after extensive searching I cannot work out what the problem is.

Any help would be greatly appreciated.
 
Did you try putting this code in a batch file and executing the batch through command shell?
 
When you try this on a command prompt, is that a command prompt on the server ? Its easy to forget that xp_cmdshell executes on the server. And then consider what username it executes as - mostly this is the username that runs SQLAgent, see BOL.
 
I have tried creating a batch file and I get the same error.

I have also tried executing the command directly on the server, and it works fine there.

How would I check what username it is using? How would I change it?

Thanks for your comments.

Richard.
 
Here are a few things to look into:

Check to make sure that the NT account that SQL Server runs xp_cmdshell in has permission to execute the command. That includes the permission to write to the directory where the output file will reside and read from the directory where the rtf source file exists.

On SQL 2000, if the SQL login that is executing xp_cmdshell is not part of the sysadmin group, then SQL Server uses the NT account defined by the xp_sqlagent_proxy_account procedure. You can see what NT account that is by executing
Code:
  EXEC master.dbo.xp_sqlagent_proxy_account N'GET'

There are a lot more convoluted permission restrictions associated with xp_cmdshell. You can see all the details in the Remarks section of this page:






“I have always found that plans are useless, but planning is indispensable.” --Dwight Eisenhower
 
It turns out that all I needed to do was add "> NUL:" to the end of the command being executed. It only works with a slight variation of the command line, but the final working piece of code is as follows.

DECLARE @cmd VARCHAR(1000)
SET @cmd = '\\PathOfComFile\rtfconv.com -crtf -ciso-1 \\path\notes.rtf > \\path\notes.txt < NUL:'
EXEC master..xp_cmdshell @cmd, NO_OUTPUT

Thank you all for your help.

Richard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top