Example

Convert Text File Formats (Line Terminators)

Author: Carl Sassenrath
Return to REBOL Cookbook

If you have a text file that does not have the correct line terminators for your computer, this code will convert it.


    file: %doc.txt
    write file read file

This code works because the READ function converts a text file to the standard REBOL string format. The WRITE function then converts the text back to the format used by the local computer.

Why You Need It

Different operating systems use different characters to indicate the end of a line:

  • CR-LF on Windows systems
  • LF on Unix, Linux, and other Posix systems
  • CR on Macintosh

Depending on how you transfer files between computers, the terminators may not be properly converted. The code above can be used to solve that problem.

This code will convert all the text files found in your current directory:


    foreach file read %./ [
        if find [%.txt %.text] suffix? file [
            write file read file
        ]
    ]

You can add additional file suffixes if necessary. (Just be sure they are for text files. If you use this code on binary files, they will be corrupted.)


2006 REBOL Technologies REBOL.com REBOL.net