473,796 Members | 2,544 Online
Bytes | Software Development & Data Engineering Community
+ 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_2007062816 2831.zip
quit
This is the batch file that is executed

@echo off
SET STATUS_FILE=D:\ FTP\folder1\ftp StatusFile.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\syst em32\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=%FA ILURE_STATUS%
IF %ERRORLEVEL%==0 SET RETURN_CODE=%SU CCESS_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_2007062816 2831.zip
200 PORT command successful.
150 Opening BINARY mode data connection for file_2007062816 2831.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_2007062816 2831.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 7432
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_2007062816 2831.zip
quit
This is the batch file that is executed

@echo off
SET STATUS_FILE=D:\ FTP\folder1\ftp StatusFile.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\syst em32\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=%FA ILURE_STATUS%
IF %ERRORLEVEL%==0 SET RETURN_CODE=%SU CCESS_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_2007062816 2831.zip
200 PORT command successful.
150 Opening BINARY mode data connection for file_2007062816 2831.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_2007062816 2831.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
7673
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
8089
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) from the executable. xp_cmdshell always returns 0 or 1. How can the executable return code say 99 be obtained in SQL Server?
4
8096
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 understand that when using xp_cmdshell, the sql thread in question waits until xp_cmdshell finishes what it's doing. Does this mean if my xp_cmdshell call takes 30 seconds, that nobody else can insert to this table until my xp_cmdshell and rest of the...
5
8624
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 or WE, it records it's progress into a log TXT file. But when executed via xp_cmdshell, it doesn't write into its log file. Is it possible to enable it to write into a log file with xp_cmdshell? Should I be using a different extended sp to...
4
7433
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
2537
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 works fine when I'm signed on with an account that has server admin rights, however it fails when I signin with an account that does not have server admin rights. I'm hoping someone can help me with this.
1
5433
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 dbo) create a stored procedure called sp_send_err: CREATE PROCEDURE sp_send_err @CompID varchar(20) AS declare @strCMD varchar(255) select @strCMD = "master.dbo.xp_cmdshell 'net send " + @CompID + " ""ERROR!""', no_output" execute (@strCMD)
1
1874
by: Antti Granqvist | last post by:
Hello! I have following object relations: Competition 1--* Category 1--* Course 1 | | * Course
9
2417
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. Communications on the Windows PCs use a class derived from CAsyncSocket(). I have written client and server versions of my software for use on the appropriate PCs. I am also using communicating with a legacy DOS based system, with the software...
0
9680
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9528
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10456
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10230
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9052
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7548
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6788
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5575
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4118
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 we have to send another system

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.