Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

call batch program from ABAP program

Former Member
0 Kudos

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.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

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

15 REPLIES 15

Former Member
0 Kudos

Hi Ambichan,

The function module WS_EXECUTE is your man.

Put the path of your batch file in the PROGRAM parameter.

Cheers,

Brad

Former Member
0 Kudos

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

Former Member
0 Kudos

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

0 Kudos

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.

0 Kudos

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

0 Kudos

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

0 Kudos

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

0 Kudos

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

0 Kudos

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.

0 Kudos

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

0 Kudos

thanks brad

i solved the problem..

0 Kudos

No problem.

0 Kudos

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

0 Kudos

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

0 Kudos

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.