sapdev logo background
sapdev logo sapdev logo
Comments

Updating single and multiple HTML/text files using an SAP program




This application was created by me to allow me to update the same content to hundreds of files at the same time. So instead of going into each an every data file and adding a new peice of text or HTML code you can simply point the ABAP program to the new data to add, where it should go, when it shouldn't update etc and then press the execute button. All the .htm files within the selected directory and any sub directories will be updated accordingly!

See below for screen shot of report selection screen, and Click here for detailed description of what each parameter does. If you want to install this program simply copy and paste the below code into a new SAP ABAP report program.



*&-------------------------------------------------------------*
*& Report  ZHTMUPDATE
*&
*&-------------------------------------------------------------*
*&
*&
*&-------------------------------------------------------------*
REPORT  Zhtmupdate.


*   Retrieve data file from presentation server(Upload from PC)
types: begin of t_datatab,
*  row(500) type c,
  row type string,
 end of t_datatab.
data:  it_datatab type STANDARD TABLE OF t_datatab,
       wa_datatab like line of it_datatab,
       it_datafind type STANDARD TABLE OF t_datatab,
       wa_datafind like line of it_datatab,
       it_datanew type STANDARD TABLE OF t_datatab,
       wa_datanew like line of it_datatab,
       it_datanewstor type STANDARD TABLE OF t_datatab.

* DATA: WA_FILE_TABLE TYPE FILE_INFO,
*        TAB TYPE STANDARD TABLE OF FILE_INFO.
*  data: len type i.
data:  it_files type STANDARD TABLE OF FILE_INFO,
       it_dirs type STANDARD TABLE OF FILE_INFO,
       wa_files like line of it_files,
       wa_dirs like line of it_dirs,

       gd_insert type string.

DATA: i_input type string,
      i_find  type string,
      i_new type string,
      gd_dir    type string,
      gd_count type i,
      gd_multi type i,
      gd_newtxt type  string,
      gd_head   type i.


*Selecting a File, plus inserting default file extension
selection-screen begin of block group with frame title text-s01.
parameters: p_selfil type c radiobutton group difi.
parameters: p_file like rlgrap-filename LOWER CASE.
parameters: p_seldir type c radiobutton group difi.
parameters: p_dir like rlgrap-filename.

selection-screen end of block group.

selection-screen begin of block find with frame title text-s02.
parameters: p_find like rlgrap-filename LOWER CASE.
parameters: p_new like rlgrap-filename LOWER CASE.
parameters: p_not type string.
parameters: p_check type string.

parameters: p_start type string.
parameters: p_end type string.

selection-screen begin of line.
selection-screen comment 1(79) text-S03.
selection-screen end of line.

selection-screen begin of line.
selection-screen comment 1(39) text-S04.
parameters: p_repfn type string.
selection-screen end of line.

*parameters: p_multi type c.
*parameters: p_repfn type string.
selection-screen end of block find.

selection-screen begin of block insert with frame title text-s01.
parameters: p_above  type c radiobutton group new.
parameters: p_below  type c radiobutton group new.
parameters: p_repl   type c radiobutton group new.
parameters: p_before  type c radiobutton group new.
parameters: p_after  type c radiobutton group new.
selection-screen end of block insert.


at selection-screen OUTPUT.
  gd_dir = p_dir.

at selection-screen on value-request for p_dir.

  CALL METHOD cl_gui_frontend_services=>directory_browse
    EXPORTING
      window_title    = 'File Directory'
      initial_folder  = gd_dir     "'M:\sap\sapdev2005\'
    CHANGING
      selected_folder = gd_dir.

  CALL METHOD cl_gui_cfw=>flush.

  concatenate gd_dir '' into p_dir.

************************************************************************
*AT SELECTION-SCREEN ON VALUE-REQUEST FOR I_FILE.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
  CALL FUNCTION 'WS_FILENAME_GET'
    EXPORTING
      def_filename     = p_file
      mask             = ',*.xls.'
      mode             = 'O'
      title            = 'Upload File'(078)
    IMPORTING
      filename         = p_file
    EXCEPTIONS
      inv_winsys       = 1
      no_batch         = 2
      selection_cancel = 3
      selection_error  = 4
      OTHERS           = 5.

************************************************************************
*AT SELECTION-SCREEN ON VALUE-REQUEST FOR I_FILE.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_find.
  CALL FUNCTION 'WS_FILENAME_GET'
    EXPORTING
      def_filename     = p_file
      mask             = ',*.xls.'
      mode             = 'O'
      title            = 'Upload File'(078)
    IMPORTING
      filename         = p_find
    EXCEPTIONS
      inv_winsys       = 1
      no_batch         = 2
      selection_cancel = 3
      selection_error  = 4
      OTHERS           = 5.

************************************************************************
*AT SELECTION-SCREEN ON VALUE-REQUEST FOR I_FILE.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_new.
  CALL FUNCTION 'WS_FILENAME_GET'
    EXPORTING
      def_filename     = p_file
      mask             = ',*.xls.'
      mode             = 'O'
      title            = 'Upload File'(078)
    IMPORTING
      filename         = p_new
    EXCEPTIONS
      inv_winsys       = 1
      no_batch         = 2
      selection_cancel = 3
      selection_error  = 4
      OTHERS           = 5.

