04-22-2005 9:18 AM
hai guys,
I have created one batch file in WINDOWS NT.
Now i want to call this file from my ABAP program.
How can i do this..is there any FM here..pls let me know.
ambichan.
04-22-2005 9:58 AM
Hi Ambichan,
Sorry, I assume that you want to run this batchfile on the presentation server?
If when you say you want to run your batch file on WINDOWS NT, you actually mean your application server, then there is another technique.
You need to set up your external program using transaction SM69.
You give it a name, specify the program in the OS command line, and specify parameters in the parameters line (use ? [mandatory] and & [optional] placeholders for dynamic parameters).
You can then test this command using transaction SM49.
To execute from your program, use the function SXPG_COMMAND_EXECUTE (you can specify a destination to have it execute the command on another application server).
There is a special authorisation check to execute external commands. If you are having problems check the EXCEPTION NO_PERMISSION or your SU53 results to make sure you are not failing on authorisation.
Cheers,
Brad
04-22-2005 9:23 AM
Hi Ambichan,
The function module WS_EXECUTE is your man.
Put the path of your batch file in the PROGRAM parameter.
Cheers,
Brad
04-22-2005 9:33 AM
Sorry, forgot, you can send commandline parameters using the COMMANDLINE parameter.
Therefore to open notepad with the file test.txt you do the following:
CALL FUNCTION 'WS_EXECUTE'
EXPORTING
COMMANDLINE = 'c:test.txt'
PROGRAM = 'notepad.exe' "If notepad is in your path
EXCEPTIONS
OTHERS = 1.
Although, of course, it would be useful to capture all the exceptions more thoroughly.
If you want to get more sophisticated you can also use the function module REGISTRY_GET beforehand to determine the path and name of the program you are calling based on registry entries. This could be useful if you wish to deploy your batch file in a corporate environment.
Cheers,
Brad
Message was edited by: Brad Williams
04-22-2005 9:58 AM
Hi Ambichan,
Sorry, I assume that you want to run this batchfile on the presentation server?
If when you say you want to run your batch file on WINDOWS NT, you actually mean your application server, then there is another technique.
You need to set up your external program using transaction SM69.
You give it a name, specify the program in the OS command line, and specify parameters in the parameters line (use ? [mandatory] and & [optional] placeholders for dynamic parameters).
You can then test this command using transaction SM49.
To execute from your program, use the function SXPG_COMMAND_EXECUTE (you can specify a destination to have it execute the command on another application server).
There is a special authorisation check to execute external commands. If you are having problems check the EXCEPTION NO_PERMISSION or your SU53 results to make sure you are not failing on authorisation.
Cheers,
Brad
04-22-2005 10:50 AM
hey
thanks for your reply.I am working in 40B.
yes ur right, I have to execute the batch file in WINDOWS NT ie.application server.
Apart from Creating OS command line and using SXPG_COMMAND_EXECUTE FM, how can i use path and file directly in this FM.
i did testing from se37 with this FM.but i did not get any possible output.
how to input parameter in FM .could you pls give me example.
ambichan.
04-22-2005 11:06 AM
Hi ambichan,
To execute a program on the application server, it really is best if you create an external command (using SM69).
Here is an example call of the program:
* call executable
CALL FUNCTION 'SXPG_COMMAND_EXECUTE'
EXPORTING
COMMANDNAME = command " Your external program
ADDITIONAL_PARAMETERS = param " Your parameters
OPERATINGSYSTEM = 'WINDOWS NT'
* TARGETSYSTEM = sy-host
* DESTINATION = destination
* STDOUT = 'X'
* STDERR = 'X'
* TERMINATIONWAIT = 'X'
* TRACE =
IMPORTING
STATUS = status
EXITCODE = exitcode
TABLES
EXEC_PROTOCOL = exec_protocol
EXCEPTIONS
NO_PERMISSION = x_no_permission
COMMAND_NOT_FOUND = x_command_not_found
PARAMETERS_TOO_LONG = x_parameters_too_long
SECURITY_RISK = x_security_risk
WRONG_CHECK_CALL_INTERFACE = x_wrong_check_call_interface
PROGRAM_START_ERROR = x_program_start_error
PROGRAM_TERMINATION_ERROR = x_program_termination_error
X_ERROR = x_x_error
PARAMETER_EXPECTED = x_parameter_expected
TOO_MANY_PARAMETERS = x_too_many_parameters
ILLEGAL_COMMAND = x_illegal_command
WRONG_ASYNCHRONOUS_PARAMETERS = x_wrong_async_parameters
CANT_ENQ_TBTCO_ENTRY = x_cant_enq_tbtco_entry
JOBCOUNT_GENERATION_ERROR = x_jobcount_generation_error
OTHERS = x_others.
if sy-subrc <> 0 or exitcode <> 0.
* error dialog only, if caller is a dialog user
if sy-batch is initial.
call function 'DB6_SHOW_SXPG_ERROR'
exporting
command = 'DB6CLP'
parameters = param
server = sy-host
return_code = sy-subrc
exitcode = exitcode
tables
exec_protocol = exec_protocol.
endif.
endif.
With this code, if the user is in dialog mode the output from the external command is displayed to them. Otherwise, you can do what you want with the EXEC_PROTOCOL table which contains the output from the external program.
Hope that helps.
Cheers,
Brad
04-22-2005 5:27 PM
Hi Ambi,
I am not sure if you followed all the steps Brad mentioned. Let us say your batch file is ABC.BAT in the directory /usr/sap/temp.
In SM69, you will enter ZABC(any name you think is appropriate), in the operating system field you will choose NT, Type customer, and then in the Operating System Command you will enter /usr/sap/temp/ABC.BAT.
Now let us say your batch file requires two parameters one a constant value and other a dynamic value. You can enter the constant value in the field "Parameters for operating system command" and you can pass the dynamic parameter through the function module.
All the return values after executing the FM will be there in the 'EXEC_PROTOCOL' internal table.
Let us know how it goes.
Srinivas
04-23-2005 1:49 AM
hay guys,
Thanks for your replies.
I tried doing below sample problem.It always goes to `error' part. Why. Is anything i have to change here.
pls have a look at my code snippets.
Data: t_exec_protocol like btcxpm occurs 0 with header line.
call executable
CALL FUNCTION 'SXPG_COMMAND_EXECUTE'
EXPORTING
COMMANDNAME = 'ZTESTING'
ADDITIONAL_PARAMETERS = param " Your parameters
OPERATINGSYSTEM = 'WINDOWS NT'
TARGETSYSTEM = 'rtjrs03'
DESTINATION = destination
STDOUT = 'X'
STDERR = 'X'
TERMINATIONWAIT = 'X'
TRACE =
IMPORTING
STATUS = status
EXITCODE = exitcode
TABLES
EXEC_PROTOCOL = t_exec_protocol
EXCEPTIONS
NO_PERMISSION = 1
COMMAND_NOT_FOUND = 2
PARAMETERS_TOO_LONG = 3
SECURITY_RISK = 4
WRONG_CHECK_CALL_INTERFACE = 5
PROGRAM_START_ERROR = 6
PROGRAM_TERMINATION_ERROR = 7
X_ERROR = 8
PARAMETER_EXPECTED = 9
TOO_MANY_PARAMETERS = 10
ILLEGAL_COMMAND = 11
WRONG_ASYNCHRONOUS_PARAMETERS = 12
CANT_ENQ_TBTCO_ENTRY = 13
JOBCOUNT_GENERATION_ERROR = 14
OTHERS = 15
.
if sy-subrc <> 0.
write 'error'.
endif.
I tried executing this FM in SE37 also. But i did not get External command execute. I think i have authorisation also...i checked su53 everything is successfull.
In ZTESTING External Command, i defined like this.
OS command : cmd
param for os comand : /C dir.
From SM49 i can execute this external command and gets
dir list display. but from FM or program i cant execute this.
pls guide me.
ambichan
04-24-2005 12:48 PM
Hi ambichan,
Can you provide the sy-subrc code?
Maybe change your error handling to:
WRITE: 'Error. RC= ', sy-subrc.
This should help a lot in determining the problem. For sure one of the EXCEPTIONS is being raised, and I think that will tell us exactly what the problem is.
Cheers,
Brad
04-24-2005 1:19 PM
hey brad,
thanks for your reply.
sy-subrc returns me 'command not found' error
but in sm49 , i have command 'ZTESTING' declared
and i can list the output (/c Dir)command while executing there.
but inside program its not working.
i think input is not properly read in FM.am i right.
can i use the /C dir command directly in FM.
ambichan.
04-24-2005 2:13 PM
Hi ambichan,
I think either the command or the operating system is not being recognised. Can you double check the spelling of both ZTESTING and WINDOWS NT as specified in your program correspond exactly with the text in SM49.
If you checked and they are correct, can you use variables for both these fields in the code instead of literals. I don't have sap with me, but it should be something like:
constants: c_cmd_test like <use FM type reference> value 'ZTESTING',
c_os_nt like <use FM type reference> value 'WINDOWS NT'.
Data: t_exec_protocol like btcxpm occurs 0 with header line.
* call executable
CALL FUNCTION 'SXPG_COMMAND_EXECUTE'
EXPORTING
COMMANDNAME = c_cmd_test
* ADDITIONAL_PARAMETERS = param
OPERATINGSYSTEM = c_os_nt
* TARGETSYSTEM = 'rtjrs03'
* DESTINATION = destination
STDOUT = 'X'
STDERR = 'X'
TERMINATIONWAIT = 'X'
* TRACE =
* IMPORTING
* STATUS = status
* EXITCODE = exitcode
TABLES
EXEC_PROTOCOL = t_exec_protocol
EXCEPTIONS
NO_PERMISSION = 1
COMMAND_NOT_FOUND = 2
PARAMETERS_TOO_LONG = 3
SECURITY_RISK = 4
WRONG_CHECK_CALL_INTERFACE = 5
PROGRAM_START_ERROR = 6
PROGRAM_TERMINATION_ERROR = 7
X_ERROR = 8
PARAMETER_EXPECTED = 9
TOO_MANY_PARAMETERS = 10
ILLEGAL_COMMAND = 11
WRONG_ASYNCHRONOUS_PARAMETERS = 12
CANT_ENQ_TBTCO_ENTRY = 13
JOBCOUNT_GENERATION_ERROR = 14
OTHERS = 15.
if sy-subrc <> 0.
write 'error'.
endif.
Cheers,
Brad
04-24-2005 3:10 PM
04-24-2005 3:35 PM
06-07-2005 3:39 PM
I have the same problem as you had
using Function Module SXPG_COMMAND_CHECK
sy-subrc returns me 'command not found' error
but
in sm49 i have a command declared and i can execute it there.
also inside a program the command is not working.
How did you fix ?
Thanks
06-07-2005 4:07 PM
Hi Sue,
If you can run the command from SM49 then it will work via the function module in SE37 and in your ABAP.
Three things to check:
1. Check the spelling of the command
2. Check the case of the command (use upper/lowercase flag when testing in SE37)
3. Check the operating system of the command.
Cheers,
Brad
06-08-2005 12:09 PM
Thanks, the operating system in SM49 was "Windows NT" SE37 was converting to "WINDOWS NT"
I created another command with O/S "WINDOWS NT" and all worked perfectly. (Don't know why we have "Windows NT" and "WINDOWS NT" which work differently).
Thanks again.