473,471 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Problems getting return codes from xp_cmdshell

1 New Member
Hi,

Thi is my first post on this forum, and I'm hoping that there is a guru out there who can help me with an annoying problem I am having.

I have written a Stored Procedure that relies heavily on dynamic SQL to export a series of table/views to CSV format, then zip the files up, then it creates an series of ftp commands to put the newly created zip file onto the relevent ftp server. The list of files and destination server are held in a control table, and are different depending on the parameters passed.

There is a batch file that is called using the xp_cmdshell that executes the list of commands to put the file. The batch file is supposed to redirect the ftp output to a text file so that I can parse it, looking for the command successful following the put command.

This work fine when I start a cmd prompt manually, I can see the whole session log in the text file. However, when I execute the same batch file from xp_cmdshell, all that is in the session log, is the commands sent to the server, not the responses.

This is the ftp command list used held in a text file called Macro.ftp
open x.x.x.x
username
password
cd folder1
bin
put D:\FTP\folder1\file_20070628162831.zip
quit
This is the batch file that is executed

@echo off
SET STATUS_FILE=D:\FTP\folder1\ftpStatusFile.txt
SET SUCCESS_STATUS=200
SET FAILURE_STATUS=0
:Clear the existing Status file ***
IF EXIST %STATUS_FILE% DEL %STATUS_FILE%
:Try to ftp the file and log the success of the transfer ***
c:\windows\system32\ftp.exe -s:D:\FTP\folder\Macro.ftp > %STATUS_FILE%
:Check the success of the transfer ***
FINDSTR /C:"200 PORT command successful." %STATUS_FILE%
IF %ERRORLEVEL%==1 SET RETURN_CODE=%FAILURE_STATUS%
IF %ERRORLEVEL%==0 SET RETURN_CODE=%SUCCESS_STATUS%
:Exit with the correct exit code.
echo %RETURN_CODE%
EXIT %RETURN_CODE%

This is the output if I run the code directly from a cmd window.

ftp> Connected to x.x.x.x.

open x.x.x.x
220-Microsoft FTP Service
220 FTP Service
User (x.x.x.x:(none)):
331 Password required for username.

230-Welcome
230 User username logged in.
ftp> ftp>
cd folder1
250 CWD command successful.
ftp> bin
200 Type set to I.
ftp> put D:\FTP\folder1\file_20070628162831.zip
200 PORT command successful.
150 Opening BINARY mode data connection for file_20070628162831.zip.
226 Transfer complete.
ftp: 11692 bytes sent in 0.19Seconds 62.52Kbytes/sec.

ftp> quit
221

And this is the output when I call it from xp_cmdshell
User (x.x.x.x:(none)): open x.x.x.x



cd folder
bin
put D:\FTP\folder1\file_20070628162831.zip
quit
As you can see, I was expecting to be able to parse the log file and find the line 226 Transfer complete. to denote a successful file transfer, then continue the SP sending an email notification of success or failure. But as I cannot get the session log to work, I always report failure.

Does anyone have any idea how I can get round this issue?

Regards
Lee
Jun 29 '07 #1
1 7420
Motoma
3,237 Recognized Expert Specialist
Hi,

Thi is my first post on this forum, and I'm hoping that there is a guru out there who can help me with an annoying problem I am having.

I have written a Stored Procedure that relies heavily on dynamic SQL to export a series of table/views to CSV format, then zip the files up, then it creates an series of ftp commands to put the newly created zip file onto the relevent ftp server. The list of files and destination server are held in a control table, and are different depending on the parameters passed.

There is a batch file that is called using the xp_cmdshell that executes the list of commands to put the file. The batch file is supposed to redirect the ftp output to a text file so that I can parse it, looking for the command successful following the put command.

This work fine when I start a cmd prompt manually, I can see the whole session log in the text file. However, when I execute the same batch file from xp_cmdshell, all that is in the session log, is the commands sent to the server, not the responses.

This is the ftp command list used held in a text file called Macro.ftp
open x.x.x.x
username
password
cd folder1
bin
put D:\FTP\folder1\file_20070628162831.zip
quit
This is the batch file that is executed

