Conteúdo

Comparação do conteúdo de 2 arquivos

Digamos que você vez fez performane num programa que gera um relatório ou tem uma tabela de saída, e agora você precisa comparar o conteúdo, para ver se o resultado final é o mesmo. Está desenhado para comparar arquivos excel e texto. Informa quais são as linhas que estão diferentes e ainda gera um arquivo contendo apenas o que ficou diferente.
REPORT ZSAPeiros.

TYPES: BEGIN OF ty_file,
         line       TYPE string,
         cont(10)   TYPE n,
       END OF ty_file,

       BEGIN OF ty_file_fim,
         line       TYPE string,
       END OF ty_file_fim,

       BEGIN OF ty_file_aux,
         cont(10)   TYPE c,
         line       TYPE string,
       END OF ty_file_aux.

DATA: lv_hora_ini     TYPE sy-uzeit,
      lv_hora_fim     TYPE sy-uzeit,
      lv_hora         TYPE sy-uzeit,
      lv_cont(8)      TYPE c,
      lv_cont1(8)     TYPE c,
      lv_cont2(8)     TYPE c,
      lv_text(5000)   TYPE c,
      lv_filetype(10) TYPE c,
      lv_filename(30) TYPE c,
      lv_name_exp_srt TYPE string.

DATA: gt_file1         TYPE TABLE OF ty_file WITH HEADER LINE.
DATA: gt_file1_xls     TYPE TABLE OF ty_file_aux WITH HEADER LINE.
DATA: gt_file1_xls_exp TYPE TABLE OF ty_file_fim WITH HEADER LINE.
DATA: gt_file2         TYPE TABLE OF ty_file WITH HEADER LINE.
DATA: gt_file2_xls     TYPE TABLE OF ty_file_aux WITH HEADER LINE.
DATA: gt_file2_xls_exp TYPE TABLE OF ty_file_fim WITH HEADER LINE.

DATA: lv_call_mask(50)    TYPE c,
      lv_name_exp         TYPE ibipparms-path.


************************************************************************


PARAMETERS: p_file1 TYPE string OBLIGATORY,
            p_file2 TYPE string OBLIGATORY.


************************************************************************


INITIALIZATION.
  CLEAR lv_call_mask.
  CONCATENATE ',Excel (*.xls),*.XLS;*.XLSX' ',Texto (*.txt),*.TXT.'
         INTO lv_call_mask.


************************************************************************


AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file1.
  CALL FUNCTION 'WS_FILENAME_GET'
    EXPORTING
      def_filename     = space
      def_path         = space
      mask             = lv_call_mask
      mode             = 'O'
    IMPORTING
      filename         = lv_name_exp
    EXCEPTIONS
      inv_winsys       = 04
      no_batch         = 08
      selection_cancel = 12
      selection_error  = 16.
  p_file1 = lv_name_exp.


************************************************************************


AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file2.
  CALL FUNCTION 'WS_FILENAME_GET'
    EXPORTING
      def_filename     = space
      def_path         = space
      mask             = lv_call_mask
      mode             = 'O'
    IMPORTING
      filename         = lv_name_exp
    EXCEPTIONS
      inv_winsys       = 04
      no_batch         = 08
      selection_cancel = 12
      selection_error  = 16.
  p_file2 = lv_name_exp.


************************************************************************


END-OF-SELECTION.
  lv_hora_ini = sy-uzeit.

  CALL FUNCTION 'GUI_UPLOAD'
    EXPORTING
      filename = p_file1
    TABLES
      data_tab = gt_file1.

  CALL FUNCTION 'GUI_UPLOAD'
    EXPORTING
      filename = p_file2
    TABLES
      data_tab = gt_file2.


************************************************************************


  LOOP AT gt_file1.
    gt_file1-cont = sy-tabix.
    MODIFY gt_file1 INDEX sy-tabix.
  ENDLOOP.

  LOOP AT gt_file2.
    gt_file2-cont = sy-tabix.
    MODIFY gt_file2 INDEX sy-tabix.
  ENDLOOP.

  SORT: gt_file1 BY line,
        gt_file2 BY line.


************************************************************************


  LOOP AT gt_file1.
    READ TABLE gt_file2 WITH KEY line = gt_file1-line BINARY SEARCH.
    IF sy-subrc IS NOT INITIAL.
      lv_cont1 = lv_cont1 + 1.
      CONDENSE lv_cont1 NO-GAPS.
      gt_file1_xls-cont = gt_file1-cont.
      gt_file1_xls-line = gt_file1-line.
      APPEND gt_file1_xls.
    ENDIF.
  ENDLOOP.

  LOOP AT gt_file2.
    READ TABLE gt_file1 WITH KEY line = gt_file2-line BINARY SEARCH.
    IF sy-subrc IS NOT INITIAL.
      lv_cont2 = lv_cont2 + 1.
      CONDENSE lv_cont2 NO-GAPS.
      gt_file2_xls-cont = gt_file2-cont.
      gt_file2_xls-line = gt_file2-line.
      APPEND gt_file2_xls.
    ENDIF.
  ENDLOOP.


