REPORT  ZTEST6      NO STANDARD PAGE HEADING MESSAGE-ID SAPLWOSA.
*=======================================================================
* Variables
*=======================================================================
DATA DESCR_STRUCT_REF TYPE REF TO CL_ABAP_STRUCTDESCR.
DATA WA_FCAT TYPE LVC_S_FCAT.
DATA IT_FIELDCATALOG TYPE LVC_T_FCAT.
DATA DATAREF TYPE REF TO DATA.
DATA: ONE  LIKE PCFILE-DRIVE,
      TWO  LIKE PCFILE-PATH,
      LONG TYPE I,
      FLAG TYPE C,
      FILEPATH(128) TYPE C,
      FILE_TAB TYPE STRING,
      TABNAME LIKE DD02L-TABNAME.
*=======================================================================
* Field-Symbols.
*=======================================================================
FIELD-SYMBOLS:
              <ROW> TYPE ANY TABLE,
              <TABLE> TYPE STANDARD TABLE,
              <COMPONENT> TYPE ABAP_COMPDESCR,
              <FS>  TYPE ANY.
*=======================================================================
* Selection screen
*=======================================================================
SELECTION-SCREEN BEGIN OF BLOCK DATA WITH FRAME TITLE TEXT-T01.
PARAMETERS:
    TABNAM(128) TYPE C,
    FUNCTION(1) TYPE C OBLIGATORY,
    LISTNAME LIKE RLGRAP-FILENAME.
SELECTION-SCREEN END OF BLOCK DATA.
*=======================================================================
* At Selection screen
*=======================================================================
AT SELECTION-SCREEN ON VALUE-REQUEST FOR LISTNAME.
  PERFORM GET_FILENAME CHANGING LISTNAME.
*=======================================================================
* Start-of-selection
*=======================================================================
START-OF-SELECTION.
  CLEAR FLAG.
  PERFORM LOAD_DATA USING TABNAM.
  PERFORM VERIFY_TABLE USING TABNAM CHANGING FLAG.
  IF FLAG NE 'X'.
    IF FUNCTION EQ 'D'.
      PERFORM CREATE_TABLE USING TABNAM.
      PERFORM DOWNLOAD_TABLE USING TABNAM.
    ELSE.
     IF FUNCTION EQ 'U'.
      PERFORM CREATE_TABLE USING TABNAM.
      PERFORM UPLOAD_TABLE USING TABNAM.
    ENDIF.
*  ELSE.
    MESSAGE S000 WITH 'The proposed table doesn''t exist.'.
  ENDIF.
    ENDIF.
*---------------------------------------------------------------------*
*       FORM GET_FILENAME                                             *
*---------------------------------------------------------------------*
*       Name of the directory.
*---------------------------------------------------------------------*
FORM GET_FILENAME CHANGING LISTNAME.
  CALL FUNCTION 'WS_FILENAME_GET'
       EXPORTING
            DEF_FILENAME     = LISTNAME
            DEF_PATH         = 'C:\downloads\list'
            MASK             = ',*.*,*.*. '
            MODE             = 'S'
            TITLE            = 'Save as'
       IMPORTING
            FILENAME         = LISTNAME
       EXCEPTIONS
            INV_WINSYS       = 1
            NO_BATCH         = 2
            SELECTION_CANCEL = 3
            SELECTION_ERROR  = 4
            OTHERS           = 5.
ENDFORM.
*---------------------------------------------------------------------*
*       FORM LOAD_DATA                                                *
*---------------------------------------------------------------------*
*       Specifies the path and table name
*---------------------------------------------------------------------*
FORM LOAD_DATA USING MY_TAB.
  DATA: W_FILE LIKE PCFILE-PATH.
  W_FILE = LISTNAME.
  CALL FUNCTION 'PC_SPLIT_COMPLETE_FILENAME'
       EXPORTING
            COMPLETE_FILENAME = W_FILE
       IMPORTING
            DRIVE             = ONE
            PATH              = TWO
       EXCEPTIONS
            INVALID_DRIVE     = 1
            INVALID_EXTENSION = 2
            INVALID_NAME      = 3
            INVALID_PATH      = 4
            OTHERS            = 5.
  CONCATENATE ONE ':' TWO INTO FILEPATH.
  CONCATENATE FILEPATH MY_TAB '.txt' INTO FILE_TAB.
