A number of times we want to copy or rename files in Application Server as per our requirements. However making changes to files in application server is not as easy as on Presentation Server.
I recently came across one such scenario while finding the answer saw a number of solutions. Some were to create a UNIX system command to move the files. Other was use of some function modules.
In the end Classes came to the rescue again and the below is the solution I used:
REPORT ZAPP_FILE. CALL METHOD cl_cts_language_file_io=>copy_files_local EXPORTING im_source_file = 'Test.txt' im_source_directory = '/olddir' im_target_file = 'TestNew.txt' im_target_directory = '/newdir' * im_overwrite_mode = SPACE * IMPORTING * ex_file_size = EXCEPTIONS open_input_file_failed = 1 open_output_file_failed = 2 write_block_failed = 3 read_block_failed = 4 close_output_file_failed = 5 others = 6. DELETE DATASET '/newdir/Test.txt'.
The above method call will create a new copy of the file Test.txt to TestNew.txt. The Source and Destination directory can be same or different.
If you want to rename the file then delete the previous file ‘Test.txt’ as in our example above.