************************************************************************


  SORT: gt_file1_xls BY cont,
        gt_file2_xls BY cont.


************************************************************************


  CLEAR lv_text.
  CONCATENATE sy-vline
              'TEM NO ARQUIVO ARQUIVO'
              p_file1
              'E NÃO TEM NO ARQUIVO'
              p_file2
         INTO lv_text SEPARATED BY space.
  WRITE: / lv_text COLOR COL_HEADING.

  WRITE: / sy-uline.

  LOOP AT gt_file1_xls.
    gt_file1_xls_exp-line = gt_file1_xls-line.
    APPEND gt_file1_xls_exp.
    IF sy-tabix = 1.
      CLEAR lv_text.
      lv_text+0(1)  = sy-vline.
      lv_text+2(10) = 'Linha'.
      lv_text+13(1) = sy-vline.
      lv_text+15    = 'Valor da linha'.
      WRITE: / lv_text COLOR COL_GROUP INTENSIFIED OFF.
      WRITE: / sy-uline.
    ENDIF.
    CLEAR lv_text.
    lv_text+0(1)  = sy-vline.
    lv_text+2(10) = gt_file1_xls-cont.
    lv_text+13(1) = sy-vline.
    lv_text+15    = gt_file1_xls-line.
    WRITE: / lv_text COLOR COL_HEADING INTENSIFIED OFF.
  ENDLOOP.


************************************************************************


  WRITE: / sy-uline.
  CLEAR lv_text.
  lv_text(1)   = sy-vline.
  WRITE: / lv_text COLOR COL_NEGATIVE INTENSIFIED.
  WRITE: / sy-uline.


************************************************************************


  CLEAR lv_text.
  CONCATENATE sy-vline
              'TEM NO ARQUIVO ARQUIVO'
              p_file2
              'E NÃO TEM NO ARQUIVO'
              p_file1
         INTO lv_text SEPARATED BY space.
  WRITE: / lv_text COLOR COL_HEADING.

  WRITE: / sy-uline.

  LOOP AT gt_file2_xls.
    gt_file2_xls_exp-line = gt_file2_xls-line.
    APPEND gt_file2_xls_exp.
    IF sy-tabix = 1.
      CLEAR lv_text.
      lv_text+0(1)  = sy-vline.
      lv_text+2(10) = 'Linha'.
      lv_text+13(1) = sy-vline.
      lv_text+15    = 'Valor da linha'.
      WRITE: / lv_text COLOR COL_GROUP INTENSIFIED OFF.
      WRITE: / sy-uline.
    ENDIF.
    CLEAR lv_text.
    lv_text+0(1)  = sy-vline.
    lv_text+2(10) = gt_file2_xls-cont.
    lv_text+13(1) = sy-vline.
    lv_text+15    = gt_file2_xls-line.
    WRITE: / lv_text COLOR COL_HEADING INTENSIFIED OFF.
  ENDLOOP.


************************************************************************


  WRITE: / sy-uline.


************************************************************************


* Linhas do arquivo 1
  CONDENSE lv_cont1 NO-GAPS.
  CLEAR lv_text.
  CONCATENATE sy-vline
              lv_cont1
              'linhas diferentes no arquivo'
              p_file1
         INTO lv_text SEPARATED BY space.

  IF lv_cont1 IS INITIAL.
    WRITE: / lv_text.
  ELSE.
    WRITE: / lv_text COLOR COL_TOTAL INTENSIFIED ON.
  ENDIF.


************************************************************************


  WRITE: / sy-uline.


************************************************************************


* Linhas do arquivo 2
  CONDENSE lv_cont2 NO-GAPS.
  CLEAR lv_text.
  CONCATENATE sy-vline
              lv_cont2
              'linhas diferentes no arquivo'
              p_file2
         INTO lv_text SEPARATED BY space.

  IF lv_cont2 IS INITIAL.
    WRITE: / lv_text.
  ELSE.
    WRITE: / lv_text COLOR COL_TOTAL INTENSIFIED ON.
  ENDIF.


************************************************************************


  WRITE: / sy-uline.


************************************************************************


