本文主要是介绍ABAP AS文件目录动态创建,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
项目上需要将附件上传得到ABAP AS的文件系统上,文件需要按照上传日期存放在 YYYY\MM对应的目录下,所以我们需要动态的创建文件夹,以应对不同月份的所需的新文件夹。以下便是使用ABAP在 AS上创建文件夹的code:
class ZCL_FILE_SYSTEM_UTIL definitionpublicfinalcreate public .public section.class-methods CHECK_DIRECTORY_EXISTimporting!IV_DIR type STRINGreturningvalue(RV_EXIST) type ABAP_BOOL .class-methods CREATE_DIRECTORYimporting!IV_CREATE_DIR type STRINGreturningvalue(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'EXPORTINGcommandname = 'ZLS'additional_parameters = lv_dirIMPORTINGstatus = lv_statusexitcode = lv_exitcodeTABLESexec_protocol = lt_protocolEXCEPTIONSno_permission = 1command_not_found = 2parameters_too_long = 3security_risk = 4wrong_check_call_interface = 5program_start_error = 6program_termination_error = 7x_error = 8parameter_expected = 9too_many_parameters = 10illegal_command = 11wrong_asynchronous_parameters = 12cant_enq_tbtco_entry = 13jobcount_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'EXPORTINGcommandname = 'ZMKDIR'additional_parameters = lv_paraIMPORTINGstatus = lv_statusexitcode = lv_exitcodeTABLESexec_protocol = lt_protocolEXCEPTIONSno_permission = 1command_not_found = 2parameters_too_long = 3security_risk = 4wrong_check_call_interface = 5program_start_error = 6program_termination_error = 7x_error = 8parameter_expected = 9too_many_parameters = 10illegal_command = 11wrong_asynchronous_parameters = 12cant_enq_tbtco_entry = 13jobcount_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.
这篇关于ABAP AS文件目录动态创建的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!