**************************************************************************
*start-of-selection.
start-of-selection.


*UPLOAD FILES*******
  case 'X'.
    when p_seldir.
      perform get_directorys_in_directory using p_dir.
      loop at it_dirs into wa_dirs.
        perform get_files_in_directory using wa_dirs-filename.
      endloop.
    when p_selfil.
      wa_files-filename =  p_file.
      append wa_files to it_files.
  endcase.

  PERFORM get_input_files.

  loop at it_files into wa_files.
    it_datanew[] = it_datanewstor[].
    i_input = wa_files-filename.

    if not p_repfn is initial.
      gd_insert = i_input.
      translate gd_insert to upper case.
      replace ALL OCCURRENCES OF p_repfn in gd_insert with ' '.
      shift gd_insert LEFT DELETING LEADING space.
      loop at it_datanew into wa_datanew.
        translate gd_insert to LOWER CASE.
        replace ALL OCCURRENCES OF '' in wa_datanew-row with gd_insert.
        if sy-subrc eq 0.
          replace ALL OCCURRENCES OF '\' in wa_datanew-row with '/'.
          modify it_datanew from wa_datanew index sy-tabix.
        endif.
      endloop.
    endif.

    perform get_date_file.
    PERFORM process_files.
*DOWNLOAD FILE*******
    PERFORM download_result_file.
  endloop.


*&---------------------------------------------------------------------*
*&      Form  get_input_files
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM get_input_files .


  i_find = p_find.
  CALL FUNCTION 'GUI_UPLOAD'
    EXPORTING
      filename        = i_find
      filetype        = 'ASC'
    TABLES
      data_tab        = it_datafind  "ITBL_IN_RECORD[]
    EXCEPTIONS
      file_open_error = 1
      OTHERS          = 2.

  i_new = p_new.
  CALL FUNCTION 'GUI_UPLOAD'
    EXPORTING
      filename        = i_new
      filetype        = 'ASC'
    TABLES
      data_tab        = it_datanewstor  "ITBL_IN_RECORD[]
    EXCEPTIONS
      file_open_error = 1
      OTHERS          = 2.


ENDFORM.                    " get_input_files


*&---------------------------------------------------------------------*
*&      Form  download_result_file
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM download_result_file .

  CALL METHOD cl_gui_frontend_services=>GUI_DOWNLOAD
    EXPORTING
      filename                = i_input
    CHANGING
      data_tab                = it_datatab[]  "ITBL_IN_RECORD[]
    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
      NOT_SUPPORTED_BY_GUI    = 22
      ERROR_NO_GUI            = 23.
  if sy-subrc ne 0.

    write:/  'download failure for file-', i_input.
  endif.
*      CALL
*      FUNCTION
*      'GUI_DOWNLOAD'
*      file_open_error         = 1
*      OTHERS                  = 2.
ENDFORM.                    " download_result_file


*&---------------------------------------------------------------------*
*&      Form  process_files
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM process_files .
  data: ld_tabix like sy-tabix,
        ld_found type i,
        ld_end   type i.

  READ TABLE it_datafind into wa_datafind index 1.

  if not p_check is initial.
    loop at it_datatab into wa_datatab where row cs p_check.
    endloop.
    if sy-subrc eq 0.
      write:/ i_input COLOR COL_NEGATIVE, '-file contains restricted value'.
      exit.
    endif.
  endif.

  clear: gd_multi, gd_head, ld_found, ld_end.
  loop at it_datatab into wa_datatab.
    if wa_datatab-row cs p_start or
       ld_found ge 1 or
       p_start is initial.
      ld_found = 1.

      if wa_datatab-row cs p_end and
        not p_start is initial.
        ld_end = 1.
       exit.
      endif.

      if wa_datatab-row cs wa_datafind-row.
        if not wa_datatab-row cs p_not or
           p_not is INITIAL.
          case 'X'.
            when p_above.
              ld_tabix = sy-tabix.
              perform insert_new_lines using ld_tabix.
            when p_below.
              ld_tabix = sy-tabix + 1.
              perform insert_new_lines using ld_tabix.
            when p_repl.
              ld_tabix = sy-tabix.
              delete it_datatab index ld_tabix.
              perform insert_new_lines using ld_tabix.
            when p_after.

              ld_tabix = sy-tabix.
              read table it_datanew into wa_datanew index 1.
              CONCATENATE wa_datafind-row wa_datanew-row into gd_newtxt.

              perform modify_lines using ld_tabix.
            when p_before.

              ld_tabix = sy-tabix.
              read table it_datanew into wa_datanew index 1.
              CONCATENATE  wa_datanew-row wa_datafind-row into gd_newtxt.
              perform modify_lines using ld_tabix.
