Additional Blog Posts by Members
cancel
Showing results for 
Search instead for 
Did you mean: 
ec1
Active Participant
10,168

Purpose: Download all Function Module source code belonging to a Function Group to local file system

Usage Scenario:

- you are working a new function group which doesn't have a version history and you want to take a backup of the source code

- backup source code in between releasing the code

Please note that the following code will only download FM. Feel free to extend or modify the code to your needs.

REPORT  Z_UTIL_FG_DOWNLOAD.

TYPES: BEGIN OF ty_enlfdir,

        funcname TYPE enlfdir-funcname,

        END OF ty_enlfdir,

        BEGIN OF ty_tfdir,

          funcname TYPE tfdir-funcname,

          pname    TYPE tfdir-pname,

          include  TYPE tfdir-include,

        END OF ty_tfdir,

        BEGIN OF ty_text,

          txt(1000) TYPE c,

        END OF ty_text.

PARAMETERS: p_fg TYPE tlibg-area, "Function group

            p_dir TYPE string,    "Directory

            p_ext(3) TYPE c LOWER CASE.      "File extension

DATA: t_fm TYPE STANDARD TABLE OF tfdir,

      wa_fm LIKE LINE OF t_fm,

      t_enlfdir TYPE STANDARD TABLE OF ty_enlfdir,

      wa_enlfdir LIKE LINE OF t_enlfdir,

      r_enlfdir TYPE RANGE OF enlfdir-area,

      wa_r_enlfdir LIKE LINE OF r_enlfdir,

      t_tfdir TYPE STANDARD TABLE OF ty_tfdir,

      wa_tfdir LIKE LINE OF t_tfdir.

DATA: t_s TYPE STANDARD TABLE OF ty_text.

* Check that Function group exists

SELECT SINGLE area INTO p_fg

  FROM tlibg

  WHERE area = p_fg.

IF sy-subrc = 0.

* Get all function module names

  SELECT DISTINCT funcname

    FROM enlfdir

    INTO CORRESPONDING FIELDS OF TABLE t_enlfdir

    WHERE area = p_fg.

  IF sy-subrc = 0.

    LOOP AT t_enlfdir INTO wa_enlfdir.

      CLEAR: wa_r_enlfdir.

      wa_r_enlfdir-sign = 'I'.

      wa_r_enlfdir-option = 'EQ'.

      wa_r_enlfdir-low = wa_enlfdir-funcname.

      APPEND wa_r_enlfdir TO r_enlfdir.

    ENDLOOP.

*   Get INCLUDE number

    SELECT funcname pname include

      FROM tfdir

      INTO CORRESPONDING FIELDS OF TABLE t_tfdir

      WHERE funcname IN r_enlfdir.

    DATA: l_include TYPE RS38L-INCLUDE,

          l_filename TYPE string.

    LOOP AT t_tfdir INTO wa_tfdir.

      CLEAR: l_include.

*     Get INCLUDE name of the corresponding FM

      CALL FUNCTION 'FUNCTION_INCLUDE_CONCATENATE'

        EXPORTING

          INCLUDE_NUMBER           = wa_tfdir-include

        IMPORTING

          INCLUDE                  = l_include

        CHANGING

          PROGRAM                  = wa_tfdir-pname

        EXCEPTIONS

          NOT_ENOUGH_INPUT         = 1

          NO_FUNCTION_POOL         = 2

          DELIMITER_WRONG_POSITION = 3

          OTHERS                   = 4.

      IF SY-SUBRC = 0.

        CLEAR: t_s[].

* Get the FM source code

        READ REPORT l_include INTO t_s.

        CONDENSE p_dir.

        CONCATENATE p_dir wa_tfdir-funcname '.' p_ext INTO l_filename.

* Download the source to client file system

        CALL FUNCTION 'GUI_DOWNLOAD'

          EXPORTING

            FILENAME                = l_filename

            FILETYPE                = 'ASC'

            CONFIRM_OVERWRITE       = 'X'

            SHOW_TRANSFER_STATUS    = ABAP_FALSE

          TABLES

            DATA_TAB                = t_s

          EXCEPTIONS

            FILE_WRITE_ERROR        = 1

            NO_BATCH                = 2

            GUI_REFUSE_FILETRANSFER = 3

            INVALID_TYPE            = 4

            NO_AUTHORITY            = 5

            UNKNOWN_ERROR           = 6

            HEADER_NOT_ALLOWED      = 7

            SEPARATOR_NOT_ALLOWED   = 8

            FILESIZE_NOT_ALLOWED    = 9

            HEADER_TOO_LONG         = 10

            DP_ERROR_CREATE         = 11

            DP_ERROR_SEND           = 12

            DP_ERROR_WRITE          = 13

            UNKNOWN_DP_ERROR        = 14

            ACCESS_DENIED           = 15

            DP_OUT_OF_MEMORY        = 16

            DISK_FULL               = 17

            DP_TIMEOUT              = 18

            FILE_NOT_FOUND          = 19

            DATAPROVIDER_EXCEPTION  = 20

            CONTROL_FLUSH_ERROR     = 21

            OTHERS                  = 22.

        IF SY-SUBRC <> 0.

          MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

          RETURN.

        ENDIF.

      ENDIF.

    ENDLOOP.

  ENDIF.

ELSE.

  WRITE:/ 'Function group not found'.

ENDIF.

2 Comments