Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

File Handling

Former Member
0 Kudos

Hi Experts..

Can any body tell me how to use WS_QUERY and WS_FILENAME_GET with some example code.

Thanx in Advance.

5 REPLIES 5

Former Member
0 Kudos

1.for ws_query

Internal table to store export data

DATA: begin of it_excelfile occurs 0,

row(500) type c,

end of it_excelfile.

DATA: rc TYPE sy-ucomm,

ld_answer TYPE c.

CALL FUNCTION 'WS_QUERY'

EXPORTING

query = 'FE' "File Exist?

filename = p_file

IMPORTING

return = rc.

IF rc NE 0. "If File alread exists

CALL FUNCTION 'POPUP_TO_CONFIRM'

EXPORTING

  • TITLEBAR = ' '

  • DIAGNOSE_OBJECT = ' '

text_question = 'File Already exists!!'

text_button_1 = 'Replace'

  • ICON_BUTTON_1 = ' '

text_button_2 = 'New name'

  • ICON_BUTTON_2 = ' '

  • DEFAULT_BUTTON = '1'

  • DISPLAY_CANCEL_BUTTON = 'X'

  • USERDEFINED_F1_HELP = ' '

  • START_COLUMN = 25

  • START_ROW = 6

  • POPUP_TYPE =

IMPORTING

answer = ld_answer

  • TABLES

  • PARAMETER =

EXCEPTIONS

text_not_found = 1

OTHERS = 2.

  • Option 1: Overwrite

*********************

IF ld_answer EQ '1'.

CALL FUNCTION 'GUI_DOWNLOAD'

EXPORTING

  • BIN_FILESIZE =

filename = p_file "File Name

filetype = 'ASC'

  • IMPORTING

  • FILELENGTH =

TABLES

data_tab = it_excelfile "Data table

EXCEPTIONS

file_write_error = 1

no_batch = 2

gui_refuse_filetransfer = 3

invalid_type = 4

OTHERS = 5.

IF sy-subrc <> 0.

MESSAGE i003(zp) WITH

'There was an error during Excel file creation'(200).

exit. "Causes short dump if removed and excel document was open

ENDIF.

  • Option 2: New name.

*********************

ELSEIF ld_answer EQ '2'.

CALL FUNCTION 'DOWNLOAD'

EXPORTING

filename = p_file "File name

filetype = 'ASC' "File type

  • col_select = 'X' "COL_SELECT

  • col_selectmask = 'XXXXXXXXXXXXXXXXXXXXXXXXXX'

  • "COL_SELECTMASK

filetype_no_show = 'X' "Show file type selection?

  • IMPORTING

  • act_filename = filename_dat

TABLES

data_tab = it_excelfile "Data table

  • fieldnames =

EXCEPTIONS

file_open_error = 01

file_write_error = 02

invalid_filesize = 03

invalid_table_width = 04

invalid_type = 05

no_batch = 06

unknown_error = 07.

ENDIF.

ELSE. "File does not alread exist.

CALL FUNCTION 'GUI_DOWNLOAD'

EXPORTING

  • BIN_FILESIZE =

filename = p_file "File name

filetype = 'ASC' "File type

  • IMPORTING

  • FILELENGTH =

TABLES

data_tab = it_excelfile "Data table

EXCEPTIONS

file_write_error = 1

no_batch = 2

gui_refuse_filetransfer = 3

invalid_type = 4

OTHERS = 5.

IF sy-subrc <> 0.

MESSAGE i003(zp) WITH

'There was an error during Excel file creation'(200).

exit. "Causes short dump if removed and excel document was open

ENDIF.

ENDIF.

2. for ws_filename_get..

REPORT ZEXCEL LINE-SIZE 170 LINE-COUNT 58

NO STANDARD PAGE HEADING.

DATA: BEGIN OF TBXLS OCCURS 5,

LINE LIKE SY-TABIX,

COLN TYPE I,

STRING(1024) TYPE C,

END OF TBXLS,

BEGIN OF TABXLS OCCURS 5,

LINEA TYPE I,

CODIGO(10) TYPE C,

NUMLINEA TYPE I,

TEXTO(80),

END OF TABXLS.

DATA: P_NCOLN TYPE I.

  • Ole objects declaration

DATA: H_APPL LIKE OBJ_RECORD,

H_WORK LIKE OBJ_RECORD,

H_CELL LIKE OBJ_RECORD.

INCLUDE OLE2INCL.

INCLUDE DOCSINCL.

