REPORT ZZBGS101 MESSAGE-ID Z1 LINE-COUNT 65 LINE-SIZE 132
                NO STANDARD PAGE HEADING.
*----------------------------------------------------------------------*
* Description: Download any tables to the desktop, without dialog.     *
*                                                                      *
* Implementing:This program dynamic build sourcecode for downloading   *
*              tables to the PC. The download take place for each 1000 *
*              records. The program uses authorization checks to secure*
*              that only authorized personal can use the program.      *
*                                                                      *
* WARNING!     When naming the dynamic abap/4 program, be sure that the*
*              name is not used and will not be used in the future. SAP*
*              does not warning you but just overwrite existing program*
*                                                                      *
* Authoriza-   S_TABU_DIS, S_PROGRAM                                   *
* tions:                                                               *
*                                                                      *
* Submitting:  Via SA38, SA39 or via batch with variant.               *
*                                                                      *
* Parametre:   TABLE:   = Tablename                                    *
*              PATH:    = Target path without the filename             *
*                                                                      *
* Output:      File downloaded to <PATH><TABLE>.TXT                    *
*                                                                      *
* Customizing: Need no customization .                                 *
*                                                                      *
* Change of    Need no changes.                                        *
* release:                                                             *
*                                                                      *
* Programmer:  Benny G. Sørensen,                                      *
* Date:        September 1995.                                         *
*                                                                      *
* SAP Release: R/3 2.2x - 3.0x                                         *
*                                                                      *
*-------------------------------Corrections----------------------------*
* Date        Userid     Correction                                    *
*----------------------------------------------------------------------*
TABLES: DD02V
       ,TDDAT      " Maintenance areas for tables
       .

*----------------------------------------------------------------------*
* Selection screen definition                                          *
*----------------------------------------------------------------------*
SELECTION-SCREEN BEGIN OF LINE.
  SELECTION-SCREEN:
    COMMENT 01(70) TEXT-001.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN BEGIN OF LINE.
  SELECTION-SCREEN:
    COMMENT 01(70) TEXT-002.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN BEGIN OF LINE.
  SELECTION-SCREEN:
    COMMENT 01(70) TEXT-003.
  SELECTION-SCREEN:
    ULINE.
SELECTION-SCREEN END OF LINE.

*----------------------------------------------------------------------*
* Table declaration                                                    *
*----------------------------------------------------------------------*
PARAMETERS: TABLE    LIKE RSRD1-OBJNAME        OBLIGATORY
           ,PATH(60) TYPE C DEFAULT 'C:\SAP\'  OBLIGATORY.
           .
DATA: BEGIN OF SOURCE OCCURS 80,
        LINE(72),
      END OF SOURCE.

DATA: BEGIN OF ITAB OCCURS 0,
        LINE(72) TYPE C,
      END OF ITAB.

DATA: FILENAME(40) TYPE C
     ,RETURNCODE   LIKE SY-SUBRC
     ,WPATH(40)    TYPE C
     ,LENGTH       TYPE I
      .
FIELD-SYMBOLS: <PTR>.

*----------------------------------------------------------------------*
* Event: At Selection Screen                                           *
*----------------------------------------------------------------------*
AT SELECTION-SCREEN.

  SELECT SINGLE * FROM DD02V
    WHERE TABNAME = TABLE
      AND DDLANGUAGE = SY-LANGU.
  IF SY-SUBRC NE 0.
    MESSAGE E400.
  ENDIF.

  SELECT SINGLE * FROM TDDAT
    WHERE TABNAME = DD02V-TABNAME.

  AUTHORITY-CHECK OBJECT 'S_TABU_DIS'
    ID 'ACTVT'     FIELD '03'
    ID 'DICBERCLS' FIELD TDDAT-CCLASS.
  IF SY-SUBRC NE 0.
    MESSAGE I100.
  ENDIF.


*----------------------------------------------------------------------*
* Event Initialization                                                 *
*----------------------------------------------------------------------*
INITIALIZATION.
  CLEAR SOURCE.
  REFRESH SOURCE.

  AUTHORITY-CHECK OBJECT 'S_PROGRAM'
    ID 'P_GROUP'  FIELD 'ZZBCU128'
    ID 'P_ACTION' DUMMY.
  IF SY-SUBRC NE 0.
    MESSAGE I100.
  ENDIF.

* ---------------------------------------------------------------------*
* Event Start-of-selection                                             *
* ---------------------------------------------------------------------*
START-OF-SELECTION.
  AUTHORITY-CHECK OBJECT 'S_PROGRAM'
    ID 'P_GROUP'  FIELD 'ZZBCU128'
    ID 'P_ACTION' DUMMY.
  IF SY-SUBRC NE 0.
    MESSAGE E100.
  ENDIF.

  AUTHORITY-CHECK OBJECT 'S_TABU_DIS'
    ID 'ACTVT'     FIELD '03'
    ID 'DICBERCLS' FIELD TDDAT-CCLASS.
  IF SY-SUBRC NE 0.
    MESSAGE E100.
  ENDIF.

* Validate parameter
  CHECK TABLE NE SPACE.
  CHECK PATH  NE SPACE.

