Skip to Content
avatar image
Former Member

Create file in background, on NT server...

Hey,

I have a program wich creates a list. when it runs in forground, all data is shown in an alv grind, wich can be exported to excel.

Now is it also possible to run this program in background, and then the requirements are that a file should be created and put on an NT-server... is this possible in background ??? (i can create the file with open dataset & close dataset on the app server, but i also should transfer it to the NT network...)

Kind regards,

Tom

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

1 Answer

  • Aug 22, 2007 at 04:13 PM

    Hi,

    If the NT server is FTP enabled then, you can FTP the file after creating using OPEN DATASET

    Here is the example code.
    
    *&---------------------------------------------------------------------*
    *& Form open_ftp_connection
    *&---------------------------------------------------------------------*
    * Open the FTP connection
    *----------------------------------------------------------------------*
    FORM open_ftp_connection .
    DATA: l_key TYPE i VALUE 26101957,
    l_dstlen TYPE i,
    l_rfc LIKE rfcdes-rfcdest.
    
    * Find the RFC destination
    IF sy-batch IS INITIAL.
    l_rfc = 'SAPFTP'.
    ELSE.
    l_rfc = 'SAPFTPA'.
    ENDIF.
    
    * Encrypt the password, Required for FTP connection
    CALL 'AB_RFC_X_SCRAMBLE_STRING'
    ID 'SOURCE' FIELD v_ftppwd
    ID 'KEY' FIELD l_key
    ID 'SCR' FIELD 'X'
    ID 'DESTINATION' FIELD v_ftppwd
    ID 'DSTLEN' FIELD l_dstlen.
    
    * Open the FTP connection
    CALL FUNCTION 'FTP_CONNECT'
    EXPORTING
    user = v_ftpusr
    password = v_ftppwd
    host = v_ftpsrv
    rfc_destination = l_rfc
    IMPORTING
    handle = v_hdl
    EXCEPTIONS
    not_connected = 01
    OTHERS = 02.
    
    IF sy-subrc NE 0.
    MESSAGE e200(zhr) WITH 'Failed to open FTP connection!'(020).
    ELSE.
    * FTP the file
    PERFORM send_file.
    * Close the FTP connection
    PERFORM close_ftp_connection.
    ENDIF.
    
    
    ENDFORM. " open_ftp_connection
    
    *&---------------------------------------------------------------------*
    *& Form send_file
    *&---------------------------------------------------------------------*
    * FTP the file
    *----------------------------------------------------------------------*
    FORM send_file .
    DATA: l_cmd(80) TYPE c,
    l_fil(80) TYPE c,
    l_loc TYPE i,
    l_lng TYPE i.
    
    DATA: BEGIN OF t_result OCCURS 0,
    line(100) TYPE c,
    END OF t_result.
    
    * Change remote directory
    CLEAR l_cmd.
    CONCATENATE 'cd' v_ftploc INTO l_cmd SEPARATED BY space.
    CALL FUNCTION 'FTP_COMMAND'
    EXPORTING
    handle = v_hdl
    command = l_cmd
    TABLES
    data = t_result
    EXCEPTIONS
    tcpip_error = 01
    command_error = 02
    data_error = 03
    OTHERS = 04.
    
    IF sy-subrc NE 0.
    MESSAGE e200(zhr) WITH 'Unable to issue command:'(021) l_cmd.
    ENDIF.
    
    * Find the local directory
    l_loc = 1.
    WHILE sy-subrc EQ 0.
    SEARCH p_ofile FOR '/' STARTING AT l_loc.
    IF sy-fdpos GT 0.
    ADD sy-fdpos TO l_loc.
    l_cmd = p_ofile(l_loc).
    l_lng = 60 - l_loc.
    l_fil = p_ofile+l_loc(l_lng).
    ELSE.
    ADD 1 TO l_loc.
    ENDIF.
    ENDWHILE.
    
    * Change the local location to the file output
    CONCATENATE 'lcd' l_cmd INTO l_cmd SEPARATED BY space.
    CALL FUNCTION 'FTP_COMMAND'
    EXPORTING
    handle = v_hdl
    command = l_cmd
    TABLES
    data = t_result
    EXCEPTIONS
    tcpip_error = 01
    command_error = 02
    data_error = 03
    OTHERS = 04.
    
    IF sy-subrc NE 0.
    MESSAGE e200(zhr) WITH 'Unable to issue command:'(021) l_cmd.
    ENDIF.
    
    * Send a file
    CLEAR l_cmd.
    CONCATENATE 'put' l_fil INTO l_cmd SEPARATED BY space.
    CALL FUNCTION 'FTP_COMMAND'
    EXPORTING
    handle = v_hdl
    command = l_cmd
    TABLES
    data = t_result
    EXCEPTIONS
    tcpip_error = 01
    command_error = 02
    data_error = 03
    OTHERS = 04.
    
    IF sy-subrc NE 0.
    MESSAGE e200(zhr) WITH 'Unable to issue command:'(021) l_cmd.
    ELSE.
    MESSAGE i200(zhr) WITH text-022. "File FTP'ed to the server.
    ENDIF.
    
    ENDFORM. " send_file
    *&---------------------------------------------------------------------*
    *& Form close_ftp_connection
    *&---------------------------------------------------------------------*
    * Close the FTP connection
    *----------------------------------------------------------------------*
    FORM close_ftp_connection .
    * Done! close FTP connection
    CALL FUNCTION 'FTP_DISCONNECT'
    EXPORTING
    handle = v_hdl.
    ENDFORM. " close_ftp_connection
    
    

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      Great!! This will help me a lot. I'll gone check if the server is FTP-enabled. If so, i'll will implement it, and see if & how it works ;).

      Thanks man,

      you're a lifesaver!