SELECTION-SCREEN BEGIN OF BLOCK B0 WITH FRAME.

PARAMETERS: P_CAMI LIKE RLGRAP-FILENAME. "Archivo Excel

PARAMETERS: P_NLINE LIKE SY-INDEX. "Numero aproximado de lineas

SELECTION-SCREEN END OF BLOCK B0.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_CAMI.

CALL FUNCTION 'WS_FILENAME_GET'

EXPORTING

DEF_FILENAME = ' '

DEF_PATH = P_CAMI

MASK = ',*.xls.'

MODE = 'O'

TITLE = 'Archivo a importar'

IMPORTING

FILENAME = P_CAMI

EXCEPTIONS

INV_WINSYS = 01

NO_BATCH = 02

SELECTION_CANCEL = 03

SELECTION_ERROR = 04.

INITIALIZATION.

P_CAMI = 'C:PLANTILLAS.XLS'.

IF P_NLINE IS INITIAL.

P_NLINE = 1000.

ENDIF.

  • Numero de columnas

P_NCOLN = 4.

START-OF-SELECTION.

PERFORM PROCESSA_PLANILHA.

PERFORM IMPRIMIR_PLANILHA.

IF SY-SUBRC = 0.

ENDIF.

  • Criar remessas de Exporta‡Æo.

END-OF-SELECTION.

*&----


*

*& Form PROCESSA_PLANILHA

*&----


*

FORM PROCESSA_PLANILHA.

  • Start Excel

IF H_APPL-HEADER = SPACE OR H_APPL-HANDLE = -1.

CREATE OBJECT H_APPL 'EXCEL.APPLICATION'.

IF SY-SUBRC NE 0. MESSAGE I002(SY) WITH SY-MSGLI. ENDIF.

SET PROPERTY OF H_APPL 'VISIBLE' = 0.

ENDIF.

  • Open file

CALL METHOD OF H_APPL 'WORKBOOKS' = H_WORK.

CALL METHOD OF H_WORK 'OPEN' EXPORTING #1 = P_CAMI.

  • Ler dados da tabela Excel.

PERFORM CAPTURAR_DADOS.

  • Release Excel

CALL METHOD OF H_APPL 'QUIT'.

FREE OBJECT H_APPL.

H_APPL-HANDLE = -1.

  • Se a primeira linha for comentarios (nome dos campos)

  • DELETE tbxls WHERE ( string = space ) OR

  • ( line = 1 ).

SORT TBXLS BY LINE COLN.

REFRESH TABXLS.

CLEAR TABXLS.

LOOP AT TBXLS.

AT NEW LINE.

CLEAR TABXLS.

ENDAT.

IF ( TBXLS-COLN = 1 ).

MOVE TBXLS-STRING TO TABXLS-LINEA.

ELSEIF ( TBXLS-COLN = 2 ).

MOVE TBXLS-STRING TO TABXLS-CODIGO.

ELSEIF ( TBXLS-COLN = 3 ).

MOVE TBXLS-STRING TO TABXLS-NUMLINEA.

ELSEIF ( TBXLS-COLN = 4 ).

MOVE TBXLS-STRING TO TABXLS-TEXTO.

ENDIF.

AT END OF LINE.

APPEND TABXLS.

ENDAT.

ENDLOOP.

ENDFORM. " PROCESSA_PLANILHA

*----


*

  • FORM CAPTURAR_DADOS *

*----


*

  • ........ *

*----


*

FORM CAPTURAR_DADOS.

DATA: EXCEL_LINE LIKE SY-INDEX,

EXCEL_COLN LIKE SY-INDEX,

CELL_VALUE(132) TYPE C.

DO P_NLINE TIMES.

EXCEL_LINE = SY-INDEX.

  • Display indicator

DO P_NCOLN TIMES.

EXCEL_COLN = SY-INDEX.

  • Get cell value data

CALL METHOD OF H_APPL 'CELLS' = H_CELL

EXPORTING #1 = EXCEL_LINE

#2 = EXCEL_COLN.

GET PROPERTY OF H_CELL 'VALUE' = CELL_VALUE.

CLEAR: TBXLS.

TBXLS-LINE = EXCEL_LINE.

TBXLS-COLN = EXCEL_COLN.

TBXLS-STRING = CELL_VALUE.

APPEND TBXLS.

ENDDO.

ENDDO.

ENDFORM. " Capturar_dados

*&----


*

*& Form IMPRIMIR_PLANILHA

*&----


