‎2007 Sep 12 9:07 AM
Can any one provide me the code for dataset .........
I want to read a file on application server.......before that i want to chk if it exits or not...........if found, i need to validate the name of the file........?
Plz provide me the code...............
‎2007 Sep 12 9:11 AM
Hi Ravindar,
Open Dataset is used to read or write on to application server ... other than that i am not sure that there exists any way to do the same ... here is a short description for that
FILE HANDLING IN SAP
Introduction
Files on application server are sequential files.
Files on presentation server / workstation are local files.
A sequential file is also called a dataset.
Handling of Sequential file
Three steps are involved in sequential file handling
OPEN
PROCESS
CLOSE
Here processing of file can be READING a file or WRITING on to a file.
OPEN FILE
Before data can be processed, a file needs to be opened.
After processing file is closed.
Syntax:
OPEN DATASET <file name> FOR {OUTPUT/INPUT/APPENDING}
IN {TEXT/BINARY} MODE
This statement returns SY_SUBRC as 0 for successful opening of file or 8, if unsuccessful.
OUTPUT: Opens the file for writing. If the dataset already exists, this will place the cursor at the start of the dataset,
the old contents get deleted at the end of the program or when the CLOSE DATASET is encountered.
INPUT: Opens a file for READ and places the cursor at the beginning of the file.
FOR APPENDING: Opens the file for writing and places the cursor at the end of file. If the file does not exist, it is generated.
BINARY MODE: The READ or TRANSFER will be character wise. Each time n characters are READ or transferred.
The next READ or TRANSFER will start from the next character position and not on the next line.
IN TEXT MODE: The READ or TRANSFER will start at the beginning of a new line each time.
If for READ, the destination is shorter than the source, it gets truncated.
If destination is longer, then it is padded with spaces.
Defaults: If nothing is mentioned, then defaults are FOR INPUT and in BINARY MODE.
PROCESS FILE:
Processing a file involves READing the file or Writing on to file TRANSFER.
TRANSFER Statement
Syntax:
TRANSFER <field> TO <file name>.
<Field> can also be a field string / work area / DDIC structure.
Each transfer statement writes a statement to the dataset. In binary mode, it writes the length of the field to the dataset.
In text mode, it writes one line to the dataset.
If the file is not already open, TRANSFER tries to OPEN file FOR OUTPUT (IN BINARY MODE) or using the last OPEN DATASET statement for this file.
IF FILE HANDLING, TRANSFER IS THE ONLY STATEMENT WHICH DOES NOT RETURN SY-SUBRC
READ Statement
Syntax:
READ DATASET <file name> INTO <field>.
<Field> can also be a field string / work area / DDIC structure.
Each READ will get one record from the dataset.
In binary mode it reads the length of the field and in text mode it reads each line.
CLOSE FILE:
The program will close all sequential files, which are open at the end of the program.
However, it is a good programming practice to explicitly close all the datasets that were opened.
Syntax:
CLOSE DATASET <file name>.
SY-SUBRC will be set to 0 or 8 depending on whether the CLOSE is successful or not.
DELETE FILE:
A dataset can be deleted.
Syntax:
DELETE DATASET <file name>.
SY-SUBRC will be set to 0 or 8 depending on whether the DELETE is successful or not.
Pseudo logic for processing the sequential files:
For reading:
Open dataset for input in a particular mode.
Start DO loop.
Read dataset into a field.
If READ is not successful.
Exit the loop.
Endif.
Do relevant processing for that record.
End the do loop.
Close the dataset.
For writing:
Open dataset for output / Appending in a particular mode.
Populate the field that is to be transferred.
TRANSFER the filed to a dataset.
Close the dataset.
Thanks,
Reward If Helpful.
‎2007 Sep 12 9:14 AM
OPEN DATASET P_FILE FOR INPUT IN TEXT MODE ENCODING UTF-8.
IF SY-SUBRC = 0.
DO.
READ DATASET P_FILE INTO WA_ITAB.
IF SY-SUBRC = 0.
SPLIT WA_ITAB AT '#' INTO ITAB-WERKS
ITAB-MATNR
ITAB-EKGRP
ITAB-LIFNR
ITAB-EVERS
L_MENGE.
ITAB-MENGE = L_MENGE.
APPEND ITAB.
CLEAR ITAB.
ELSE.
EXIT.
ENDIF.
ENDDO.
ENDIF.
CLOSE DATASET P_FILE.
Also check the answered thread: -
Reward points for helpful answers.
Regards,
hari krsihna
‎2007 Sep 12 9:15 AM
Hi Ravinder...
This code can help u...
<b>How to read the Data from Tab delimited files. from APPLICATION SERVER</b>
Example:
DATA: V_RECORD(200).
OPEN DATASET P_FILE FOR INPUT IN TEXT MODE ENCODING DEFAULT.
IF SY-SUBRC <> 0.
EXIT.
ENDIF.
DO.
READ DATASET P_FILE INTO V_RECORD.
IF SY-SUBRC NE 0.
EXIT.
ENDIF.
SPLIT V_Record at CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB
INTO WA-FIELD1 WA-FIELD2.
APPEND WA TO ITAB.
ENDDO.
Reward if Helpful.
‎2007 Sep 12 9:15 AM
This is the code....
OPEN DATASET P_FILE FOR OUTPUT IN TEXT MODE.
IF SY-SUBRC NE 0.
MESSAGE E010(AD) WITH TEXT-E50 P_FILE.
ENDIF.
DO.
READ DATASET P_FILE INTO OUTPUT_TAB.
IF SY-SUBRC NE 0.
EXIT.
ENDIF.
APPEND OUTPUT_TAB.
ENDDO.
Reward if it useful..................
‎2007 Sep 12 9:17 AM
Hello ravindar,
***Code to read a file from application server.
DATA : BEGIN OF g_t_data OCCURS 0,
content(2000),
END OF g_t_data.
OPEN DATASET p_file FOR INPUT IN TEXT MODE.
IF sy-subrc EQ 0.
DO.
READ DATASET p_file INTO g_t_data-content.
IF sy-subrc <> 0.
EXIT.
ELSE.
APPEND g_t_data.
CLEAR g_t_data.
ENDIF.
ENDDO.
ENDIF.
CLOSE DATASET p_file.
***code to get all the files in a directory on application server
DATA: BEGIN OF g_t_searchpoints OCCURS 10,
dirname(75) TYPE c, " name of directory.
sp_name(75) TYPE c, " name of entry. (may end with *)
sp_cs(10) TYPE c, " ContainsString pattern for name.
END OF g_t_searchpoints.
DATA: BEGIN OF file,
dirname(75) TYPE c, " name of directory. (possibly truncated.)
name(75) TYPE c, " name of entry. (possibly truncated.)
type(10) TYPE c, " type of entry.
len(8) TYPE p, " length in bytes.
owner(8) TYPE c, " owner of the entry.
mtime(6) TYPE p, " last modification date, seconds since 1970
mode(9) TYPE c, " like "rwx-r-x--x": protection mode.
useable(1) TYPE c,
subrc(4) TYPE c,
errno(3) TYPE c,
errmsg(40) TYPE c,
mod_date TYPE d,
mod_time(8) TYPE c, " hh:mm:ss
seen(1) TYPE c,
changed(1) TYPE c,
END OF file.
DATA: BEGIN OF g_t_file_list OCCURS 100,
dirname(75) TYPE c, " name of directory. (possibly truncated.)
name(75) TYPE c, " name of entry. (possibly truncated.)
type(10) TYPE c, " type of entry.
len(8) TYPE p, " length in bytes.
owner(8) TYPE c, " owner of the entry.
mtime(6) TYPE p, " last modification date, seconds since 1970
mode(9) TYPE c, " like "rwx-r-x--x": protection mode.
useable(1) TYPE c,
subrc(4) TYPE c,
errno(3) TYPE c,
errmsg(40) TYPE c,
mod_date TYPE d,
mod_time(8) TYPE c, " hh:mm:ss
seen(1) TYPE c,
changed(1) TYPE c,
END OF g_t_file_list.
DATA : sap_no(1) VALUE ' ',
srt(1) VALUE 'T',
no_cs VALUE ' ', " no MUST_ContainString
g_f_start TYPE i.
DATA : l_f_val.
g_t_searchpoints-dirname = p_sfile.
g_t_searchpoints-sp_name = '*'.
g_t_searchpoints-sp_cs = ''.
APPEND g_t_searchpoints.
CLEAR g_t_searchpoints.
LOOP AT g_t_searchpoints.
PERFORM fill_file_list USING g_t_searchpoints-dirname
g_t_searchpoints-sp_name
g_t_searchpoints-sp_cs.
ENDLOOP.
SORT g_t_file_list BY mod_date DESCENDING
mtime DESCENDING.
***Check the file name based on your requirement.
***my requirement is to get the latest file in that directory.
LOOP AT g_t_file_list WHERE name CP ABC*'.
IF g_t_file_list-useable IS INITIAL.
DELETE g_t_file_list.
CONTINUE.
ENDIF.
IF g_t_file_list-name CP 'ABC*' AND l_f_val IS INITIAL.
CONCATENATE g_t_file_list-dirname g_t_file_list-name
INTO g_f_file
l_f_val = 'X'.
ENDIF.
ENDLOOP.
&----
*& Form fill_file_list
&----
text
----
-->P_G_T_SEARCHPOINTS_DIRNAME text
-->P_G_T_SEARCHPOINTS_SP_NAME text
-->P_G_T_SEARCHPOINTS_SP_CS text
----
FORM fill_file_list USING a_dir_name a_generic_name a_must_cs.
A_DIR_NAME ....... directory name
A_GENERIC_NAME ... generic filename (may end with *)
A_MUST_CS ........ a contains pattern for legal filenames OR NO_CS
*
DATA: errcnt(2) TYPE p VALUE 0.
IF a_dir_name IS INITIAL.
MESSAGE e220(s1). " 'Place cursor on valid line !'.
ENDIF.
CALL 'C_DIR_READ_FINISH' " just to be sure
ID 'ERRNO' FIELD g_t_file_list-errno
ID 'ERRMSG' FIELD g_t_file_list-errmsg.
CALL 'C_DIR_READ_START' ID 'DIR' FIELD a_dir_name
ID 'FILE' FIELD a_generic_name
ID 'ERRNO' FIELD file-errno
ID 'ERRMSG' FIELD file-errmsg.
IF sy-subrc <> 0.
MESSAGE e204(s1) WITH g_t_file_list-errmsg file-errmsg.
ENDIF.
DO.
CLEAR file.
CALL 'C_DIR_READ_NEXT'
ID 'TYPE' FIELD file-type
ID 'NAME' FIELD file-name
ID 'LEN' FIELD file-len
ID 'OWNER' FIELD file-owner
ID 'MTIME' FIELD file-mtime
ID 'MODE' FIELD file-mode
ID 'ERRNO' FIELD file-errno
ID 'ERRMSG' FIELD file-errmsg.
file-dirname = a_dir_name.
MOVE sy-subrc TO file-subrc.
CASE sy-subrc.
WHEN 0.
CLEAR: file-errno, file-errmsg.
CASE file-type(1).
WHEN 'F'. " normal file.
PERFORM filename_useable(rswatch0) USING file-name
file-useable.
WHEN 'f'. " normal file.
PERFORM filename_useable(rswatch0) USING file-name
file-useable.
WHEN OTHERS. " directory, device, fifo, socket,...
MOVE sap_no TO file-useable.
ENDCASE.
IF file-len = 0.
MOVE sap_no TO file-useable.
ENDIF.
WHEN 1.
EXIT.
WHEN OTHERS. " SY-SUBRC >= 2
ADD 1 TO errcnt.
IF errcnt > 10.
EXIT.
ENDIF.
IF sy-subrc = 5.
MOVE: '???' TO file-type,
'???' TO file-owner,
'???' TO file-mode.
ELSE.
ENDIF.
MOVE sap_no TO file-useable.
ENDCASE.
PERFORM p6_to_date_time_tz(rstr0400) USING file-mtime
file-mod_time
file-mod_date.
* Does the filename contains the requested pattern?
* Then store it, else forget it.
IF a_must_cs = no_cs.
MOVE-CORRESPONDING file TO g_t_file_list.
APPEND g_t_file_list.
ELSE.
IF file-name CS a_must_cs.
MOVE-CORRESPONDING file TO g_t_file_list.
APPEND g_t_file_list.
ENDIF.
ENDIF.
ENDDO.
CALL 'C_DIR_READ_FINISH'
ID 'ERRNO' FIELD g_t_file_list-errno
ID 'ERRMSG' FIELD g_t_file_list-errmsg.
IF sy-subrc <> 0.
WRITE: / 'C_DIR_READ_FINISH', 'SUBRC', sy-subrc.
ENDIF.
IF srt = 'T'.
SORT g_t_file_list BY mtime DESCENDING name ASCENDING.
ELSE.
SORT g_t_file_list BY name ASCENDING mtime DESCENDING.
ENDIF.
ENDFORM. " fill_file_list
Reward if useful..
Regards,
Phani.
‎2007 Sep 12 9:18 AM
Hi...
<b>**** Uploading from Application Server</b>
DATA:
T_APPREAD LIKE STANDARD TABLE
OF FS_DATA
WITH HEADER LINE.
<b>OPEN DATASET</b> W_FNAME FOR INPUT IN TEXT MODE ENCODING DEFAULT.
if sy-subrc ne 0. " <b>Validate file name</b>
<b> message 'No file found' type 'I'.</b>
else.
DO.
READ DATASET W_FNAME INTO T_APPREAD.
IF SY-SUBRC EQ 0.
APPEND T_APPREAD.
ELSE.
EXIT.
ENDIF.
ENDDO.
ENDIF.
CLOSE DATASET W_FNAME.
‎2007 Sep 12 9:19 AM
Hi,
This is the Program code for Application Server files.
Data has to be in a Txt file.
REPORT YRP00_20 .
DATA : IT_CARR TYPE TABLE OF SCARR,
WA_CARR TYPE SCARR.
parameters : p_dsn(30) DEFAULT 'AIRLINE.TXT'. "Give the path on App server
DATA : V_TEXT(40).
START-OF-SELECTION.
**Writing data to file
OPEN DATASET P_DSN FOR OUTPUT
IN TEXT MODE ENCODING DEFAULT.
IF SY-SUBRC NE 0.
WRITE:/ 'FILE NOT OPENED' .
EXIT.
ELSE.
SELECT * FROM SCARR INTO TABLE IT_CARR.
LOOP AT IT_CARR INTO WA_CARR.
TRANSFER WA_CARR TO P_DSN.
ENDLOOP.
CLOSE DATASET P_DSN.
ENDIF.
***reading from file
OPEN DATASET P_DSN FOR INPUT
IN TEXT MODE ENCODING DEFAULT.
IF SY-SUBRC = 0.
DO.
READ DATASET P_DSN INTO WA_CARR.
IF SY-SUBRC NE 0.
EXIT.
ENDIF.
WRITE:/ WA_CARR-CARRID,
WA_CARR-CARRNAME.
ENDDO.
CLOSE DATASET P_DSN.
ENDIF.
Assign points if helpful
Regards,
Sunil
‎2007 Sep 12 2:13 PM
Hi Phani,
Your code has some errors.........can u chk them and resend the code