ENDFORM.
*---------------------------------------------------------------------*
*       FORM DOWNLOAD_TABLE                                           *
*---------------------------------------------------------------------*
*       Downloads the table data.
*---------------------------------------------------------------------*
FORM DOWNLOAD_TABLE USING MY_TAB.
  SELECT *
  INTO TABLE <ROW>
  FROM (MY_TAB).
  ASSIGN <ROW> TO <TABLE>.
  CALL FUNCTION 'GUI_DOWNLOAD'
       EXPORTING
            FILENAME                = FILE_TAB
            FILETYPE                = 'ASC'
       TABLES
            DATA_TAB                = <TABLE>
       EXCEPTIONS
            FILE_WRITE_ERROR        = 1
            NO_BATCH                = 2
            GUI_REFUSE_FILETRANSFER = 3
            INVALID_TYPE            = 4
            NO_AUTHORITY            = 5
            UNKNOWN_ERROR           = 6.
  IF SY-SUBRC EQ 0.
    CALL FUNCTION 'POPUP_TO_INFORM'
         EXPORTING
              TITEL = 'Successful Download'
              TXT1  = 'All the data from the table'
              TXT2  = 'was correctly downloaded.'.
  ELSE.
    CALL FUNCTION 'POPUP_TO_INFORM'
         EXPORTING
              TITEL = 'Download Error!'
              TXT1  = 'The data of the table'
              TXT2  = 'couldn''t be downloaded.'.
  ENDIF.
ENDFORM.
*---------------------------------------------------------------------*
*       FORM UPLOAD_TABLE                                           *
*---------------------------------------------------------------------*
*       Table Upload.
*---------------------------------------------------------------------*
FORM UPLOAD_TABLE USING MY_TAB.
  ASSIGN <ROW> TO <TABLE>.
  CALL FUNCTION 'GUI_UPLOAD'
       EXPORTING
            FILENAME                = FILE_TAB
            FILETYPE                = 'ASC'
       IMPORTING
            FILELENGTH              = LONG
       TABLES
            DATA_TAB                = <TABLE>
       EXCEPTIONS
            FILE_OPEN_ERROR         = 1
            FILE_READ_ERROR         = 2
            NO_BATCH                = 3
            GUI_REFUSE_FILETRANSFER = 4
            INVALID_TYPE            = 5
            NO_AUTHORITY            = 6
            UNKNOWN_ERROR           = 7.
  MODIFY (MY_TAB) FROM TABLE <TABLE>.
  IF SY-SUBRC EQ 0.
    CALL FUNCTION 'POPUP_TO_INFORM'
         EXPORTING
              TITEL = 'Successful Upload'
              TXT1  = 'All the data from the table'
              TXT2  = 'was correctly uploaded.'.
  ELSE.
    CALL FUNCTION 'POPUP_TO_INFORM'
         EXPORTING
              TITEL = 'Upload Error!'
              TXT1  = 'The data of the table'
              TXT2  = 'couldn''t be uploaded.'.
  ENDIF.
ENDFORM.
*---------------------------------------------------------------------*
*       FORM CREATE_TABLE                                             *
*---------------------------------------------------------------------*
*       Creates a dynamic internal table.
*---------------------------------------------------------------------*
FORM CREATE_TABLE USING MY_TAB.
  CREATE DATA DATAREF TYPE (MY_TAB).
  ASSIGN DATAREF->* TO <FS>.
  DESCR_STRUCT_REF ?= CL_ABAP_TYPEDESCR=>DESCRIBE_BY_DATA( <FS> ).
  LOOP AT DESCR_STRUCT_REF->COMPONENTS ASSIGNING <COMPONENT>.
    WA_FCAT-FIELDNAME     = <COMPONENT>-NAME.
    WA_FCAT-REF_TABLE     = MY_TAB.
    WA_FCAT-REF_FIELD     = <COMPONENT>-NAME.
    APPEND WA_FCAT TO IT_FIELDCATALOG.
  ENDLOOP.
  CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE
     EXPORTING
       IT_FIELDCATALOG           = IT_FIELDCATALOG
     IMPORTING
       EP_TABLE                  = DATAREF
     EXCEPTIONS
       GENERATE_SUBPOOL_DIR_FULL = 1
       OTHERS                    = 2.
  ASSIGN DATAREF->* TO <ROW>.
ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  VERIFY_TABLE
*&---------------------------------------------------------------------*
*       The table must exist!
*----------------------------------------------------------------------*
FORM VERIFY_TABLE USING TABNAM CHANGING FLAG.
  SELECT SINGLE TABNAME
  INTO (TABNAME)
  FROM DD02L
  WHERE TABNAME EQ TABNAM.
  IF SY-SUBRC NE 0.
    FLAG = 'X'.
  ENDIF.
ENDFORM.


Serkan AKKAVAK
Computer Engineer
ABAP Developer & SAP MM SD Consultant
Contact : serkurumsal@yandex.com