Monday, November 15, 2010

Get Name of Files from Selected Directory to Excel File

REPORT  zfsl_gdirfn.

DATA: it_file LIKE STANDARD TABLE OF sdokpath WITH HEADER LINE,
      it_dir  LIKE STANDARD TABLE OF sdokpath WITH HEADER LINE,
      file_count TYPE i,
      dir_count TYPE i.

PARAMETER:  pdirp TYPE pfeflnamel OBLIGATORY, " Directory Path from where you want to get Name of Files
            psavep TYPE string OBLIGATORY. " Excel File Path and Name in which you want to Save Name of Files

CALL FUNCTION 'TMP_GUI_DIRECTORY_LIST_FILES' " To Get the Name of Files in Internal Table of Given Directory
  EXPORTING
    directory  = pdirp
    filter     = '*.*'
  IMPORTING
    file_count = file_count " Number of File in Directory
    dir_count  = dir_count " Number of Directory in Directory
  TABLES
    file_table = it_file " Table for Name of Files
    dir_table  = it_dir " Table for Name of Directories
  EXCEPTIONS
    cntl_error = 1
    OTHERS     = 2.
IF sy-subrc <> 0.
  MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
          WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.

IF file_count IS NOT INITIAL.
  CALL FUNCTION 'GUI_DOWNLOAD'
    EXPORTING
      filename                = psavep " Path and Name of Excel File with Extension .xls
    TABLES
      data_tab                = it_file " Internal Table having all Name of Files
    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.
  ENDIF.
ELSE.
  MESSAGE: 'No file found in the selected Directory' TYPE 'I'.
ENDIF.

INITIALIZATION.
  pdirp = 'C:\'.
  psavep = 'C:\test.xls'.

*F4 for Getting Directory Path
AT SELECTION-SCREEN ON VALUE-REQUEST FOR pdirp.
  DATA: fname TYPE string.

  CALL METHOD cl_gui_frontend_services=>directory_browse
    EXPORTING
      window_title         = 'Select Directory'
    CHANGING
      selected_folder      = fname
    EXCEPTIONS
      cntl_error           = 1
      error_no_gui         = 2
      not_supported_by_gui = 3
      OTHERS               = 4.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
               WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

  pdirp = fname.

*F4 for Getting File Path and Name
AT SELECTION-SCREEN ON VALUE-REQUEST FOR psavep.

  DATA: filename TYPE string,
        path TYPE string.

  CALL METHOD cl_gui_frontend_services=>file_save_dialog
    EXPORTING
      window_title         = 'Select path and file name'
      default_extension    = 'XLS'
    CHANGING
      filename             = filename
      path                 = path
      fullpath             = psavep
    EXCEPTIONS
      cntl_error           = 1
      error_no_gui         = 2
      not_supported_by_gui = 3
      OTHERS               = 4.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
               WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

0 comments:

Post a Comment