* Linhas do arquivo 1 e 2
  lv_cont = lv_cont1 + lv_cont2.
  CONDENSE lv_cont NO-GAPS.
  CLEAR lv_text.
  CONCATENATE sy-vline
              lv_cont
              'linhas diferentes no total'
         INTO lv_text SEPARATED BY space.

  IF lv_cont IS INITIAL.
    WRITE: / lv_text.
  ELSE.
    WRITE: / lv_text COLOR COL_TOTAL INTENSIFIED ON.
  ENDIF.


************************************************************************


  WRITE: / sy-uline.


************************************************************************


  lv_hora_fim = sy-uzeit.
  lv_hora = lv_hora_fim - lv_hora_ini.

  CLEAR lv_text.
  CONCATENATE sy-vline
              'Tempo gasto:'
              lv_hora
         INTO lv_text SEPARATED BY space.
  WRITE: / lv_text COLOR COL_POSITIVE INTENSIFIED ON USING EDIT MASK '_________________:__:__'.


************************************************************************


  WRITE: / sy-uline.


************************************************************************


  CLEAR lv_text.
  CONCATENATE sy-vline
              'Duplo clique para exportar as linhas diferentes do arquivo 1:'
              p_file1
              'para TXT ou XLS'
         INTO lv_text SEPARATED BY space.
  WRITE: / lv_text COLOR COL_GROUP INTENSIFIED OFF.


************************************************************************


  WRITE: / sy-uline.


************************************************************************


  CLEAR lv_text.
  CONCATENATE sy-vline
              'Duplo clique para exportar as linhas diferentes do arquivo 2:'
              p_file2
              'para TXT ou XLS'
         INTO lv_text SEPARATED BY space.
  WRITE: / lv_text COLOR COL_GROUP INTENSIFIED OFF.

  WRITE: / sy-uline.


************************************************************************


AT LINE-SELECTION.
  IF sy-ucomm EQ 'PICK'.


************************************************************************


    IF sy-lisel+2(26) = 'Duplo clique para exportar' AND
       sy-lisel+61(1) = '1'.

      lv_cont = STRLEN( p_file1 ).
      lv_cont = lv_cont - 4.
      CONCATENATE p_file1(lv_cont)
                  '_DIF'
             INTO lv_filename.

      CLEAR lv_name_exp.
      CALL FUNCTION 'WS_FILENAME_GET'
        EXPORTING
          def_filename     = lv_filename
          def_path         = space
          mask             = lv_call_mask
          mode             = 'S'
        IMPORTING
          filename         = lv_name_exp
        EXCEPTIONS
          inv_winsys       = 04
          no_batch         = 08
          selection_cancel = 12
          selection_error  = 16.

      IF sy-subrc IS INITIAL.
        lv_cont = STRLEN( lv_name_exp ).
        lv_cont = lv_cont - 3.
        IF lv_name_exp+lv_cont(3) = 'XLS'.
          lv_filetype = 'DAT'.
        ELSE.
          lv_filetype = 'ASC'.
        ENDIF.
        lv_name_exp_srt = lv_name_exp.
        CALL FUNCTION 'GUI_DOWNLOAD'
          EXPORTING
            filename              = lv_name_exp_srt
            filetype              = lv_filetype
            write_field_separator = 'X'
          TABLES
            data_tab              = gt_file1_xls_exp.
      ENDIF.

    ENDIF.


************************************************************************


    IF sy-lisel+2(26) = 'Duplo clique para exportar' AND
       sy-lisel+61(1) = '2'.

      lv_cont = STRLEN( p_file2 ).
      lv_cont = lv_cont - 4.
      CONCATENATE p_file2(lv_cont)
                  '_DIF'
             INTO lv_filename.

      CLEAR lv_name_exp.
      CALL FUNCTION 'WS_FILENAME_GET'
        EXPORTING
          def_filename     = lv_filename
          def_path         = space
          mask             = lv_call_mask
          mode             = 'S'
        IMPORTING
          filename         = lv_name_exp
        EXCEPTIONS
          inv_winsys       = 04
          no_batch         = 08
          selection_cancel = 12
          selection_error  = 16.

      IF sy-subrc IS INITIAL.
        lv_cont = STRLEN( lv_name_exp ).
        lv_cont = lv_cont - 3.
        IF lv_name_exp+lv_cont(3) = 'XLS'.
          lv_filetype = 'DAT'.
        ELSE.
          lv_filetype = 'ASC'.
        ENDIF.
        lv_name_exp_srt = lv_name_exp.
        CALL FUNCTION 'GUI_DOWNLOAD'
          EXPORTING
            filename              = lv_name_exp_srt
            filetype              = lv_filetype
            write_field_separator = 'X'
          TABLES
            data_tab              = gt_file2_xls_exp.
      ENDIF.

    ENDIF.

  ENDIF.