@echo off
SET STATUS_FILE=D:\FTP\folder1\ftpStatusFile.txt
SET SUCCESS_STATUS=200
SET FAILURE_STATUS=0
:Clear the existing Status file ***
IF EXIST %STATUS_FILE% DEL %STATUS_FILE%
:Try to ftp the file and log the success of the transfer ***
c:\windows\system32\ftp.exe -s:D:\FTP\folder\Macro.ftp > %STATUS_FILE%
:Check the success of the transfer ***
FINDSTR /C:"200 PORT command successful." %STATUS_FILE%
IF %ERRORLEVEL%==1 SET RETURN_CODE=%FAILURE_STATUS%
IF %ERRORLEVEL%==0 SET RETURN_CODE=%SUCCESS_STATUS%
:Exit with the correct exit code.
echo %RETURN_CODE%
EXIT %RETURN_CODE%

This is the output if I run the code directly from a cmd window.

ftp> Connected to x.x.x.x.

open x.x.x.x
220-Microsoft FTP Service
220 FTP Service
User (x.x.x.x:(none)):
331 Password required for username.

230-Welcome
230 User username logged in.
ftp> ftp>
cd folder1
250 CWD command successful.
ftp> bin
200 Type set to I.
ftp> put D:\FTP\folder1\file_20070628162831.zip
200 PORT command successful.
150 Opening BINARY mode data connection for file_20070628162831.zip.
226 Transfer complete.
ftp: 11692 bytes sent in 0.19Seconds 62.52Kbytes/sec.

ftp> quit
221

And this is the output when I call it from xp_cmdshell
User (x.x.x.x:(none)): open x.x.x.x



cd folder
bin
put D:\FTP\folder1\file_20070628162831.zip
quit
As you can see, I was expecting to be able to parse the log file and find the line 226 Transfer complete. to denote a successful file transfer, then continue the SP sending an email notification of success or failure. But as I cannot get the session log to work, I always report failure.

Does anyone have any idea how I can get round this issue?

Regards
Lee
I think you may only receive error messages. Try executing a couple of things that will produce errors, and you will probably receive those with xp_cmdshell.
Jun 29 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Yvonne | last post by:
Would like to implement Blat on SQL Server 2000. I'm looking for example syntax for setting up BLAT with xp_cmdshell. TIA
1
by: Chandra Mohan | last post by:
Hi All, I have a requirement of calling a Executable from SQL Server. I know we can use - EXEC @result = Master..xp_cmdshell @<command_string> However I want to get the return value (int)...
4
by: Joel Thornton | last post by:
Whenever something is inserted to a given table, I want to run some shell commands using xp_cmdshell. Would it be a bad idea to put this xp_cmdshell in the INSERT trigger of this table? I...
5
by: Narine | last post by:
Hi Folks, I am using an xp_cmdshell sp to start a C# app. This app downloads data files and imports them into DB. It has a logging functionality built-in and when executed from the command line...
4
by: Lauren Quantrell | last post by:
I have created the following trigger: CREATE TRIGGER ON OutputTable FOR INSERT AS Declare @filename nvarchar(35) Declare @filecontents nvarchar(2000) Declare @strcmdshell varchar(150)
2
by: Lauren Quantrell | last post by:
I have created a view named viewOutput that shows one column in a table. I insert a row into the table and then I'm using this code to create a file with the text in the single row. This code...
1
by: Micah Gentry | last post by:
Is there any way to allow a user to use the xp_cmdshell extended stored procedure without giving that user execute permissions to xp_cmdshell in SQL server 6.5? Let me clarify. Lets say I (as the...
1
by: Antti Granqvist | last post by:
Hello! I have following object relations: Competition 1--* Category 1--* Course 1 | | * Course
9
by: =?Utf-8?B?SG93YXJkIFNtaXRo?= | last post by:
I am using VC++ 6.0 (with SP5 installed). When using WinXP this is with SP2 installed. I am developing an instrumentation system comprising a set of networked PCs connected using TCP/IP TCP links....
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.