*

FORM IMPRIMIR_PLANILHA.

LOOP AT TABXLS.

WRITE: / TABXLS-LINEA, TABXLS-CODIGO, TABXLS-NUMLINEA, TABXLS-TEXTO.

ENDLOOP.

ENDFORM.

reward points if helpful

Former Member
0 Kudos

You can find example code at : http://www.sapgenie.com/abap/code/abap1.htm

tables:
       SSCRFIELDS. "

   data:
       IT(5000) OCCURS 100 WITH HEADER LINE.

   *-parameters----------------------------------------------------------*
   selection-screen:
       begin of block b1 with frame title text-004.
   parameters:
       sfn(128) obligatory lower case memory id z0s   " server file name
                default '/usr/sap/tmp/?',
       pfn(128) obligatory lower case memory id z0p   " pc file name
                default 'c:temptemp.txt',
       efn(128) obligatory lower case                 " editor file name
                DEFAULT 'notepad.exe'.
   selection-screen:
       skip,
       PUSHBUTTON /05(23) TEXT-001 USER-COMMAND ZGET, "Serv -> PC
       PUSHBUTTON  30(23) TEXT-002 USER-COMMAND ZPUT, "PC -> Serv
       PUSHBUTTON  55(23) TEXT-003 USER-COMMAND ZPAD, "Ver fichero
       end of block b1.
*-mainline------------------------------------------------------------*

*-events--------------------------------------------------------------*
   at selection-screen.
       case sscrfields-ucomm.
       when 'ZGET'.  perform getfrsrv.
       when 'ZPUT'.  perform puttosrv.
       when 'ZPAD'.  perform editfile.
       endcase.

   AT SELECTION-SCREEN ON VALUE-REQUEST FOR EFN.
     CALL FUNCTION 'WS_FILENAME_GET'
          EXPORTING
               DEF_FILENAME     = EFN
               DEF_PATH         = '/'
               MASK             = ',*.*,*.*.'
               MODE             = 'O'
               TITLE            = 'Get filename'
          IMPORTING
               FILENAME         = EFN
*              rc               =
          EXCEPTIONS
               INV_WINSYS       = 01
               NO_BATCH         = 02
               SELECTION_CANCEL = 03
               SELECTION_ERROR  = 04.

*-forms---------------------------------------------------------------*
   form editfile.
       data:
           exists value '0'.
       perform fileexists
           changing exists.
       if exists = '0'.
           refresh it.
           perform download.
           endif.
       perform runnotepad.
       endform.

*---------------------------------------------------------------------*
   form fileexists
       changing exists.
       call function 'WS_QUERY'
            exporting
                 filename       = pfn
                 query          = 'FE'     " file exists?
            importing
                 return         = exists
            exceptions
                 inv_query      = 1
                 no_batch       = 2
                 frontend_error = 3
                 others         = 4.
       endform.

*---------------------------------------------------------------------*
   form getfrsrv.
       refresh it.
       perform dsopen_input_text
           using sfn.
       perform xfer2it.
       perform download.
       perform runnotepad.
       endform.

*-forms---------------------------------------------------------------*
  form puttosrv.
       refresh it.
       perform upload.
       perform dsopen_output_text
          using sfn.
       perform xferfromit
           using sfn.
       endform.

*---------------------------------------------------------------------*
   form xferfromit
       using dsn.
       loop at it.
           perform dswrite
               using dsn
                     it.
           endloop.
       endform.

*---------------------------------------------------------------------*
   form xfer2it.
       while sy-subrc = 0.
           perform dsread
               using    sfn
               changing it.
           if sy-subrc <> 0.
               exit.
               endif.
           append it.
           endwhile.
       perform dsclose
           using sfn.
       endform.

*---------------------------------------------------------------------*
   form download.
       call function 'WS_DOWNLOAD'
            exporting
                 filename            = pfn
            tables
                 data_tab            = it
            exceptions
                 file_open_error     = 1
                 file_write_error    = 2
                 invalid_filesize    = 3
                 invalid_table_width = 4
                 invalid_type        = 5
                 no_batch            = 6
                 unknown_error       = 7
                 others              = 8.
      perform checkrc
           using 'WS_DOWNLOAD'.
       endform.

*---------------------------------------------------------------------*
   form runnotepad.
       data pgm(128).
       concatenate
           efn pfn into pgm
           separated by space.
       call function 'WS_EXECUTE'
            exporting
                 program        = pgm
            exceptions
                 frontend_error = 1
                 no_batch       = 2
                 prog_not_found = 3
                 illegal_option = 4
                 others         = 5.
       perform checkrc
           using 'WS_EXECUTE'.
       endform.

