Foro de programación ABAP

Código abierto => Funciones => Mensaje iniciado por: jesusrodriguez en 16 de Junio de 2009, 08:54:59 am

Título: Tratamiento de ficheros servidor
Publicado por: jesusrodriguez en 16 de Junio de 2009, 08:54:59 am
function ztratamiento_ficheros.
*"----------------------------------------------------------------------
*"*"Interfase local
*"  IMPORTING
*"     REFERENCE(P_RUTA1) TYPE  STRING
*"     REFERENCE(P_FICH) TYPE  STRING
*"     REFERENCE(P_RUTA2) TYPE  STRING OPTIONAL
*"     REFERENCE(P_ACCION) TYPE  CHAR01 DEFAULT 'M'
*"  EXCEPTIONS
*"      ACCION_INVALIDA
*"      PARAMETROS_INVALIDOS
*"      FICHERO_NO_BORRADO
*"      DESTINO_NO_ENCONTRADO
*"      FICHERO_NO_ENCONTRADO
*"      FALLO_TRATAMIENTO
*"----------------------------------------------------------------------

* Validación de la acción
  if p_accion ne 'M' and p_accion ne 'B' and p_accion ne 'C'.
    raise accion_invalida.
    exit.
  endif.

  if ( ( p_accion eq 'M' or p_accion eq 'C' ) and p_ruta2 is initial ) or
       p_ruta1 is initial or p_fich is initial.
    raise parametros_invalidos.
    exit.
  endif.

  case p_accion.

    when 'C'.

      clear d_retorno.
      perform copiar_fichero using p_ruta1
                                   p_fich
                                   p_ruta2
                                   d_retorno.
      if d_retorno is initial.
        write: /01(100) 'Fichero copiado'.
      endif.

    when 'M'.

      clear d_retorno.
      perform copiar_fichero using p_ruta1
                                   p_fich
                                   p_ruta2
                                   d_retorno.

      if d_retorno is initial.
        perform borrar_fichero using p_ruta1
                                     p_fich
                                     d_retorno.
      endif.

      if d_retorno is initial.
        write: /01(100) 'Fichero movido'.
      endif.

    when 'B'.

      perform borrar_fichero using p_ruta1
                                   p_fich
                                   d_retorno.

      if d_retorno is initial.
        write: /01(100) 'Fichero borrado'.
      endif.

  endcase.

endfunction.

*********************************
*  INCLUDE DE DECLARACION DE DATOS    *
*********************************
function-pool zficheros.                    "MESSAGE-ID ..


data: d_ruta type string,
      d_retorno(1) type c.


types: begin of t_fichero,
         campo(1000) type c,
       end of t_fichero.

data: i_fichero type t_fichero occurs 0.

data: w_fichero(1000) type c.


*************************
*  INCLUDE DE SUBRUTINASS    *
*************************
*----------------------------------------------------------------------*
***INCLUDE LZFICHEROSF01 .
*----------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*&      Form  COPIAR_FICHERO
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form copiar_fichero using p_ruta1 type any
                          p_fich  type any
                          p_ruta2 type any
                          p_retorno type any.

  data: l_longitud  type i,
        ld_ruta_ini type string,
        ld_ruta_fin type string.

  concatenate p_ruta1
              p_fich
        into ld_ruta_ini.

  concatenate p_ruta2
              p_fich
        into ld_ruta_fin.

  open dataset ld_ruta_ini for input
                         in text mode
                         encoding non-unicode.

  if sy-subrc <> 0.
    raise fichero_no_encontrado.
    p_retorno = 'X'.
    exit.
  else.

    do.
      clear w_fichero.
      read dataset ld_ruta_ini into w_fichero.
      if sy-subrc ne 0.
        p_retorno = 'X'.
        exit.
      else.
        append w_fichero to i_fichero.
      endif.
    enddo.

* Cerrar el fichero
    close dataset ld_ruta_ini.

    if sy-subrc ne 0.
      raise fallo_tratamiento.
      p_retorno = 'X'.
    else.
      clear p_retorno.
    endif.

  endif.

* Se copia el contenido de la tabla
  open dataset ld_ruta_fin for output
                           in text mode
                           encoding non-unicode.

  if sy-subrc <> 0.
    raise destino_no_encontrado.
    p_retorno = 'X'.
    exit.
  else.

    clear l_longitud.
    describe field w_fichero length l_longitud
                             in character mode.

    clear w_fichero.
    loop at i_fichero into w_fichero.

      transfer w_fichero to ld_ruta_fin
                       length l_longitud.

      clear w_fichero.
    endloop.

    close dataset ld_ruta_fin.

    if sy-subrc ne 0.
      raise fallo_tratamiento.
      p_retorno = 'X'.
    else.
      clear p_retorno.
    endif.

  endif.

  free i_fichero.

endform.                    " COPIAR_FICHERO
*&---------------------------------------------------------------------*
*&      Form  BORRAR_FICHERO
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_P_RUTA1  text
*      -->P_P_FICH  text
*----------------------------------------------------------------------*
form borrar_fichero  using    p_ruta1
                              p_fich
                              d_retorno.

  clear d_ruta.

  concatenate p_ruta1
              p_fich
         into d_ruta.

  delete dataset d_ruta.

  if sy-subrc ne 0.
    raise fichero_no_borrado.
    d_retorno = 'X'.
    exit.
  else.
    clear d_retorno.
  endif.

endform.                    " BORRAR_FICHERO