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: 

Prevent uploading # character from a file

former_member407089
Participant
0 Kudos

Hello

I need to enchance a code given below. I'm not an ABAP programmer so it is really difficult to me. Here is a problem. I have a program that allows users to upload csv documents to BW that are push to DSO by DTP. Most of users know that # character is not allowed in csv file, but some of them by mistake or not try to upload files with such a character. That causes data corruption and DTP fail.

What I'd like to do is to let them upload files with any data they want and modify the program that way it removes any # character it finds.

Example of a file:

MATERIAL, APO Attribute 1 ,APO Attribute 2

000345654, #, #

334542454, Essential, Essential

I'd like it to be uploaded:

MATERIAL, APO Attribute 1 ,APO Attribute 2

000345654, ,

334542454, Essential, Essential

The program is quite long but as I understand ABAP this is the part that is important from my point of view because here the program uploads data from a file.

How to prevent # loading? How to trim that character?

*eject
*&---------------------------------------------------------------------*
*&      Form  1000_get_data
*&---------------------------------------------------------------------*
*       uploading data into internal table
*----------------------------------------------------------------------*
form 1000_get_data.

call function 'WS_UPLOAD'
EXPORTING
filename               
= p_file
filetype               
= 'DAT'
TABLES
data_tab               
= t_rec
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
gui_refuse_filetransfer
= 8
customer_error         
= 9
others                  = 10.

if sy-subrc ne 0.
message s999 with text-004.

stop.
endif.

refresh t_upload.

if p_skip ne c_space or
p_skip
ne 0.
delete t_rec to p_skip.
endif.

loop at t_rec into line.

CONCATENATE line ',' INTO line.

SPLIT line AT c_comma INTO  TABLE t_columns.

LOOP AT t_columns into wa_columns.

translate wa_columns to upper case.

if wa_columns eq c_delete or
wa_columns
eq c_caps.
clear wa_columns.
modify t_columns from wa_columns.
elseif wa_columns eq c_space.
wa_columns
= C_X.
modify t_columns from wa_columns.
endif.

clear wa_columns.
endloop.
CONCATENATE LINES OF t_columns INTO record SEPARATED BY c_comma.

split record at c_comma into t_upload-MATERIAL
t_upload
-/BIC/ZCAPOATT1
t_upload
-/BIC/ZCAPOATT2

append t_upload.

w_counter
w_counter + 1.

loop at t_upload from w_counter.

if t_upload-MATERIAL ne space and
t_upload
-MATERIAL co ' 0123456789'.
shift     t_upload-MATERIAL right deleting trailing space.
translate t_upload-MATERIAL using c_spzero.
endif.

perform 2100_convert_matnr_18 changing t_upload-MATERIAL.

clear /BIC/ZMATTABLE00.
select single
MATERIAl
RECORDMODE
/BIC/ZCAPOATT1
/BIC/ZCAPOATT2

  into  /BIC/ZMATTABLE00
from /BIC/ZMATTABLE00
where MATERIAL eq t_upload-MATERIAL.

if sy-subrc eq 0 or
sy
-subrc eq 4.


if t_upload-/BIC/ZCAPOATT1 eq 'X'.
t_upload
-/BIC/ZCAPOATT1 = /BIC/ZMATTABLE00-/BIC/ZCAPOATT1.
modify t_upload.
clear /BIC/ZMATTABLE00-/BIC/ZCAPOATT1.
endif.
if
t_upload
-/BIC/ZCAPOATT2 eq 'X'.
t_upload
-/BIC/ZCAPOATT2 = /BIC/ZMATTABLE00-/BIC/ZCAPOATT2.
modify t_upload.
clear /BIC/ZMATTABLE00-/BIC/ZCAPOATT2.
endif.

endif.
endloop.

endloop.

endform.                    "1000_get_data

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi Hed,

Use REPLACE command to replace all occurances to space ( ' ').

Use this help file for reference http://help.sap.com/saphelp_nw04/helpdata/en/fc/eb3398358411d1829f0000e829fbfe/content.htm

Regards,

Mahidhar.

8 REPLIES 8

Former Member
0 Kudos

Hi Hed,

Use REPLACE command to replace all occurances to space ( ' ').

Use this help file for reference http://help.sap.com/saphelp_nw04/helpdata/en/fc/eb3398358411d1829f0000e829fbfe/content.htm

Regards,

Mahidhar.

0 Kudos

Hi Mahirdar

but if I want to use REPLACE I must first load the data to internal table. Don't I? Or can I use it during data upload so they are # free when they go to internal table?

Where exactly should I insert that REPLACE code?

0 Kudos

WS_UPLOAD would need to be run first of course.

The REPLACE could be executed after LOOP AT t_rec INTO line .

REPLACE ALL OCCURRENCES OF '#' IN line WITH space .

0 Kudos

How do I need to modify

REPLACE ALL OCCURRENCES OF '#' IN line WITH space . code to be sure that also ! character will be excluded? Or better how to be sure that only ABCDE....Z characters will be allowed and all other _ % & * will be converted to "" (empty value)?

0 Kudos

Hi Andrzej,

First dump all the data from flat file to an internal table.

loop at t_rec into line.


CONCATENATE line ',' INTO line.

SPLIT line AT c_comma INTO  TABLE t_columns.

LOOP AT t_columns into wa_columns.

translate wa_columns to upper case.

Replace all occurences of "#' IN wa_columns WITH space.

.............

.............

endloop.

In order to be sure only the '#' character is excluded that can be checked in debugg mode.

Put the debug point at the start of loop.

Once after executing in debug mode you can check the contents of your internal table thereby you can check.

Former Member
0 Kudos

    DATA LV_NEWLINE VALUE CL_ABAP_CHAR_UTILITIES=>NEWLINE.
    DATA LV_SPACE  TYPE CHAR1.
    DATA LV_STRING TYPE STRING.
    MOVE SPACE TO LV_SPACE.
    CONCATENATE LV_SPACE '  ' INTO LV_STRING SEPARATED BY SPACE.
    REPLACE ALL OCCURRENCES OF REGEX LV_NEWLINE IN TABLE <table_name> WITH LV_STRING  RESPECTING CASE.

reward if helpful

Former Member
0 Kudos

Hi Hed,

Just after the code which uses "WS_UPLOAD" and before REFRESH t_upload statement, place the REPLACE command as suggested by many.

Regards,

Mahidhar.

former_member407089
Participant
0 Kudos

Thank you all for the help.