*---------------------------------------------------------------------*
   form upload.
       call function 'WS_UPLOAD'
            exporting
                 filename            = pfn
           tables
                 data_tab            = it
            exceptions
                 conversion_error    = 1
                 file_open_error     = 2
                 file_read_error     = 3
                 invalid_table_width = 4
                 invalid_type        = 5
                 no_batch            = 6
                 unknown_error       = 7
                 others              = 8.
       perform checkrc
           using 'WS_UPLOAD'.
       endform.

*---------------------------------------------------------------------*
   form writetoscrn.
       window starting at 5  3
              ending   at 90 15.
       loop at it.
           write it.
           endloop.
       endform.

*---------------------------------------------------------------------*
   form checkrc using n.
       if sy-subrc <> 0.
           write: / 'rc=', sy-subrc, 'from', n.
           stop.
           endif.
       endform.

*-macro-for-form-definition-------------------------------------------*
  define dsform_open.
       form dsopen_&1_&2 using
               dsn  type c.
           data msg(80).
           open dataset dsn for &1 in &2 mode message msg.
           if sy-subrc <> 0.
               message e001(zk) with 'Error opening' dsn msg.
               endif.
           endform.
       end-of-definition.

*---------------------------------------------------------------------*
  dsform_open input  text.
   dsform_open input  binary.
   dsform_open output text.
   dsform_open output binary.
*--------------------------------------------------------------------
   form dsread
       using    dsn type c
       changing rec.

       statics: rctr type i.

       read dataset dsn into rec.
       case sy-subrc.
       when 0.
           add 1 to rctr.
       when 4.
           message s001(zk) with rctr 'records read from' dsn.
       when others.
           message e001(zk) with 'Error reading' dsn 'rc=' sy-subrc.
           endcase.
       endform.

*--------------------------------------------------------------------
   form dswrite
       using    dsn type c
                rec.

    statics: rctr type i.

      transfer rec to dsn.
       case sy-subrc.
       when 0.
           add 1 to rctr.
       when others.
           message e001(zk) with 'Error writing to' dsn 'rc=' sy-subrc.
           endcase.
       endform.

*--------------------------------------------------------------------
   form dsclose
       using dsn type c.
       close dataset dsn.
       if sy-subrc <> 0.
           message e001(zk) with 'Error closing' dsn 'rc=' sy-subrc.
           endif.
       endform.

Former Member
0 Kudos

HI

here is a sample program for ws_query

/**************************************************************/

/ */

/ Check users workstation is running WINDOWS, */

/ WINDOWS 95, or WINDOWS NT. OS/2 no good for */

/ this application. */

/ */

/**************************************************************/

CALL FUNCTION 'WS_QUERY'

EXPORTING

QUERY = 'WS'

IMPORTING

RETURN = WINSYS.

IF WINSYS(2) NE 'WN'. "Win 3.X no good either

WRITE: / 'Windows NT or Windows 95/98 is required'.

EXIT.

ENDIF.

FORM get_file_name USING x_file_field_name

CHANGING y_file_name_with_path.

DATA : lv_file_path TYPE rlgrap-filename,

lv_file_name TYPE rlgrap-filename.

PERFORM read_screen_values USING x_file_field_name

CHANGING y_file_name_with_path.

PERFORM split_path USING y_file_name_with_path

CHANGING lv_file_path

lv_file_name.

CALL FUNCTION 'WS_FILENAME_GET'

EXPORTING

def_filename = lv_file_name

def_path = lv_file_path

mask = ',.,..'

mode = 'O'

title = 'Directory'

IMPORTING

filename = p_infile

EXCEPTIONS

OTHERS = 1.

ENDFORM. " get_file_name

Former Member
0 Kudos

hi,

below is the code for ws_filename_get.

CALL FUNCTION 'WS_FILENAME_GET'

EXPORTING

DEF_PATH = P_FILE

MASK = ',*.txt.'

MODE = ' '

IMPORTING

FILENAME = F_NAME

EXCEPTIONS

INV_WINSYS = 1

NO_BATCH = 2

SELECTION_CANCEL = 3

SELECTION_ERROR = 4.

<b>instead of ws_filename_get u can use below functionmodule because ws_filename_get is obsolete in current version.</b>

CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_OPEN_DIALOG

