Skip to Content
author's profile photo Former Member
Former Member

reading .csv file from application layer

hi folks,

i am new to this forum,

i have a small doubt.

i have a .csv file in my application server which is in a particular directory.if the directory is not present i should give an error message. once i find the directory i need to get the details of the .csv file into my internal table. shld i use the fm rzl_read_dir_local and later use open dataset in read mode?? if it is rite can some1 give me a sample code..

thanks in advance..!!

prasad

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

5 Answers

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Jan 08, 2007 at 04:27 AM

    hi,here is the program to download and Upload file from n to application server.

    hope it will help.

    REPORT ZGILL_AS message-id rp .

    tables: pa0001,pa0002.

    select-options s_pernr for pa0001-pernr no intervals MODIF ID XYZ.

    parameters: p_dwnld AS CHECKBOX ,

    p_upld AS CHECKBOX DEFAULT 'X'.

    parameters: P_DSNI(75) TYPE C MODIF ID ABG DEFAULT

    '/usr/local/sapdata/amit.dat' LOWER CASE.

    data: begin of itab occurs 0,

    pernr(8),

    sp1(1) value ',',

    werks(4),

    sp2(1) value ',',

    persg(1),

    sp3(1) value ',',

    persk(2),

    end of itab.

    data: s_eof(3).

    start-of-selection.

    if p_upld = 'X'.

    OPEN DATASET P_DSNI FOR OUTPUT IN LEGACY TEXT MODE.

    PERFORM FETCH_DATA.

    STOP.

    elseif p_dwnld = 'X'.

    OPEN DATASET P_DSNI FOR INPUT IN LEGACY TEXT MODE.

    IF SY-SUBRC NE 0.

    MESSAGE E016 WITH

    'Error opening seq. file, RC:' SY-SUBRC.

    EXIT.

    ENDIF.

    CLEAR S_EOF.

    DO.

    PERFORM FETCH_file.

    IF S_EOF EQ 'YES'. stop. ENDIF.

    ENDDO.

    endif.

    END-OF-SELECTION.

    if itab[] is not initial.

    perform print_file1 tables itab.

    else.

    write:/ 'No records exists'.

    endif.

    &----


    *& Form FETCH_DATA

    &----


    • text

    ----


    • --> p1 text

    • <-- p2 text

    ----


    FORM FETCH_DATA .

    SELECT * FROM PA0001 WHERE PERNR IN S_PERNR.

    MOVE-CORRESPONDING PA0001 TO ITAB.

    TRANSFER ITAB TO P_DSNI.

    APPEND ITAB.

    ENDSELECT.

    CLOSE DATASET P_DSNI.

    ENDFORM. " FETCH_DATA

    &----


    *& Form FETCH_file

    &----


    • text

    ----


    • --> p1 text

    • <-- p2 text

    ----


    FORM FETCH_file .

    READ DATASET P_DSNI INTO itab.

    append itab.

    clear itab.

    IF SY-SUBRC NE 0.

    S_EOF = 'YES'. EXIT.

    ENDIF.

    ENDFORM. " FETCH_file

    &----


    *& Form print_file1

    &----


    • text

    ----


    • -->P_ITAB text

    ----


    FORM print_file1 tables P_ITAB structure itab .

    write:/2 'EmpNo',

    14 'Personnel Area',

    34 'Emp Group',

    47 'Emp SubGroup'.

    skip 1.

    loop at p_itab.

    write:2 p_itab-pernr,

    14 p_itab-werks,

    34 p_itab-persg,

    47 p_itab-persk.

    skip 1.

    endloop.

    ENDFORM. " print_file1

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jan 08, 2007 at 04:28 AM

    Hi Prasad,

    Use the below FM to check valid directory or not ,based on that raise error message.

    CALL FUNCTION 'TMP_GUI_DIRECTORY_LIST_FILES'

    EXPORTING

    DIRECTORY = T_DIREC

    TABLES

    FILE_TABLE = FILE_TABLE

    DIR_TABLE = DIR_TABLE .

    IF DIR_TABLE[] IS INITIAL.

    CALL FUNCTION 'POPUP_TO_DISPLAY_TEXT'

    EXPORTING

    TEXTLINE1 = 'Please Enter Valid Directory'

    TEXTLINE2 = 'Ex: d:\temp'.

    MESSAGE e013(zmamin) WITH ''.

    end if.

    To extract tha data.........

    READ DATASET s_filename INTO I_TEMP.

    IF SY-SUBRC <> 0.

    EXIT.

    ELSE.

    move I_TEMP to I_FINALTAB.

    append I_FINALTAB.

    clear I_FINALTAB.

    ENDIF.

    ENDDO.

    CLOSE DATASET s_filename .

    clear v_flag.

    CALL FUNCTION 'WS_DOWNLOAD'

    EXPORTING

    CODEPAGE = 'IBM'

    FILENAME = T_PATH

    FILETYPE = 'ASC'

    TABLES

    DATA_TAB = I_FINALTAB.

    IF SY-SUBRC <> 0.

    MESSAGE e013(zmamin) WITH 'Unable to download the File'.

    ELSE.

    MESSAGE i013(zmamin) WITH 'File downloaded Successfully'.

    LEAVE.

    ENDIF.

    Pls. mark if useful

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jan 08, 2007 at 04:47 AM

    Hi Prasad,

    Use the below FM to check valid directory or not ,based on that raise error message.

    CALL FUNCTION 'TMP_GUI_DIRECTORY_LIST_FILES'

    EXPORTING

    DIRECTORY = T_DIREC

    TABLES

    FILE_TABLE = FILE_TABLE

    DIR_TABLE = DIR_TABLE .

    IF DIR_TABLE[] IS INITIAL.

    CALL FUNCTION 'POPUP_TO_DISPLAY_TEXT'

    EXPORTING

    TEXTLINE1 = 'Please Enter Valid Directory'

    TEXTLINE2 = 'Ex: d:\temp'.

    MESSAGE e013(zmamin) WITH ''.

    end if.

    To extract tha data.........

    READ DATASET s_filename INTO I_TEMP.

    IF SY-SUBRC <> 0.

    EXIT.

    ELSE.

    move I_TEMP to I_FINALTAB.

    append I_FINALTAB.

    clear I_FINALTAB.

    ENDIF.

    ENDDO.

    CLOSE DATASET s_filename .

    clear v_flag.

    CALL FUNCTION 'WS_DOWNLOAD'

    EXPORTING

    CODEPAGE = 'IBM'

    FILENAME = T_PATH

    FILETYPE = 'ASC'

    TABLES

    DATA_TAB = I_FINALTAB.

    IF SY-SUBRC <> 0.

    MESSAGE e013(zmamin) WITH 'Unable to download the File'.

    ELSE.

    MESSAGE i013(zmamin) WITH 'File downloaded Successfully'.

    LEAVE.

    ENDIF.

    Pls. mark if useful

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jan 08, 2007 at 05:25 AM

    Dear Prasad,

    Please go though the following lines of code:

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

    • D A T A D E C L A R A T I O N *

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

    TABLES: ANEP,

    BKPF.

    TYPES: BEGIN OF TY_TABDATA,

    MANDT LIKE SY-MANDT, " Client

    ZSLNUM LIKE ZSHIFTDEPN-ZSLNUM, " Serial Number

    ZASSET LIKE ZSHIFTDEPN-ZASSET, " Original asset that was transferred

    ZYEAR LIKE ZSHIFTDEPN-ZYEAR, " Fiscal Year

    ZPERIOD LIKE ZSHIFTDEPN-ZPERIOD, " Fiscal Period

    ZSHIFT1 LIKE ZSHIFTDEPN-ZSHIFT1, " Shift No. 1

    ZSHIFT2 LIKE ZSHIFTDEPN-ZSHIFT1, " Shift No. 2

    ZSHIFT3 LIKE ZSHIFTDEPN-ZSHIFT1, " Shift No. 3

    END OF TY_TABDATA.

    *----


    • Declaration of the Internal Table with Header Line comprising of the uploaded data.

    *----


    DATA: BEGIN OF IT_FILE_UPLOAD OCCURS 0.

    INCLUDE STRUCTURE ALSMEX_TABLINE. " Rows for Table with Excel Data

    DATA: END OF IT_FILE_UPLOAD.

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

    • S E L E C T I O N - S C R E E N *

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

    SELECTION-SCREEN: BEGIN OF BLOCK B1 WITH FRAME,

    BEGIN OF BLOCK B2 WITH FRAME.

    PARAMETERS: P_FNAME LIKE RLGRAP-FILENAME OBLIGATORY.

    SELECTION-SCREEN: END OF BLOCK B2,

    END OF BLOCK B1.

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

    • E V E N T : AT S E L E C T I O N - S C R E E N *

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

    AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FNAME.

    CALL FUNCTION 'KD_GET_FILENAME_ON_F4'

    EXPORTING

    • PROGRAM_NAME = SYST-REPID

    • DYNPRO_NUMBER = SYST-DYNNR

    • FIELD_NAME = ' '

    STATIC = 'X'

    • MASK = '.'

    CHANGING

    FILE_NAME = P_FNAME

    • EXCEPTIONS

    • MASK_TOO_LONG = 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.

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

    • E V E N T : S T A R T - O F - S E L E C T I O N *

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

    START-OF-SELECTION.

    • --------------------------------------

    • Upload Excel file into Internal Table.

    • --------------------------------------

    PERFORM UPLOAD_EXCEL_FILE.

    • -------------------------------------------------------

    • Organize the uploaded data into another Internal Table.

    • -------------------------------------------------------

    PERFORM ORGANIZE_UPLOADED_DATA.

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

    • E V E N T : E N D - O F - S E L E C T I O N *

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

    END-OF-SELECTION.

    &----


    *& Form UPLOAD_EXCEL_FILE

    &----


    • text

    ----


    • --> p1 text

    • <-- p2 text

    ----


    FORM UPLOAD_EXCEL_FILE .

    CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'

    EXPORTING

    FILENAME = P_FNAME

    I_BEGIN_COL = 1

    I_BEGIN_ROW = 3

    I_END_COL = 7

    I_END_ROW = 32000

    TABLES

    INTERN = IT_FILE_UPLOAD

    EXCEPTIONS

    INCONSISTENT_PARAMETERS = 1

    UPLOAD_OLE = 2

    OTHERS = 3

    .

    IF SY-SUBRC <> 0.

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

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

    ENDIF.

    ENDFORM. " UPLOAD_EXCEL_FILE

    &----


    *& Form ORGANIZE_UPLOADED_DATA

    &----


    • text

    ----


    • --> p1 text

    • <-- p2 text

    ----


    FORM ORGANIZE_UPLOADED_DATA .

    SORT IT_FILE_UPLOAD BY ROW

    COL.

    LOOP AT IT_FILE_UPLOAD.

    CASE IT_FILE_UPLOAD-COL.

    • ....................................................

    WHEN 1.

    WA_TABDATA-ZSLNUM = IT_FILE_UPLOAD-VALUE.

    WHEN 2.

    WA_TABDATA-ZASSET = IT_FILE_UPLOAD-VALUE.

    WHEN 3.

    WA_TABDATA-ZYEAR = IT_FILE_UPLOAD-VALUE.

    WHEN 4.

    WA_TABDATA-ZPERIOD = IT_FILE_UPLOAD-VALUE.

    WHEN 5.

    WA_TABDATA-ZSHIFT1 = IT_FILE_UPLOAD-VALUE.

    WHEN 6.

    WA_TABDATA-ZSHIFT2 = IT_FILE_UPLOAD-VALUE.

    WHEN 7.

    WA_TABDATA-ZSHIFT3 = IT_FILE_UPLOAD-VALUE.

    • ....................................................

    ENDCASE.

    AT END OF ROW.

    WA_TABDATA-MANDT = SY-MANDT.

    APPEND WA_TABDATA TO IT_TABDATA.

    CLEAR: WA_TABDATA.

    ENDAT.

    ENDLOOP.

    ENDFORM. " ORGANIZE_UPLOADED_DATA

    In the subroutine --> ORGANIZE_UPLOADED_DATA, data are organized as per the structure declared above.

    Regards,

    Abir

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

    • Don't forget to award points *

                                                                      Add a comment
                                                                      10|10000 characters needed characters exceeded

                                                                    • author's profile photo Former Member
                                                                      Former Member
                                                                      Posted on Jan 08, 2007 at 07:58 AM

                                                                      hi frnds, thanks for helping me out.

                                                                      but my requiremtn is to use only rzl_read_dir_local function module for validating the selection screen parameter(whcih is file directory path which is in application server), n later read the data from .csv file to internal table using open data set in read mode.

                                                                      can ne1 help me according to my requirement??

                                                                      Add a comment
                                                                      10|10000 characters needed characters exceeded

                                                                    Before answering

                                                                    You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
                                                                    You must be Logged in to submit an answer.

                                                                    Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.