ABAP AS文件目录动态创建

项目上需要将附件上传得到ABAP AS的文件系统上,文件需要按照上传日期存放在 YYYY\MM对应的目录下,所以我们需要动态的创建文件夹,以应对不同月份的所需的新文件夹。以下便是使用ABAP在 AS上创建文件夹的code:

class ZCL_FILE_SYSTEM_UTIL definition
  public
  final
  create public .

public section.

  class-methods CHECK_DIRECTORY_EXIST
    importing
      !IV_DIR type STRING
    returning
      value(RV_EXIST) type ABAP_BOOL .
  class-methods CREATE_DIRECTORY
    importing
      !IV_CREATE_DIR type STRING
    returning
      value(RV_SUCCESS) type ABAP_BOOL .
protected section.
private section.

  types:
    TY_SEPARATOR(1)  type c .

  class-data GC_SEPARATOR_SLASH type TY_SEPARATOR value '/' ##NO_TEXT.
  class-data GC_SEPARATOR_BACKSLASH type TY_SEPARATOR value '\' ##NO_TEXT.
ENDCLASS.



CLASS ZCL_FILE_SYSTEM_UTIL IMPLEMENTATION.


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZCL_FILE_SYSTEM_UTIL=>CHECK_DIRECTORY_EXIST
* +-------------------------------------------------------------------------------------------------+
* | [--->] IV_DIR                         TYPE        STRING
* | [<-()] RV_EXIST                       TYPE        ABAP_BOOL
* +--------------------------------------------------------------------------------------</SIGNATURE>
  METHOD check_directory_exist.
**********************************************************************
* 检查目录是否存在
**********************************************************************
    DATA:lv_dir      TYPE sxpgcolist-parameters,
         lv_status   TYPE extcmdexex-status,
         lv_exitcode TYPE extcmdexex-exitcode,
         lt_protocol TYPE  TABLE OF btcxpm.


    lv_dir = iv_dir.
* 调用SM69中定义的OS命令
    CALL FUNCTION 'SXPG_COMMAND_EXECUTE'
      EXPORTING
        commandname                   = 'ZLS'
        additional_parameters         = lv_dir
      IMPORTING
        status                        = lv_status
        exitcode                      = lv_exitcode
      TABLES
        exec_protocol                 = lt_protocol
      EXCEPTIONS
        no_permission                 = 1
        command_not_found             = 2
        parameters_too_long           = 3
        security_risk                 = 4
        wrong_check_call_interface    = 5
        program_start_error           = 6
        program_termination_error     = 7
        x_error                       = 8
        parameter_expected            = 9
        too_many_parameters           = 10
        illegal_command               = 11
        wrong_asynchronous_parameters = 12
        cant_enq_tbtco_entry          = 13
        jobcount_generation_error     = 14.

    IF sy-subrc EQ 0 AND lv_status EQ 'O' AND lv_exitcode EQ '0'.
      rv_exist = abap_true.
    ELSE.
      rv_exist = abap_false.
    ENDIF.

  ENDMETHOD.


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZCL_FILE_SYSTEM_UTIL=>CREATE_DIRECTORY
* +-------------------------------------------------------------------------------------------------+
* | [--->] IV_CREATE_DIR                  TYPE        STRING
* | [<-()] RV_SUCCESS                     TYPE        ABAP_BOOL
* +--------------------------------------------------------------------------------------</SIGNATURE>
  METHOD create_directory.
**********************************************************************
* 在AS上创建目录
**********************************************************************


    DATA:lv_dir      TYPE string,
         lv_para     TYPE sxpgcolist-parameters,
         lv_status   TYPE extcmdexex-status,
         lv_exitcode TYPE extcmdexex-exitcode,
         lt_protocol TYPE  TABLE OF btcxpm.

    DATA:lv_separator TYPE string." 目录分隔符

    DATA:lt_dir TYPE TABLE OF string.


    IF check_directory_exist( iv_dir = iv_create_dir ) EQ abap_false." 目录不存在

* 判断分隔符
      IF iv_create_dir CS gc_separator_slash.
        lv_separator = gc_separator_slash.
      ELSE.
        lv_separator = gc_separator_backslash.
      ENDIF.

* 逐级检查目录
      SPLIT iv_create_dir AT lv_separator INTO TABLE lt_dir.

      CLEAR lv_dir.
      LOOP AT  lt_dir INTO DATA(ls_dir).
        IF ls_dir eq ''.
          CONTINUE.
        ENDIF.

        lv_dir = |{ lv_dir }{ lv_separator }{ ls_dir }|.
* 检查目录是否存在
        IF  check_directory_exist( iv_dir = lv_dir ) EQ abap_false." 不存在,则创建

          lv_para = lv_dir.
** 调用SM69中定义的 OS 命令
          CALL FUNCTION 'SXPG_COMMAND_EXECUTE'
            EXPORTING
              commandname                   = 'ZMKDIR'
              additional_parameters         = lv_para
            IMPORTING
              status                        = lv_status
              exitcode                      = lv_exitcode
            TABLES
              exec_protocol                 = lt_protocol
            EXCEPTIONS
              no_permission                 = 1
              command_not_found             = 2
              parameters_too_long           = 3
              security_risk                 = 4
              wrong_check_call_interface    = 5
              program_start_error           = 6
              program_termination_error     = 7
              x_error                       = 8
              parameter_expected            = 9
              too_many_parameters           = 10
              illegal_command               = 11
              wrong_asynchronous_parameters = 12
              cant_enq_tbtco_entry          = 13
              jobcount_generation_error     = 14.
          IF  sy-subrc EQ 0 AND lv_status EQ 'O' AND lv_exitcode EQ '0'." 创建成功

          ELSE." 创建失败
            rv_success = abap_false.
            EXIT.
          ENDIF.
        ELSE." 存在,则继续检查下级目录

        ENDIF.

      ENDLOOP.

      IF sy-subrc EQ 0 AND lv_status EQ 'O' AND lv_exitcode EQ '0'.
        rv_success = abap_true.
      ELSE.
        rv_success = abap_false.
      ENDIF.

    ELSE." 目录存在,直接返回成功
      rv_success = abap_true.
    ENDIF.


  ENDMETHOD.

ENDCLASS.
相关推荐
斌将军5 天前
BP新增页签增强
abap
LilySesy7 天前
SAP+Internet主题HTML样式选择
前端·html·sap·abap·internet服务·its·扫码枪
SAP学习成长之路7 天前
如何在SM30生成的维护表中增加选择框 CheckBox
开发语言·数据库·sap·健康医疗·abap·代码规范
SAPABAP东芝10 天前
【SAP-ABAP】MIRO--界面增强增加数据导出导入按钮
sap·abap·fico
cyqweqe12310 天前
SAP采购发票税额总额非自动计算。TAX不写行号
abap
乐乐823413 天前
ABAP:SET CURSOR FIELD设置鼠标焦点
abap
冰魄禾20 天前
SAP ABAP开发学习——BAPI
运维·开发语言·笔记·运维开发·sap·abap
Lisson 31 个月前
The ABAP program lines are wider than the internal table.
abap
1314lay_10073 个月前
FUNCTION_ALV 下拉框的实现
abap
Fireworks_me3 个月前
SAP 有趣的‘bug‘ 选择屏幕输入框没了
abap