首先需要检查指定文件夹路径,若该路径不存在,则新建一文件夹。需要注意的是,对于多层路径的文件夹需要根据文件路径从主文件夹到子文件夹逐一创建。
用到的两个主要函数:
- WS_QUERY:判断文件夹是否存在
- GUI_CREATE_DIRECTORY:创建文件夹
REPORT zgml_001.
PARAMETERS p_file TYPE localfile.
DATA status TYPE c.
"保存文件夹路径
DATA: BEGIN OF folder OCCURS 0,
dir TYPE localfile,
END OF folder.
"保存文件夹路径识别符号
DATA: BEGIN OF sign OCCURS 0,
sig TYPE c,
END OF sign.
START-OF-SELECTION.
CLEAR:folder[],sign[],status.
"新增文件夹路径识别符号
sign-sig = '/'.
APPEND sign.
sign-sig = '\'.
APPEND sign.
"判断文件夹路径是否有效,无效则提示错误并退出
SEARCH p_file FOR sign.
IF sy-subrc <> 0.
MESSAGE e001(00) WITH '文件路径无效'.
EXIT.
ELSE.
"检查文件夹路径是否已经存在
PERFORM check_dir USING p_file CHANGING status.
IF status = 1.
MESSAGE i001(00) WITH '文件路径已存在'.
EXIT.
ENDIF.
ENDIF.
SPLIT p_file AT sign INTO TABLE folder.
READ TABLE folder INDEX 1.
p_file = folder-dir."盘符
"循环读取文件夹字符,检查改路径是否存在,若不存在则创建
LOOP AT folder.
IF sy-tabix > 1.
CONCATENATE p_file '\' folder-dir INTO p_file.
PERFORM check_dir USING p_file CHANGING status.
IF status = 0.
PERFORM create_dir USING p_file.
ENDIF.
ENDIF.
ENDLOOP.
*&---------------------------------------------------------------------*
*&调用函数检查路径是否存在
*&---------------------------------------------------------------------*
FORM check_dir USING filepath
CHANGING status.
CALL FUNCTION 'WS_QUERY'
EXPORTING
filename = filepath
query = 'DE'
IMPORTING
return = status "1:success,0:fail
EXCEPTIONS
inv_query = 1
no_batch = 2
frontend_error = 3
OTHERS = 4.
ENDFORM.
*&---------------------------------------------------------------------*
*&调用函数根据路径创建文件夹
*&---------------------------------------------------------------------*
FORM create_dir USING filepath.
CALL FUNCTION 'GUI_CREATE_DIRECTORY'
EXPORTING
dirname = filepath
EXCEPTIONS
failed = 1
OTHERS = 2.
WRITE:/ '文件夹创建成功,路径为:',filepath.
ENDFORM.
效果如下:
执行界面.png 结果界面.png
网友评论