EXPORTING

DEFAULT_EXTENSION =

DEFAULT_FILENAME = 'c:temp.txt'

FILE_FILTER = FILTER01

INITIAL_DIRECTORY = DIRECTORY

CHANGING

FILE_TABLE = ITAB

RC = RC01

USER_ACTION = ACTION01

  • FILE_ENCODING =

EXCEPTIONS

FILE_OPEN_DIALOG_FAILED = 1

CNTL_ERROR = 2

ERROR_NO_GUI = 3

NOT_SUPPORTED_BY_GUI = 4

others = 5.

IF SY-SUBRC <> 0.

  • MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

  • WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

below is for ws_query.

CALL FUNCTION 'WS_QUERY'

EXPORTING

FILENAME = filename

QUERY = 'FE'

IMPORTING

RETURN = RC

EXCEPTIONS

INV_QUERY = 1

NO_BATCH = 2

FRONTEND_ERROR = 3

OTHERS = 4.

<b>instead of ws_query u can use below function module because ws_query is obsolete in current version</b>

CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_EXIST

EXPORTING

FILE = filename

RECEIVING

RESULT = W_RESULT

EXCEPTIONS

CNTL_ERROR = 1

ERROR_NO_GUI = 2

WRONG_PARAMETER = 3

NOT_SUPPORTED_BY_GUI = 4

others = 5.

IF SY-SUBRC <> 0.

  • MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

  • WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

Regards,

sunil.

Former Member
0 Kudos

Hi,

Go through the code extract below. Hope this will be helpful.

DATA: V_FILENAME01 TYPE RLGRAP-FILENAME,

V_RC TYPE I,

V_TEXT_QUESTION TYPE STRING,

V_TEXT_QUESTION1 TYPE STRING,

V_ANSWER TYPE STRING.

CALL FUNCTION 'WS_FILENAME_GET'

EXPORTING

  • DEF_FILENAME = ' '

DEF_PATH = 'C:\'

  • MASK = ' '

  • MODE = ' '

  • TITLE = ' '

IMPORTING

FILENAME = V_FILENAME01

RC = V_RC

EXCEPTIONS

INV_WINSYS = 1

NO_BATCH = 2

SELECTION_CANCEL = 3

SELECTION_ERROR = 4

OTHERS = 5.

IF SY-SUBRC <> 0.

  • MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

  • WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ELSE.

CALL FUNCTION 'WS_QUERY'

EXPORTING

  • ENVIRONMENT =

FILENAME = V_FILENAME01

QUERY = 'FE'

  • WINID =

  • IMPORTING

  • RETURN =

EXCEPTIONS

INV_QUERY = 1

NO_BATCH = 2

FRONTEND_ERROR = 3

OTHERS = 4.

IF SY-SUBRC <> 0.

  • MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

  • WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ELSE.

CONCATENATE 'ARE YOU SURE TO DELETE FILE ' V_FILENAME01 ' ?' INTO

V_TEXT_QUESTION1.

CALL FUNCTION 'POPUP_TO_CONFIRM'

EXPORTING

  • TITLEBAR = ' '

  • DIAGNOSE_OBJECT = ' '

TEXT_QUESTION = V_TEXT_QUESTION1

  • TEXT_BUTTON_1 = 'Ja'(001)

  • ICON_BUTTON_1 = ' '

  • TEXT_BUTTON_2 = 'Nein'(002)

  • ICON_BUTTON_2 = ' '

DEFAULT_BUTTON = '1'

DISPLAY_CANCEL_BUTTON = 'X'

  • USERDEFINED_F1_HELP = ' '

  • START_COLUMN = 25

  • START_ROW = 6

  • POPUP_TYPE =

  • IV_QUICKINFO_BUTTON_1 = ' '

  • IV_QUICKINFO_BUTTON_2 = ' '

IMPORTING

ANSWER = V_ANSWER

  • TABLES

  • PARAMETER =

EXCEPTIONS

TEXT_NOT_FOUND = 1

OTHERS = 2.

IF SY-SUBRC <> 0.

  • MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

  • WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

IF V_ANSWER = '1'.

CALL FUNCTION 'WS_FILE_DELETE'

EXPORTING

FILE = V_FILENAME01

IMPORTING

RETURN = V_RC.

IF V_RC = '0'.

WRITE: 'FILE DELETED SUCCESSFULLY'.

ENDIF.

ENDIF.

ENDIF.

ENDIF.

Regards,

Vaitheeswaran.