Showing posts with label Commands. Show all posts
Showing posts with label Commands. Show all posts

Jun 30, 2026

How to Move Files between Directories on the Application Server?

SAP developers natively accomplish this using one of three standard methods: standard ABAP dataset commands, OS-level commands via SXPG_COMMAND_EXECUTE, or specialized functions like ARCHIVFILE_SERVER_TO_SERVER. 

Option 1: Native ABAP Statements (Recommended & Safest)
The cleanest, platform-independent approach is to copy the file content data line-by-line using datasets and then delete the original file. This completely eliminates operating system dependencies. 
abap
DATA: lv_line TYPE string.

" 1. Open the original source file
OPEN DATASET p_src_file FOR INPUT IN BINARY MODE.
IF sy-subrc = 0.
  
  " 2. Open the destination target file
  OPEN DATASET p_tgt_file FOR OUTPUT IN BINARY MODE.
  IF sy-subrc = 0.
    
    " 3. Read line by line and transfer data
    DO.
      READ DATASET p_src_file INTO lv_line.
      IF sy-subrc <> 0.
        EXIT.
      ENDIF.
      TRANSFER lv_line TO p_tgt_file.
    ENDDO.
    
    CLOSE DATASET p_tgt_file.
  ENDIF.
  
  CLOSE DATASET p_src_file.
  
  " 4. Delete the source file once successfully transferred
  IF sy-subrc = 0.
    DELETE DATASET p_src_file.
  ENDIF.
ENDIF.

Option 2: Function Module ARCHIVFILE_SERVER_TO_SERVER
You can utilize the standard function module ARCHIVFILE_SERVER_TO_SERVER to replicate a copy of the file on the server and then pair it with a DELETE DATASET statement. [1, 2]
  • Limitation: The source and target parameter variables have a maximum length of 70 characters. If your filepath or filename structure exceeds this limitation, you must utilize Option 1 instead. [1]
abap
CALL FUNCTION 'ARCHIVFILE_SERVER_TO_SERVER'
  EXPORTING
    sourcepath       = p_src_file
    targetpath       = p_tgt_file
  EXCEPTIONS
    error_file       = 1
    OTHERS           = 2.

IF sy-subrc = 0.
  DELETE DATASET p_src_file. " Removes original file to complete the move action
ENDIF.

Option 3: Function Module SXPG_COMMAND_EXECUTE
If you are moving exceptionally large files, executing an operating system level command (mv for Unix/Linux or move for Windows) is the fastest option since it changes file pointers rather than rewriting raw blocks. 
  1. Create a custom OS command (e.g., ZMV) mapped to your OS type in transaction SM69.
  2. Call the function module programmatically: 
abap
DATA: lv_parameters TYPE btcxpgpar,
      lv_status     TYPE btcxpgstat.

" Concatenate source and target paths as arguments for the terminal command
CONCATENATE p_src_file p_tgt_file INTO lv_parameters SEPARATED BY space.

CALL FUNCTION 'SXPG_COMMAND_EXECUTE'
  EXPORTING
    commandname         = 'ZMV'
    additional_parameters = lv_parameters
  IMPORTING
    status              = lv_status
  EXCEPTIONS
    no_permission       = 1
    command_not_found   = 2
    security_risk       = 3
    OTHERS              = 4.

If you would like to proceed with setting up one of these options, let me know:
  • What is the average size of the files you are moving?
  • Will your typical filepaths exceed 70 characters?
  • Which Operating System (Unix, Linux, Windows) is your SAP application server running on?

Aug 28, 2025

What is the Difference between Statement RETURN and EXIT in SAP ABAP?

Both RETURN and EXIT are control flow statements, but they serve different purposes and behave differently depending on the context. Here's a clear breakdown:

🔁 RETURN Statement

Purpose:
Used to exit a procedure—like a function module, method, or form—immediately and return control to the calling program.

Where it's used:

  • Function modules
  • Methods
  • Subroutines (FORM routines)

Behavior:

  • Ends the current procedure and returns to the caller.
  • No further code in the procedure is executed after RETURN.

Example:

FORM my_form. 

  IF sy-subrc <> 0. 

    RETURN. 

  ENDIF. " More code here won't run if RETURN is triggered 

ENDFORM.


🚪 EXIT Statement

Purpose:
Used to exit a loop or control structure like DO, WHILE, or LOOP.

Where it's used:

  • Inside loops (LOOP, DO, WHILE)
  • Not valid in procedures like methods or function modules (outside loops)

Behavior:

  • Immediately exits the current loop, but the rest of the surrounding code continues.
  • Does not exit the entire procedure.

Example:

LOOP AT it_table INTO wa_table. 

  IF wa_table-flag = 'X'. 

    EXIT. " Exits the LOOP, not the FORM 

  ENDIF. 

ENDLOOP.


🧠 Summary Table




Let me know if you want to see how CHECK fits into this trio—it’s another control statement that behaves a bit differently again.

May 13, 2016

ABAP Write Command


The write command used to be the bread and butter of SAP report writing where a user wanted to view the data on screen. This is not used as much for report writing as easier more advanced techniques are available such as the advance list viewer (ALV). This does not mean this is no longer used but its use is more for batch programs so that message appear in the output log, reports that only need a simple message output and other output uses where a report consisting of columns of data is not appropriate. 

Below is an ABAP code extract that demonstrates and explains the basic functionally of the write command.

  write:/10(45) 'Total No of Employees entered:', gd_records,  "/10 indents 10 chars
        /10(45) 'Number of Employees processed successfully:', "(45) sets field lenth
                gd_success. "displays variable
  NEW-LINE.  "moves to a new line

  describe table it_error lines gd_lines. "gets number of records in a table
  check gd_lines gt 0.  "check there are some error records

  skip 2. "skips 2 lines
  write:/10 'Unsuccessful Employee records'.  "(10) makes field take up 10 chars
  write:/10 sy-uline(67).   "sy-uline(67) display a line 67 chars long

  write:/10  sy-vline,   "sy-vline creates a vertical line
        (10) 'Employee' COLOR COL_HEADING, sy-vline, "COLOR changes background colour
        (50) 'Description'  COLOR COL_HEADING, sy-vline.

  write:/10 sy-uline(67).     "display a line 67 chars long

  loop at it_error into wa_error.  "loops at err table
    write:/10  sy-vline,      "sy-vline creates a vertical line
          (10) wa_error-pernr, sy-vline,  "(10) makes field take up 10 chars
          (50) wa_error-text, sy-vline.
  endloop.

  write:/10 sy-uline(67). "display a line 67 chars long