* Asure path ending with a slash
  CONDENSE PATH.
  LENGTH = STRLEN( PATH ).
  SUBTRACT 1 FROM LENGTH.
  ASSIGN PATH+LENGTH(1) TO <PTR>.
  IF <PTR> NE '\'.
    ADD 1 TO LENGTH.
    ASSIGN PATH+LENGTH(1) TO <PTR>.
    MOVE '\' TO <PTR>.
  ENDIF.

  SOURCE = 'REPORT ZZBGS### .' .                        APPEND SOURCE.
  SOURCE = 'TABLES: &.'.
  REPLACE '&' WITH TABLE INTO SOURCE.                   APPEND SOURCE.
  SOURCE = 'DATA: BEGIN OF TAB OCCURS 0.'.              APPEND SOURCE.
  SOURCE = '        INCLUDE STRUCTURE &.'.
  REPLACE '&' WITH TABLE INTO SOURCE.                   APPEND SOURCE.
  SOURCE = 'DATA: END OF TAB. '.                        APPEND SOURCE.
  SOURCE = 'DATA: BEGIN OF OUT OCCURS 0.'.              APPEND SOURCE.
  SOURCE = '        INCLUDE STRUCTURE &.'.
  REPLACE '&' WITH TABLE INTO SOURCE.                   APPEND SOURCE.
  SOURCE = 'DATA: END OF OUT. ' .                       APPEND SOURCE.
  SOURCE = 'DATA: FILENAME(40) TYPE C.'.                APPEND SOURCE.
  SOURCE = 'DATA: ROWS         TYPE I.'.                APPEND SOURCE.
  SOURCE = 'DATA: I            TYPE I.'.                APPEND SOURCE.
  SOURCE = 'DATA: MODE         TYPE C.'.                APPEND SOURCE.

  CALL FUNCTION 'STRING_CONCATENATE'
       EXPORTING
            STRING1 = PATH
            STRING2 = '&.TXT'
       IMPORTING
            STRING = FILENAME
       EXCEPTIONS
            TOO_SMALL = 01.

  CHECK SY-SUBRC = 0.
  REPLACE '&' WITH TABLE INTO FILENAME.
  CONDENSE FILENAME NO-GAPS.
  SOURCE = '  FILENAME = ''&'' .' .
  REPLACE '&' WITH FILENAME INTO SOURCE.                APPEND SOURCE.
  SOURCE = '  REFRESH TAB.'.                            APPEND SOURCE.
  SOURCE = '  SELECT * FROM & INTO TAB. ' .
  REPLACE '&' WITH TABLE INTO SOURCE.                   APPEND SOURCE.
  SOURCE = '    APPEND TAB.                      '.     APPEND SOURCE.
  SOURCE = '  ENDSELECT.                         '.     APPEND SOURCE.
  SOURCE = '  I = 0.                             '.     APPEND SOURCE.
  SOURCE = '  LOOP AT TAB.                       '.     APPEND SOURCE.
  SOURCE = '    OUT = TAB.                       '.     APPEND SOURCE.
  SOURCE = '    APPEND OUT.                      '.     APPEND SOURCE.
  SOURCE = '    ADD 1 TO I.                      '.     APPEND SOURCE.
  SOURCE = '    IF I >= 1000.                    '.     APPEND SOURCE.
  SOURCE = '      CALL FUNCTION ''WS_DOWNLOAD'' '.      APPEND SOURCE.
  SOURCE = '        EXPORTING FILENAME = FILENAME'.     APPEND SOURCE.
  SOURCE = '                  FILETYPE = ''DAT'' '.     APPEND SOURCE.
  SOURCE = '                  MODE     = MODE    '.     APPEND SOURCE.
  SOURCE = '        TABLES    DATA_TAB = OUT.    '.     APPEND SOURCE.
  SOURCE = '      MODE = ''A''.                  '.     APPEND SOURCE.
  SOURCE = '      REFRESH OUT.                   '.     APPEND SOURCE.
  SOURCE = '      I = 0.                         '.     APPEND SOURCE.
  SOURCE = '    ENDIF.                           '.     APPEND SOURCE.
  SOURCE = '  ENDLOOP.                           '.     APPEND SOURCE.
  SOURCE = '  CALL FUNCTION ''WS_DOWNLOAD''      '.     APPEND SOURCE.
  SOURCE = '    EXPORTING FILENAME = FILENAME    '.     APPEND SOURCE.
  SOURCE = '              FILETYPE = ''DAT''     '.     APPEND SOURCE.
  SOURCE = '              MODE     = MODE        '.     APPEND SOURCE.
  SOURCE = '    TABLES    DATA_TAB = OUT.        '.     APPEND SOURCE.

* Look for existence of sourcecode
  READ REPORT 'ZZBGS###' INTO ITAB.
  IF SY-SUBRC > 0.
    INSERT REPORT 'ZZBGS###' FROM SOURCE.
  ELSE.
    MESSAGE E065 WITH 'Error: Sourcecode exist'.
  ENDIF.

  SUBMIT ZZBGS### AND RETURN.
  DELETE REPORT 'ZZBGS###'.