Application Development and Automation 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: 
Read only

Is it a text or binary file?

Former Member
0 Likes
4,010

Hello gurus,

I would like to know the following:

If I have to open a file (with 'OPEN DATASET [...]'), how do I know whether it is a text or a binary one?

If it is a text one, I have to perform some tasks; and if it's a binary one, I have to do others.

I think a command in Tx. 'SM69' could be used but... Isn't any other easier way available?

Thanks a lot!,

Eloi

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,568

Hi,

The only sure way is to read the data and then compare it with some arbitrary values to see if any of the data is outside of what is considered a text file.

Are the files structured in anyway ?  do they have header records or flags ?? Is the byte at the start of a binary file always (for example) 'FF' ?

If your system is running on Linux or Unix you could create an SM69 command for the command 'file' and then parse the output of that which will tell you

Check to see if there are any 0a0d byte combinations - binary files shouldn't contain linefeed/carriage return pairs.

Regards

Rich

10 REPLIES 10
Read only

Former Member
0 Likes
2,569

Hi,

The only sure way is to read the data and then compare it with some arbitrary values to see if any of the data is outside of what is considered a text file.

Are the files structured in anyway ?  do they have header records or flags ?? Is the byte at the start of a binary file always (for example) 'FF' ?

If your system is running on Linux or Unix you could create an SM69 command for the command 'file' and then parse the output of that which will tell you

Check to see if there are any 0a0d byte combinations - binary files shouldn't contain linefeed/carriage return pairs.

Regards

Rich

Read only

Former Member
0 Likes
2,568

Hello Eloi,

Even though the file is binary or text (at the source), the file type at the target is determined by what mode you are using for 'OPEN DATASET' to read the file.

- A file can be opened in Binary or ASCII irrespective of what is the source format.

I understand you are trying do the following.

IF BIN_FILE..

   Do XXXXX.

ELSEIF TEXT_FILE.

   Do XXXXX.

ENDIF.

If I were to do the above, I will do it the following way.

OPEN DATASET file_path FOR INPUT IN BINARY MODE.

IF successfully_opened.

      <<Do what you want to do when the file is opened in binary mode>>

ENDIF.

                    or

OPEN DATASET file_path FOR INPUT in TEXT MODE ENCODING <UTF-8>

IF successfully_opened.

      <<Do what you want to do when the file is opened in TEXT mode>>

ENDIF.

Yet, if you want to know the file type ( before using 'OPEN DATASET'), you can use the FM PC_SPLIT_COMPLETE_FILENAME - presuming the file exists with an extension!

Best Regards,

Pradeep.

Read only

0 Likes
2,568

Hi,

"Yet, if you want to know the file type ( before using 'OPEN DATASET'), you can use the FM PC_SPLIT_COMPLETE_FILENAME - presuming the file exists with an extension!"

And assuming the extension doesn't lie.  A common one is the use of XLS to denote Excel files.  A lot of people use XLS to denote a file readable by Excel - a subtle difference.  Native XLS is generally considered a binary file.  Whereas it may contain for example tab or comma delimited ascii files (in otherwords a text file).  So don't rely on the file extension.

Rich

Read only

0 Likes
2,568

Thanks for your help Pradeep but I may even don't have any extension at all in the file!

That is why I need to know which kind of file is prior to opening it.

Eloi

Read only

matt
Active Contributor
0 Likes
2,568

Even if you know all the files you receive are text - you have to know something about the structure to even begin to parse it. It's simply not possible to write a generic program that can read anything.

You know where the data is coming from, you know what application is writing it, you know quite a lot, probably, about the contents. Why wouldn't you know whether it is binary?


What is your scenario? Why don't you know?

Read only

0 Likes
2,568

Hi Matthew,

I know that if I receive a text file have to proceed in one way and if I receive a binary file have to proceed in the other.

The problem is that there is not any clue available in file name (or size) that determines one path or the other.

I have a server directory full of files and have to be attached as GOS in their corresponding documents (but some of them might be just text files). I know, at least, to which document has the file to be associated.

Thanks a lot for your contribution,

Eloi

Read only

0 Likes
2,568

The only real way to proceed is to read some of thedata in the file.  See my first answer.

Rich

Read only

0 Likes
2,568

That's exactly what the class CL_ABAP_FILE_UTILITIES do

Read only

Tomas_Buryanek
Product and Topic Expert
Product and Topic Expert
0 Likes
2,568

There is no sure way to determine this.

Why don't you always attach it in GOS as binary file?

Let the OS/extension settings to decide how to open that file later.

-- Tomas --
Read only

RaymondGiuseppi
Active Contributor
0 Likes
2,568

You could use some method of class CL_ABAP_FILE_UTILITIES like CHECK_FOR_BOM to identify some text UTF-8 file. (Read documentation of method) But what will you perform with an unknown file?


Regards,

Raymond