*          replace ALL OCCURRENCES OF wa_datafind-row in wa_datatab-row with gd_new.
*          modify it_datatab from wa_datatab index ld_tabix.
          endcase.
        else.
          if gd_head lt 1.
            write:/ i_input COLOR COL_POSITIVE.
            gd_head = 1.
          endif.
          write:/ wa_datatab-row COLOR COL_HEADING, '-line contains excluded value'.
        endif.
      endif.
    endif.
  endloop.
  if gd_multi le 0.
    write:/ i_input color COL_TOTAL, '-file not updated'.
    if ld_end ge 1.
      write: space, ' -end value found'.
    endif.
  endif.

ENDFORM.                    " process_files

*&---------------------------------------------------------------------*
*&      Form  get_files_in_directory
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM get_files_in_directory using lp_dir.
  data: ld_dir type string,
        it_storefiles type STANDARD TABLE OF FILE_INFO,
        wa_storefiles like line of it_storefiles.
  ld_dir = lp_dir.
  CALL METHOD cl_gui_frontend_services=>DIRECTORY_LIST_FILES
    EXPORTING
      DIRECTORY  = ld_dir
      files_only = 'X'
    CHANGING
      file_table = it_storefiles
      count      = gd_count
    EXCEPTIONS
      OTHERS     = 1.

  loop at it_storefiles into wa_storefiles.
    shift ld_dir right deleting TRAILING '/'.
    shift ld_dir right deleting TRAILING '\'.
    shift ld_dir left deleting leading space.
    concatenate ld_dir '\' wa_storefiles-FILENAME into wa_storefiles-FILENAME.

    if wa_storefiles-FILENAME cs '.htm' and
       wa_storefiles-filename cs '.HTM'.
      append wa_storefiles to it_files.
    endif.
  endloop.

ENDFORM.                    " get_files_in_directory


*&---------------------------------------------------------------------*
*&      Form  get_date_file
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM get_date_file .

  CALL FUNCTION 'GUI_UPLOAD'
    EXPORTING
      filename        = i_input
      filetype        = 'ASC'
    TABLES
      data_tab        = it_datatab  "ITBL_IN_RECORD[]
    EXCEPTIONS
      file_open_error = 1
      OTHERS          = 2.
ENDFORM.                    " get_date_file


*&---------------------------------------------------------------------*
*&      Form  insert_new_lines
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM insert_new_lines USING ld_tabix.
  insert LINES OF it_datanew into it_datatab index ld_tabix.
  gd_multi = gd_multi + 1.
  if gd_multi = 1.
    write:/ i_input COLOR COL_POSITIVE, '-File Updated OK'.
*    if p_multi is initial.
*      exit.
*    endif.
  else.
    write:/ i_input COLOR COL_NEGATIVE, '-duplicate updated'.
  endif.
ENDFORM.                    " insert_new_lines


*&---------------------------------------------------------------------*
*&      Form  modify_lines
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_LD_TABIX  text
*----------------------------------------------------------------------*
FORM modify_lines  USING    p_TABIX.

  gd_multi = gd_multi + 1.
  if gd_head lt 1.
    write:/ i_input COLOR COL_POSITIVE.
    gd_head = 1.
  endif.


  replace ALL OCCURRENCES OF wa_datafind-row in wa_datatab-row with gd_newtxt.
  if sy-subrc ne 0.
    translate  wa_datafind-row to UPPER CASE.
    replace ALL OCCURRENCES OF wa_datafind-row in wa_datatab-row with gd_newtxt.
    if sy-subrc ne 0.
      write:/ wa_datatab-row COLOR COL_NEGATIVE, '-String found but no changes made'.
    endif.
  endif.

  modify it_datatab from wa_datatab index p_tabix.

  write:/ wa_datatab-row.

ENDFORM.                    " modify_lines


*&---------------------------------------------------------------------*
*&      Form  get_directorys_in_directory
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM get_directorys_in_directory using lp_dir.
  data: ld_dir type string,
        it_storedirs type STANDARD TABLE OF FILE_INFO,
        wa_storedirs like line of it_storedirs.
  ld_dir = lp_dir.

  wa_storedirs-FILENAME = ld_dir.
  append wa_storedirs to it_dirs.

  CALL METHOD cl_gui_frontend_services=>DIRECTORY_LIST_FILES
    EXPORTING
      DIRECTORY  = ld_dir
      files_only = ' '
    CHANGING
      file_table = it_storedirs
      count      = gd_count
    EXCEPTIONS
      OTHERS     = 1.

  loop at it_storedirs into wa_storedirs where isdir = '1'.
    shift ld_dir right deleting TRAILING '/'.
    shift ld_dir right deleting TRAILING '\'.
    shift ld_dir left deleting leading space.
    concatenate ld_dir '\' wa_storedirs-FILENAME into wa_storedirs-FILENAME.
*    append wa_storedirs to it_dirs.
    perform get_directorys_in_directory using wa_storedirs-FILENAME.

*    modify it_storedirs from wa_storedirs index sy-tabix.
*    if not wa_files-FILENAME cs '.htm' and
*       not wa_files-filename cs '.HTM'.
*      DELETE it_storedirs index sy-tabix.
*    endif.
  endloop.

ENDFORM.                    " get_directorys_in_directory



comments powered by Disqus